WPF、Simple Injector 和 MaterialDesignThemes 静态资源无效
WPF, Simple Injector and MaterialDesignThemes Static resource invalid
我有用 WPF 编写并使用简单注入器和 Material 设计主题的示例应用程序。
这是我的程序文件:
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: ''Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '44' and line position '21'.' Exception: Cannot find resource named 'MaterialDesignFloatingHintTextBox'. Resource names are case sensitive.
此网页上是带有 StaticResource 的示例应用程序(我从该应用程序中获取了代码):
https://www.c-sharpcorner.com/article/wpf-application-with-googles-material-design/
而且有效。我能看到的唯一区别是我的应用程序有 Simple Injector 而示例中的应用程序没有。
两个应用中的引用相同:
您应该安装 MaterialDesignThemes
NuGet 包并将以下资源字典添加到您的 App.xaml
文件中,如 docs:
中所述
<?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 容器无关。
您需要将资源导入您的应用程序才能使用它们。
假设您将 ResourceDictionaries
提供给 App
class,如 @mm8 的答案中给出的那样,您应该通过调用 [=] 加载并应用 ResourceDictionaries
14=] 在 App
class.
的构造函数中
像这样:
public partial class App : Application
{
public App()
{
this.InitializeComponent();
}
}
我在你的问题中看到注释掉了这一行。这可能是遵循 Simple Injector 文档中提供的启动代码并在此之后添加 Material 设计主题的结果。
然而,当您将 MergedDictionaries
添加到 App.xaml 时,此代码是必需的。所以你需要把它加回来。
我有用 WPF 编写并使用简单注入器和 Material 设计主题的示例应用程序。 这是我的程序文件:
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: ''Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '44' and line position '21'.' Exception: Cannot find resource named 'MaterialDesignFloatingHintTextBox'. Resource names are case sensitive.
此网页上是带有 StaticResource 的示例应用程序(我从该应用程序中获取了代码):
https://www.c-sharpcorner.com/article/wpf-application-with-googles-material-design/
而且有效。我能看到的唯一区别是我的应用程序有 Simple Injector 而示例中的应用程序没有。 两个应用中的引用相同:
您应该安装 MaterialDesignThemes
NuGet 包并将以下资源字典添加到您的 App.xaml
文件中,如 docs:
<?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 容器无关。
您需要将资源导入您的应用程序才能使用它们。
假设您将 ResourceDictionaries
提供给 App
class,如 @mm8 的答案中给出的那样,您应该通过调用 [=] 加载并应用 ResourceDictionaries
14=] 在 App
class.
像这样:
public partial class App : Application
{
public App()
{
this.InitializeComponent();
}
}
我在你的问题中看到注释掉了这一行。这可能是遵循 Simple Injector 文档中提供的启动代码并在此之后添加 Material 设计主题的结果。
然而,当您将 MergedDictionaries
添加到 App.xaml 时,此代码是必需的。所以你需要把它加回来。