我的WPF项目引用另一个项目。在参考项目中,我在其中定义了一些颜色和画笔。
共享的dll资源:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Color x:Key="FooColor">#FF00FF00</Color>
<SolidColorBrush x:Key="FooColorBrush" Color="{DynamicResource FooColor}" />
</ResourceDictionary>
主项目的App.xaml:
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Shared;component/UI/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
如何在运行时修改共享dll资源的颜色/笔刷?
在我的app . example .cs中,我尝试了:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
//base.OnStartup(e)
// Didn't work - tried modifying the color from this.Resources
this.Resources.Remove("FooColor");
this.Resources.Add("FooColor", Color.FromArgb(255, 255, 255, 0));
// Didn't work - tried modifying the color from the Merged Dictionary
ResourceDictionary sharedResources = this.Resources.MergedDictionaries.First(x => x.Source.OriginalString.Contains("/Shared;component/UI/Resources.xaml"));
sharedResources.Remove("FooColor");
sharedResources.Add("FooColor", Color.FromArgb(255, 255, 255, 0));
}
}
如果我试图修改的资源是在当前项目的App.xaml中定义的,那么这工作得很好:
this.Resources.Remove(resourceKey);
this.Resources.Add(resourceKey, newValue);
要测试它,在我的主窗口。我有:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Shared;component/UI/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<TextBlock Foreground="{DynamicResource FooColorBrush}" Text="Hello world" />
</Window>
期望是前景色将被更改为我在运行时设置的值在我的App.xaml.cs (Color.FromArgb(255, 255, 255, 0)
),而不是在共享资源xaml文件(<Color x:Key="FooColor">#FF00FF00</Color>
)中定义的颜色
明白了。我所要做的就是从MainWindow.xaml中删除合并字典。我的App.xaml.cs代码正在修改App.xaml的MergedDictionary,但由于我也在主窗口上定义了MergedDictionary。xaml,主窗口。xaml正在获取未被覆盖的资源的另一个副本,并且优先于App.xaml中编辑的副本。
改变:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Shared;component/UI/Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<TextBlock Foreground="{DynamicResource FooColorBrush}" Text="Hello world" />
</Window>
:
<Window x:Class="MyProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<TextBlock Foreground="{DynamicResource FooColorBrush}" Text="Hello world" />
</Window>
或者,我可以添加用于更改资源的逻辑到我的mainwindow . example .cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Resources.Remove("FooColor");
this.Resources.Add("FooColor", Color.FromArgb(255, 255, 255, 0));
}
}