C# WPF用C重写资源字典中的键#
我有一个wpf应用程序,其资源字典如下:C# WPF用C重写资源字典中的键#,c#,wpf,colors,config,resourcedictionary,C#,Wpf,Colors,Config,Resourcedictionary,我有一个wpf应用程序,其资源字典如下: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Color x:Key="Primary200">
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="Primary200">#FFE6FDF9</Color>
<Color x:Key="Primary500">#FF00E6BE</Color>
<Color x:Key="Primary700">#FF01987F</Color>
<SolidColorBrush x:Key="Background" Color="{StaticResource Primary500}"/>
</ResourceDictionary>
<Application x:Class="ResourceTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/ResourceTest;component/Colors.xaml"/>
</Application.Resources>
</Application>
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Resources.Remove("Primary500");
Resources.Add("Primary500", Colors.Red);
var mainWindow = new MainWindow();
mainWindow.Show();
}
}
#FFE6FDF9
#FF00E6BE
#FF01987F
但是现在,在应用程序启动时,我有时想更改颜色
我试过这样的方法:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="Primary200">#FFE6FDF9</Color>
<Color x:Key="Primary500">#FF00E6BE</Color>
<Color x:Key="Primary700">#FF01987F</Color>
<SolidColorBrush x:Key="Background" Color="{StaticResource Primary500}"/>
</ResourceDictionary>
<Application x:Class="ResourceTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/ResourceTest;component/Colors.xaml"/>
</Application.Resources>
</Application>
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Resources.Remove("Primary500");
Resources.Add("Primary500", Colors.Red);
var mainWindow = new MainWindow();
mainWindow.Show();
}
}
公共部分类应用程序
{
启动时受保护的覆盖无效(StartupEventArgs e)
{
基础。启动时(e);
资源。移除(“Primary500”);
资源。添加(“Primary500”,颜色。红色);
var mainWindow=新的mainWindow();
mainWindow.Show();
}
}
这里的问题是“背景”没有改变,因为它是用原始颜色计算的
最后,从配置文件中读取新颜色-所以我在编译时不知道它。
你知道怎么做吗
提前谢谢你我想你的
资源字典
在应用程序中被引用了。参考资料
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="YourDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这是附加到
应用程序的ResourceDictionary
,还是合并到中的单独词典?试试{DynamicResource…}
,不管怎样是的,它已附加到应用程序的idea!我知道,由于性能损失,在用户控件中使用DynamicSource是“危险的”。那么在资源字典中使用动态资源是否存在类似的问题?