WPF共享资源问题-没有App.xaml,没有共享资源
本文关键字:共享资源 xaml 没有 问题 WPF App | 更新日期: 2023-09-27 17:54:07
我正在开发的一个应用程序遇到了一个小问题。
我正在为我公司的应用程序做一个模块。该应用程序是一个WinForm应用程序,但我一直在开发一个WPF应用程序(不是真正的应用程序,如您所见),当它完成后将托管在这个WinForm应用程序中。
要做到这一点,我使用WinForm元素主机,我已经创建了一个"shell"用户控件,然后在该shell用户控件内的其他用户控件窗口。所以它看起来像一个WPF应用程序,并且只使用WinForm应用程序作为它的启动项目,因为WPF应用程序实际上只是一个WPF控件的集合。
我遇到的问题是,因为我没有创建一个实际的"WPF应用程序",没有App.xaml。这使我无法以我想要的方式使用共享资源,尤其是XAML共享资源。
是否有一种方法,我仍然可以把我的WPF用户控件的集合作为一个WPF应用程序,并以某种方式使用App.xaml文件为我的资源。如果没有,在我的应用程序中使用共享资源有哪些选项。
将ResourceDictionary
(xaml)文件添加到您的项目中(假设它是一个类库- WPF自定义控件库),然后将其合并到Generic.xaml
之上您将能够参考它,并且您的StaticResource
将工作。
还可以将资源包含在泛型中。Xaml(或其他Xaml文件)文件本身
你现在的字典应该是这样的:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">
<sys:String x:Key="myString">sdfasdf</sys:String>
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Text" Value="{StaticResource myString}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<TextBlock Text="{TemplateBinding Text}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
我在VS2010中初始化了上述控件的设计时实例,它显示了文本(Text
是我手动添加到CustomControl1
的字符串DP属性),这意味着它读取myString
资源。
你可以在这里和这里找到更具体的信息