K.Mvvm.Ioc.Wpf
3.1.0
The owner has unlisted this package.
This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package K.Mvvm.Ioc.Wpf --version 3.1.0
NuGet\Install-Package K.Mvvm.Ioc.Wpf -Version 3.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="K.Mvvm.Ioc.Wpf" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="K.Mvvm.Ioc.Wpf" Version="3.1.0" />
<PackageReference Include="K.Mvvm.Ioc.Wpf" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add K.Mvvm.Ioc.Wpf --version 3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: K.Mvvm.Ioc.Wpf, 3.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package K.Mvvm.Ioc.Wpf@3.1.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=K.Mvvm.Ioc.Wpf&version=3.1.0
#tool nuget:?package=K.Mvvm.Ioc.Wpf&version=3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Mvvm.Ioc.Wpf
实现WPF开发中MVVM模式和IOC的结合,方便为View注入ViewModel。典型用法如下:
开始
定义View
可以在View的构造函数中通过依赖注入的方式获取指定的DataContext,已解决ViewModel有参构造函数的问题:
- HelloPage.xaml
<Page x:Class="Mvv.Ioc.Wpf.Test.Views.HelloPage"
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:vm="clr-namespace:Mvv.Ioc.Wpf.Test.ViewModels"
Title="HelloPage"
d:DataContext="{d:DesignInstance Type=vm:HelloViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
FontWeight="Bold"
Text="{Binding Data}" />
<Button Grid.Row="1" Content="Change" Command="{Binding ChangeCommand}"/>
</Grid>
</Page>
public partial class HelloPage : Page
{
public HelloPage()
{
DataContext = App.Current.Services.GetRequiredService<IViewModel<HelloPage>>();
InitializeComponent();
}
}
还可以通过DataContextExtension可以使用依赖注入来获取ViewModel,解决了ViewModel包含有参构造函数的问题,并保证所有的代码都在xaml文件中进行设置,而无需在后台代码中进行设置:
- WorldPage.xaml
<Page x:Class="Mvv.Ioc.Wpf.Test.Views.WorldPage"
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:vm="clr-namespace:Mvv.Ioc.Wpf.Test.ViewModels"
Title="WorldPage"
d:DataContext="{d:DesignInstance Type=vm:WorldViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
DataContext="{DataContextExtension}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="30"
FontWeight="Bold"
Text="{Binding Data}" />
<Button Grid.Row="1"
Content="Reflush"
Command="{Binding ReflushCommand}" />
</Grid>
</Page>
也可以明确其ViewModel类型:
DataContext="{DataContextExtension ViewModelType=vm:WorldViewModel}"
如果不指定ViewModelType,则会获取IVewModel<WorldPage>的实例作为DataContext(前提是注入了IVewModel<WorldPage>服务的实现)。
定义ViewModel
internal partial class WorldViewModel(IViewModel<HelloPage> hello) : ObservableObject, IViewModel<WorldPage>
{
private readonly IViewModel<HelloPage> _hello = hello;
[ObservableProperty]
private string _data = $"World !";
[RelayCommand]
private void Reflush()
{
Data = $"{((HelloViewModel)_hello).Data} {Data}";
}
}
internal partial class HelloViewModel : ObservableObject, IViewModel<HelloPage>
{
[ObservableProperty]
private string _data = "Hello";
[RelayCommand]
private void Change()
{
Data = "Good";
}
}
修改App类
public partial class App : WpfApplication
{
protected override void OnConfigureServices(IServiceCollection services)
{
services.AddViewModels(ServiceLifetime.Transient);
}
}
注意修改App.xaml文件中App类型的父类为WpfApplication:
<WpfApplication x:Class="Mvv.Ioc.Wpf.Test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources />
</WpfApplication>
在MainWindow中使用View
<Window
x:Class="Mvv.Ioc.Wpf.Test.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:local="clr-namespace:Mvv.Ioc.Wpf.Test"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:view="clr-namespace:Mvv.Ioc.Wpf.Test.Views"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<view:HelloPage />
</Window>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|