是否可以使用不在WPF应用程序中的ResourceDictionary和UserControl

hujrc8aj  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(141)

我将为CAD应用程序创建一个插件。它的API允许我们引入ClassLibrary项目,并实现我们可以使用UserControl的UI。
我已经添加了UserControl并引入了插件,它工作得很好。但是当我尝试添加材质设计样式http://materialdesigninxaml.net/并将资源添加到UserControl时,我在InitializeComponent()方法上得到错误,调用:
System.Windows.Markup.XamlParseException:在“System.Windows.Markup.StaticResourceHolder”上提供值时引发了异常。
未实现异常:方法或操作未实现。
用户控件代码:

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

kcwpcxri

kcwpcxri1#

1.-安装材料设计主题nuget:安装包材料设计主题
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>
x8diyxa7

x8diyxa72#

我也遇到过同样的问题,对我有效的方法是在wpf应用程序的入口点项目上安装nuget包。如果你不把MaterialDesign的资源字典放在用户控件之外的任何地方(正如你所发布的),它不应该影响应用程序的其他部分,至少对我来说是这样

相关问题