我可以在WPF应用程序中使用ResourceDictionary和UserControl

时间:2018-05-23 12:00:21

标签: wpf xaml resourcedictionary

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

我已添加UserControl并引入插件,效果很好。但是当我尝试添加Material设计样式http://materialdesigninxaml.net/并将Resources添加到UserControl时,我得到了InitializeComponent()方法的错误:

  

System.Windows.Markup.XamlParseException:'在System.Windows.Markup.StaticResourceHolder'上提供价值。抛出异常。'

     

NotImplementedException:未实现方法或操作。

UserControl代码:

<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吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

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