如何将 userControl 的 ResourceDictionary 合并到 app.xaml?
How can I merged ResourceDictionary of userControl into app.xaml?
我创建了 UserControl 并编写了 ResourcesDictionary。有一些样式。
我想将 ResourceDictionary 合并到 App.xaml。
但是Main Project是A,还有App.xaml.
而UserControl在B项目中,所以他们住在另一个地方。
如何合并 ResourceDictonary?
这是我的部分代码,但它不起作用。
第一次尝试 App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myUserControl.xaml"/>
</ResourceDictionary.MergedDictionaries> ......
第二次尝试
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/B;component/SubFolderName/myUserControl.xaml" />
</ResourceDictionary.MergedDictionaries> ......
第三次尝试
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/B;component/SubFolderName/myUserControl.xaml" />
</ResourceDictionary.MergedDictionaries> ......
都没有用.....
+ myUserControl.xaml
<UserControl x:Class="........."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:B"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="100">
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="TitleTest" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}">
<Setter Property="Margin" Value="15"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style x:Key="PanelTestStyle" TargetType="StackPanel">
<Setter Property="ClipToBounds" Value="True"/>
<Setter Property="Height" Value="55"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="15"/>
</Style>
<Style x:Key="TestStyle2" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}">
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="FontSize" Value="40"/>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<TextBlock Text="{Binding StatusTitle}" Style="{DynamicResource TitleTest}"/>
<StackPanel Style="{StaticResource PanelTestStyle}">
<TextBlock x:Name="testText" Style="{DynamicResource TestStyle2}" Text="{Binding StatusNumber}"/>
<TextBlock x:Name="testText2" Style="{DynamicResource TestStyle2}" Text="TEST" />
</StackPanel>
</Grid>
此外,我想获取Setter值来更改cs代码中的FontSize,所以我需要设置DynamicResource(TextBlocks)。
您的项目 'A' 中必须有对项目 'B' 的引用,然后您的示例 'Second Try' 应该可以工作
您必须将 ResourceDictionary 放在单独的 xaml 文件中;不在 App.xaml。此文件必须如下所示:
<ResourceDictionary>
<Style x:Key="TitleTest" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}">
<Setter Property="Margin" Value="15"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="FontSize" Value="20"/>
</Style>
...
如果你想在同一个 dll 中使用这个 ResourceDictionary,你可以使用你第一次尝试的代码来访问它。如果您想在另一个 dll 中使用它,您可以使用第二次尝试的代码访问它。
我创建了 UserControl 并编写了 ResourcesDictionary。有一些样式。
我想将 ResourceDictionary 合并到 App.xaml。
但是Main Project是A,还有App.xaml.
而UserControl在B项目中,所以他们住在另一个地方。
如何合并 ResourceDictonary?
这是我的部分代码,但它不起作用。
第一次尝试 App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="myUserControl.xaml"/>
</ResourceDictionary.MergedDictionaries> ......
第二次尝试
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/B;component/SubFolderName/myUserControl.xaml" />
</ResourceDictionary.MergedDictionaries> ......
第三次尝试
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/B;component/SubFolderName/myUserControl.xaml" />
</ResourceDictionary.MergedDictionaries> ......
都没有用.....
+ myUserControl.xaml
<UserControl x:Class="........."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:B"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="100">
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="TitleTest" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}">
<Setter Property="Margin" Value="15"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="FontSize" Value="20"/>
</Style>
<Style x:Key="PanelTestStyle" TargetType="StackPanel">
<Setter Property="ClipToBounds" Value="True"/>
<Setter Property="Height" Value="55"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="15"/>
</Style>
<Style x:Key="TestStyle2" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}">
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="FontSize" Value="40"/>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<TextBlock Text="{Binding StatusTitle}" Style="{DynamicResource TitleTest}"/>
<StackPanel Style="{StaticResource PanelTestStyle}">
<TextBlock x:Name="testText" Style="{DynamicResource TestStyle2}" Text="{Binding StatusNumber}"/>
<TextBlock x:Name="testText2" Style="{DynamicResource TestStyle2}" Text="TEST" />
</StackPanel>
</Grid>
此外,我想获取Setter值来更改cs代码中的FontSize,所以我需要设置DynamicResource(TextBlocks)。
您的项目 'A' 中必须有对项目 'B' 的引用,然后您的示例 'Second Try' 应该可以工作
您必须将 ResourceDictionary 放在单独的 xaml 文件中;不在 App.xaml。此文件必须如下所示:
<ResourceDictionary>
<Style x:Key="TitleTest" TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}">
<Setter Property="Margin" Value="15"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="FontSize" Value="20"/>
</Style>
...
如果你想在同一个 dll 中使用这个 ResourceDictionary,你可以使用你第一次尝试的代码来访问它。如果您想在另一个 dll 中使用它,您可以使用第二次尝试的代码访问它。