在 Winform App 中定义 WPF 资源字典
Define WPF Resource Dictionary in Winform App
我正在测试是否能够创建可在现有 Winforms 应用程序中使用的 WPF 控件。通过此测试,目标是全局使用 WPF 资源字典。
我在控件中定义样式设置的第一步工作正常。
<UserControl.Resources>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="LightYellow"/>
<Setter Property="FontSize" Value="30"/>
</Style>
<Style TargetType="Label" x:Key="myHW">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="FontSize" Value="30"/>
</Style>
<Style TargetType="Label" x:Key="StatusMessage">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="LightPink"/>
</Style>
</UserControl.Resources>
其次是将样式信息提取到字典 XAML 文件中,然后通过资源字典引用它。此步骤不起作用。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionaries/ResourceTest.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
程序集名称: test2
项目内部路径:/Dictionaries/ResourceTest.xaml
开发中收到的错误是:
异常:查找资源字典“/Dictionaries/ResourceTest.xaml”时出错。
我尝试了 Microsoft 提供的 URI 语法的变体。
xaml 的构建操作类型是否与该问题相关?
感谢 Jeff R. 的回复。这是问题之一。
我继续搞砸了,终于解决了多个问题。由于这是在沙盒中的 Winforms 应用程序中创建的,因此我创建了一个 WPF 应用程序,然后添加了一个资源字典。对两个资源字典 xaml 文件进行了比较,以确定任何差异。
资源字典 xaml 文件需要更新以下设置:
- 构建操作 = 页面
- 自定义工具 = XamlIntelliSenseFileGenerator
此外,字典文件中缺少正确的开始标记。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
最后,由于手动输入源路径不起作用,我尝试使用“属性”页面“源”下拉选项,然后填写所需的路径语法,然后匹配 Jeff R. 推荐的内容。
总结起来,存在三个问题:
- 资源字典文件属性设置
- XAML 缺少开始标记
- URI 引用语法
我正在测试是否能够创建可在现有 Winforms 应用程序中使用的 WPF 控件。通过此测试,目标是全局使用 WPF 资源字典。
我在控件中定义样式设置的第一步工作正常。
<UserControl.Resources>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="LightYellow"/>
<Setter Property="FontSize" Value="30"/>
</Style>
<Style TargetType="Label" x:Key="myHW">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="FontSize" Value="30"/>
</Style>
<Style TargetType="Label" x:Key="StatusMessage">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="LightPink"/>
</Style>
</UserControl.Resources>
其次是将样式信息提取到字典 XAML 文件中,然后通过资源字典引用它。此步骤不起作用。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionaries/ResourceTest.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
程序集名称: test2
项目内部路径:/Dictionaries/ResourceTest.xaml
开发中收到的错误是:
异常:查找资源字典“/Dictionaries/ResourceTest.xaml”时出错。
我尝试了 Microsoft 提供的 URI 语法的变体。
xaml 的构建操作类型是否与该问题相关?
感谢 Jeff R. 的回复。这是问题之一。
我继续搞砸了,终于解决了多个问题。由于这是在沙盒中的 Winforms 应用程序中创建的,因此我创建了一个 WPF 应用程序,然后添加了一个资源字典。对两个资源字典 xaml 文件进行了比较,以确定任何差异。
资源字典 xaml 文件需要更新以下设置:
- 构建操作 = 页面
- 自定义工具 = XamlIntelliSenseFileGenerator
此外,字典文件中缺少正确的开始标记。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
最后,由于手动输入源路径不起作用,我尝试使用“属性”页面“源”下拉选项,然后填写所需的路径语法,然后匹配 Jeff R. 推荐的内容。
总结起来,存在三个问题:
- 资源字典文件属性设置
- XAML 缺少开始标记
- URI 引用语法