假设我的 Application.xaml 中有一些 ResourceDictionary 定义如下:
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="brushResources"/>
<ResourceDictionary x:Name="graphicsResources"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
我本以为我可以简单地访问后面代码中的字典,如下所示:
brushResources.MergedDictionaries.Add(someNewBrushes)
graphicsResources.MergedDictionaries.Add(someNewGraphics)
Run Code Online (Sandbox Code Playgroud)
但事实似乎并非如此,这是处理 ResourceDictionary 的错误方法吗?我真的很喜欢使用 MEF 动态加载资源的能力,但这对我来说似乎是一个绊脚石。
编辑
修复了示例代码以显示字典实际上位于 MergedDictionaries 中
我有以下ResourceDictionary,它被合并到我的Themes/Generic.xaml文件中
<DataTemplate DataType="{x:Type model:RequirementResourceRelation}" x:Key="{x:Static local:Resources.RequirementResourceRelationListTemplateKey}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Resource.Name, TargetNullValue=Loading...}" />
<TextBlock Grid.Column="1" Text="-" />
<TextBlock Grid.Column="2" MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Path=RelationType, TargetNullValue=Loading...}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type model:RequirementResourceRelation}" x:Key="{x:Static local:Resources.RequirementResourceRelationListTemplate2Key}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Requirement.Name, TargetNullValue=Loading...}" />
<TextBlock Grid.Column="1" Text="-" />
<TextBlock Grid.Column="2" MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Path=RelationType, TargetNullValue=Loading...}" …Run Code Online (Sandbox Code Playgroud) 如果资源字典不可观察,那么DynamicResource引用如何工作?资源字典的添加/删除方法是否具有内部代码,以便"轮询"所有DynamicResource引用并在删除或添加它们时刷新它们?
我试图从其他文件加载WPF样式实际上从WPF自定义控件库,但我无法加载这里是我的解决方案.
该解决方案包含两个项目
WPF自定义控件库类型的WpfTestControls
WPF应用程序库类型的WpfTestApp,它引用了WpfTestControls
来自WPF应用程序库的MainWindow.xaml
<Window.Resources>
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
</Window.Resources>
<Grid>
<TextBox Height="50px" Width="100px" Style="{DynamicResource TempStyle}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
来自WPF自定义控件库的Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfTestControls;component/TextBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
Run Code Online (Sandbox Code Playgroud)
来自WPF自定义控件库的TextBoxStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TempStyle" TargetType="{x:Type TextBox}">
<Setter Property="BorderBrush" Value="Green"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
我的AssemblyInfo.cs文件包含以下内容
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource …Run Code Online (Sandbox Code Playgroud) 我正在开发一个有很多屏幕的WPF应用程序,每个屏幕都有很多控件.它很难确定哪个控件具有焦点.
所以我想强调目前关注它的控件.它可以是文本框,按钮,组合框,列表框或网格.
如果我们可以使用样式和触发器来做这件事会更好.
谢谢
我有两个程序集,每个程序集都提供了一组我希望包含在我的应用程序中的通用样式和资源.我正在使用合并的词典App.xaml来加载它们,并且在运行时它们很好.不幸的是,这些资源不会在设计时加载,在我的错误窗口中填充有关无法解析的资源的消息,并给我一个不代表实际显示内容的UI.
这是我现在的App.xaml:
<Application x:Class="ClientDebug.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dai.Common;component/Xaml/Common.xaml" />
<ResourceDictionary Source="/Dai.DevExpress;component/Xaml/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
对于这两个合并的词典,我在"错误"窗口中收到以下错误:
Error 11 An error occurred while finding the resource dictionary "/Dai.Common;component/Xaml/Common.xaml". C:\DevProjects\Core Application\ClientDebug\App.xaml 12 17 ClientDebug
Error 12 An error occurred while finding the resource dictionary "/Dai.DevExpress;component/Xaml/Styles.xaml". C:\DevProjects\Core Application\ClientDebug\App.xaml 13 17 ClientDebug
Run Code Online (Sandbox Code Playgroud)
这显然缺乏有用的信息.同样,它们在运行时加载正如您所期望的那样,但在设计时没有任何资源可用.
我的MyControl.cs应用程序项目(.exe)中有一个WPF自定义控件,其样式位于资源字典中MyControlResources.xaml.此xaml app.xaml作为合并字典的一部分指定.一切正常.
现在我想将此自定义控件移动到应用程序引用的现有DLL项目中.有没有办法可以在DLL中创建资源字典"赋值"并使其对调用者透明,即应用程序项目可以像使用任何内置控件一样使用它,而不需要您了解有关资源字典的任何信息?
我读过关于创建一个新的自定义控件项目可以做到这一点,但它只是一个我不想为其创建新项目的控件.任何人都知道如何在现有的类库DLL中执行此操作?
我在包含颜色和画笔的WPF类库中定义了一个Reource Dictionary,名为BrushResources.xaml.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Lots of Colors and Brushes here>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
我想在另一个程序集的代码中使用一些Brushes,它引用了这个库项目.如何获得它的实例ResourceDictionary?
我正在使用MahApps.Metro,版本0.11.0.9-ALPHA for .NET 4.5.并想知道引用其资源词典的最佳方式.
有人告诉我,我不应该在App.xaml中将它们作为合并字典引用.如果不是这样,我是否需要为每个窗口添加它们作为Window.Resources?
我这样做:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Red.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
这个问题是一些控件表现得很时髦,例如窗口上的最小化,最大化和关闭按钮显示为没有文本的黑色块.当我将这些资源字典引用为Window.Resources时,一切正常......
我正在使用WPF类库,而不是应用程序。这是我在c#中制作的Label的示例,我想使用XAML对其进行“样式化”。
private void CreateElement(int i)
{
UIElementOut[i] = new Label();
var uiElement = (Label)UIElementOut[i];
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.FontFamily = new FontFamily(FFontInput[i]);
uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
uiElement.Content = TextIn[i];
Brush BgBrushColor = new SolidColorBrush(RGBAToMediaColor(FBgCol[i]));
Brush FgBrushColor = new SolidColorBrush(RGBAToMediaColor(FFgCol[i]));
uiElement.Background = BgBrushColor;
uiElement.Foreground = FgBrushColor;
Uri uri = new Uri("Styles/LabelStyle.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetContentStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
ResourceDictionary myResourceDictionary = (ResourceDictionary)reader.LoadAsync(info.Stream);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style;
uiElement.Style = myLabelStyle;
}
Run Code Online (Sandbox Code Playgroud)
为此,我有包含我的LabelStyle的ressourcedictionnary,所有内容都可以正常编译。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" …Run Code Online (Sandbox Code Playgroud)