【问题标题】:Can I use ResourceDictionary and UserControl not in WPF app我可以在 WPF 应用程序中使用 ResourceDictionary 和 UserControl 吗
【发布时间】:2018-11-02 09:57:47
【问题描述】:

我将为 Solid Works 应用程序创建一个插件。 Solid Works 的 API 允许我们引入 ClassLibrary 项目,并且我们可以使用 UserControl 来实现 UI。

我添加了 UserControl 并引入了插件,效果很好。但是当我尝试添加材料设计样式http://materialdesigninxaml.net/ 并将资源添加到 UserControl 时,我在调用 InitializeComponent() 方法时收到错误:

System.Windows.Markup.XamlParseException: 'Provide value on 'System.Windows.Markup.StaticResourceHolder' 引发了异常。'

NotImplementedException:方法或操作未实现。

用户控件代码:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Makerbot.Print.Addin.Wpf"
         x:Class="Makerbot.Print.Addin.Wpf.MyAddinControl"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid>

</Grid>

您能否指定我可以在 WPF 应用程序之外使用 ResourceDictionary 和 UserControl 吗?

【问题讨论】:

    标签: wpf xaml resourcedictionary


    【解决方案1】:

    1.- 安装 MaterialDesignThemes nuget:安装包 MaterialDesignThemes

    2.- 编辑 AXML 文件

    <UserControl ......
    
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    TextElement.FontWeight="Regular"
    TextElement.FontSize="13"
    TextOptions.TextFormattingMode="Ideal"
    TextOptions.TextRenderingMode="Auto"
    Background="{DynamicResource MaterialDesignPaper}"
    FontFamily="{DynamicResource MaterialDesignFont}">
    
    
    <UserControl.Resources>
        <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,对我有用的是在 wpf 应用程序的入口点项目上安装 nuget 包。 如果您没有将 MaterialDesign 的资源字典放在您的用户控件之外的任何位置(如您发布的那样),它不应该影响应用程序的其他部分,至少对我来说没有

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多