如何在bootstrap应用程序中添加ResourceDictionary
本文关键字:添加 ResourceDictionary 应用程序 bootstrap | 更新日期: 2023-09-27 18:08:30
在我的Bootstrapper应用程序中,我使用我自己的消息框。这个消息框需要样式化,样式化存在于dll
中。对于其他视图(xaml
),将其添加为ResourceDictionary
,如:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyApp;component/MyStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
或者通常任何C#
应用程序都有app.xaml
,其中可以添加这种样式并且它可以工作。对于Bootstrapper,我无法添加app.xaml
或提供此ResourceDictionary
。指针吗?
在能够将资源字典添加到应用程序资源之前,您必须做两件事。由于本机Bootstrapper托管我们的WPF窗口,所以不会自动设置应用程序。这可以通过简单地创建一个System.Windows.Application
实例来实现。其次,您必须设置Application.ResourceAssembly
。完成这两个步骤后,您就可以使用Application.Current.Resources
访问应用程序资源了。完整的代码示例:
public class CustomBootstrapper : BootstrapperApplication
{
protected override void Run()
{
if (Application.Current == null)
{
new Application();
}
if (Application.ResourceAssembly == null)
{
var assembly = typeof(CustomBootstrapper).Assembly;
Application.ResourceAssembly = assembly;
}
var myStyle = (ResourceDictionary)Application.LoadComponent(new Uri("/styling/MyStyle.xaml", UriKind.Relative));
Application.Current.Resources.MergedDictionaries.Add(myStyle);
var view = new MainWindow();
view.Show();
}
}
我希望这能回答你的问题