wpf - 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/
它有效。我能看到的唯一区别是我的应用程序具有简单注射器,而示例中的应用程序没有。两个应用程序中的引用相同:
解决方案
您应该安装MaterialDesignThemes
NuGet 包并将以下资源字典添加到您的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 容器无关。
您需要将资源导入您的应用程序才能使用它们。
推荐阅读
- google-drive-api - Google Drive API - 当多个用户登录时从 webViewLink 为特定用户加载文档
- python - itertools.product 参数的类型?
- android - 如何在用户单击注销之前安全地删除 Firestore 侦听器?
- clion - 打开 AzerothCore 项目时出现 CLion 错误
- python - 在 Python 中使用斯坦福 CoreNLP 进行情感分析
- javascript - Jquery querySelectorAll 使用除类以外的其他属性
- ruby-on-rails - 在 rails 6 alpha 上安装 https://github.com/twbs/bootstrap-rubygem
- c++ - gdb 正在跳过指令
- c# - 如何在插入数据的同时将一张表复制到另一张表
- java - 与等效的 Java 代码相比,C# BitConverter 输出错误的双精度值