我有一个 C# 项目,原本是一个 WPF 应用程序,但现在我想将它构建为 DLL。我之前通过从项目中移除 app.xaml 并将其构建类型设置为 dll 来完成了这个过程。
现在的问题是,app.xaml 包含一些 XAML 代码来实例化应用程序变量。为了解决这个问题,我正在尝试在第一个调用的 XAML 窗口中从代码中设置这些应用程序变量。
我正试图在代码中模拟以下 XAML 代码:
现在的问题是,app.xaml 包含一些 XAML 代码来实例化应用程序变量。为了解决这个问题,我正在尝试在第一个调用的 XAML 窗口中从代码中设置这些应用程序变量。
我正试图在代码中模拟以下 XAML 代码:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Shared.xaml"/>
<ResourceDictionary Source="Resources/Styles/ToolBar.xaml"/>
<ResourceDictionary Source="Resources/Styles/GroupBox.xaml"/>
<ResourceDictionary Source="Resources/Styles/ZoomBox.xaml"/>
<ResourceDictionary Source="Resources/Styles/ScrollBar.xaml"/>
<ResourceDictionary Source="Resources/Styles/Expander.xaml"/>
<ResourceDictionary Source="Resources/ApplicationToolbar.xaml"/>
<ResourceDictionary Source="Resources/DesignerItem.xaml"/>
<ResourceDictionary Source="Resources/Styles/ToolboxItem.xaml"/>
<ResourceDictionary Source="Resources/Styles/Toolbox.xaml"/>
<ResourceDictionary Source="Resources/Connection.xaml"/>
<ResourceDictionary Source="Resources/Slider.xaml"/>
<ResourceDictionary Source="Resources/ScrollViewer.xaml"/>
<ResourceDictionary Source="Resources/StatusBar.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这是我的代码:
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中声明资源的地方。
<LinearGradientBrush x:Key="ToolbarSelectedBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFFEE3" Offset="0.0"/>
<GradientStop Color="#FFE797" Offset="0.4"/>
<GradientStop Color="#FFD750" Offset="0.4"/>
<GradientStop Color="#FFE796" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
这是它在toolbar.xaml中的引用位置。
<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" />
抱歉我的问题有点啰嗦,但我想提供尽可能多的信息。如果你需要其他帮助,请告诉我。
App.xaml
并想要编写代码来解决问题。事实证明,你仍然可以使用App.xaml
,参见 https://dev59.com/ilLTa4cB1Zd3GeqPcada#4441500 。然后调用 DLL 实例化App
,就像 https://dev59.com/6XE85IYBdhLWcg3wnUwd#26890426 中所示。对我来说,所有资源都能正常工作,设计时和运行时都能正常使用相对 URI。 - Stéphane Gourichon