首页 > 解决方案 > WPF、Simple Injector 和 MaterialDesignThemes 静态资源无效

问题描述

我有用 WPF 编写并使用简单注入器和材料设计主题的示例应用程序。这是我的程序文件:

private static Container Bootstrap()
{
    // Create the container as usual.
    var container = new Container();

    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

    // Register your types, for instance:
    container.Register<IFreewayReviewCreatorDbContext, FreewayReviewCreatorDbContext>(Lifestyle.Scoped);
    container.Register<IUnitOfWorkFactory, UnitOfWorkFactory>(Lifestyle.Transient);
    container.Register<IUnitOfWork, UnitOfWork>(Lifestyle.Scoped);
    container.Register<IReviewBodyBLL, ReviewBodyBLL>(Lifestyle.Transient);

    // Register your windows and view models:
    container.Register<MainWindow>();
    container.Register<MainWindowViewModel>();

    container.Verify();

    return container;
}

private static void RunApplication(Container container)
{
    try
    {
        var app = new App();
        //app.InitializeComponent();
        var mainWindow = container.GetInstance<MainWindow>();
        app.Run(mainWindow);
    }
    catch (Exception ex)
    {
        //Log the exception and exit
    }
}

在上面的代码中,视图模型添加在 Simple Injector 中注册。

现在在 MainWindow 中,我想使用 Material Design 中的 StaticResource。这是我的代码:

<Window x:Class="FreewayReviewCreator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FreewayReviewCreator"
        xmlns:localvm="clr-namespace:FreewayReviewCreator.ViewModel"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        mc:Ignorable="d" Loaded="MainWindow_OnLoaded"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel HorizontalAlignment = "Left">

            <TextBox
                    Name="tbxPassword"
                 Text="{Binding Password, Mode = TwoWay}"    
                    HorizontalContentAlignment="Center"                                                        
                    Style="{StaticResource MaterialDesignFloatingHintTextBox}"         
                    MaxLength="28"
                    materialDesign:HintAssist.Hint="Enter your username"    
                            />

错误在这一行Style="{StaticResource MaterialDesignFloatingHintTextBox}"::

System.Windows.Markup.XamlParseException: ''在 'System.Windows.StaticResourceExtension' 上提供值引发异常。' 行号“44”和行位置“21”。例外:找不到名为“MaterialDesignFloatingHintTextBox”的资源。资源名称区分大小写。

在此处输入图像描述

在这个网页上是带有 StaticResource 的示例应用程序(我从这个应用程序中获取了代码):

https://www.c-sharpcorner.com/article/wpf-application-with-googles-material-design/ 

它有效。我能看到的唯一区别是我的应用程序具有简单注射器,而示例中的应用程序没有。两个应用程序中的引用相同:

在此处输入图像描述

标签: wpfsimple-injectormaterial-design-in-xaml

解决方案


您应该安装MaterialDesignThemesNuGet 包并将以下资源字典添加到您的App.xaml文件中,如文档中所述:

<?xml version="1.0" encoding="UTF-8"?>
<Application . . .>
    <Application.Resources>
        <ResourceDictionary>
            <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>
    </Application.Resources>
</Application> 

这与简单的注入器或您使用的任何 IoC 容器无关。

您需要将资源导入您的应用程序才能使用它们。


推荐阅读