从代码设置应用程序资源
我有一个作为WPF应用程序的ac #project但我现在想把它构建为一个dll。 我以前通过从项目中删除app.xaml并将其构建类型设置为dll来完成此操作。
我现在的问题是app.xaml包含一些xaml来实例化应用程序变量。 为了解决这个问题,我试图以编程方式在将要调用的第一个xaml窗口中设置这些应用程序变量。
我试图在代码中模拟的xaml是:
这是我的代码:
ResourceDictionary myResourceDictionary = new ResourceDictionary(); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Shared.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolBar.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\GroupBox.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ZoomBox.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ScrollBar.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Expander.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\ApplicationToolbar.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\DesignerItem.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolboxItem.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Toolbox.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Connection.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\Slider.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\ScrollViewer.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("C:\\Resources\\StatusBar.xaml"); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
这有用吗?
我遇到的问题是Toolbar.xaml引用了Shared.xaml中声明的资源,但它没有被拾取并且我得到以下错误。
Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.
这是资源在shared.xaml中进行delcared的地方
这是在toolbar.xaml中引用它的地方
对于一个问题的文章感到抱歉,但是我认为id尽可能多地提供信息。 需要帮助请叫我。
这段代码适合我。 刚刚将URIS改为亲戚:
ResourceDictionary myResourceDictionary = new ResourceDictionary(); myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary); myResourceDictionary.Source = new Uri("Dictionary2.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
我认为您需要指定资源所在的组件的名称
如果你的dll名为My.Wpf.Component.dll,你应该放置My.Wpf.Component
所以在代码中它应该是
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"/;component/Resources/Styles/Shared.xaml", UriKind.Relative) });
你必须创建单独的ResourceDictionary文件,例如包含(不要忘记命名空间)的Style.xaml
结束在你所有的控件中引用它