1、开发环境,Win10+VS2022+.NET8+Prism.DryIoc(9.0.537)或Prism.Unity。
2、通过NuGet安装Prism.DryIoc(9.0.537)或Prism.Unity。
2.1、创建ViewModels文件夹用于存放ViewModel文件、创建Views文件夹存放View文件。
将App.xaml文件修改,代码如下。
<prism:PrismApplication x:Class="PrismSimpleLearn.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/"xmlns:local="clr-namespace:PrismSimpleLearn"><!--StartupUri="Views/MainWindow.xaml"--><Application.Resources></Application.Resources>
</prism:PrismApplication>
App.xaml.cs,代码如下。
//using Prism.Unity;
using Prism.Ioc;
using Prism.Mvvm;
using System.Windows;
using PrismSimpleLearn.Views;
using System;
using System.Reflection;
using System.ComponentModel;
using PrismSimpleLearn.ViewModels;
using static System.Net.Mime.MediaTypeNames;
using System.Reflection.Metadata;
using System.Security.Claims;
using System.Windows.Controls;
using System.Windows.Media.Media3D;
using System.Xml.Linq;
using PrismSimpleLearn.Core;
using PrismSimpleLearn.BLL;
using PrismSimpleLearn.Module;namespace PrismSimpleLearn
{/// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : PrismApplication{//设置启动起始页protected override Window CreateShell(){return Container.Resolve<MainWindow>();}protected override void RegisterTypes(IContainerRegistry containerRegistry){//通过IOC容器注册IApplicationCommands为单例//containerRegistry.RegisterSingleton<IApplicationCommands, ApplicationCommands>();}protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){//base.ConfigureModuleCatalog(moduleCatalog);//moduleCatalog.AddModule<CompositeCommandMoudle>();}//配置规则protected override void ConfigureViewModelLocator(){ViewModel注册方式0/// ViewModelLocator.AutoWireViewModel = "True" //< Window x: Class = "PrismSimpleLearn.Views.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:PrismSimpleLearn"// xmlns: prism = "http://prismlibrary.com/"// mc: Ignorable = "d"// Title = "MainWindow" Height = "450" Width = "800" prism: ViewModelLocator.AutoWireViewModel = "True" >// <!--// prism:ViewModelLocator.AutoWireViewModel = "True", 表示自动绑定View和ViewModel// prism:ViewModelLocator.AutoWireViewModel="False", 表示不自动绑定View和ViewModel,并且其它的绑定也会失效// prism:ViewModelLocator.AutoWireViewModel="{x:Null}",表示不自动绑定View和ViewModel,但其它的绑定不会失效// --> // < StackPanel >// < TextBlock Text = "{Binding Title}" FontSize = "16" />// < Button x: Name = "btnClicke" Command = "{Binding ClickCommand}" Height = "30" Width = "200" Content = "DelegateCommand" />// </ StackPanel >//</ Window >ViewModel注册方式1//base.ConfigureViewModelLocator();//ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>//{// var viewName = viewType.FullName.Replace(".Views.", ".ViewModels.");// var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;// var viewModelName = $"{viewName}ViewModel, {viewAssemblyName}";// return Type.GetType(viewModelName);//});ViewModel注册方式2//base.ConfigureViewModelLocator();//ViewModelLocationProvider.Register<MainWindow, MainWindowViewModel>();ViewModel注册方式3//base.ConfigureViewModelLocator();//ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), typeof(MainWindowViewModel));ViewModel注册方式4//base.ConfigureViewModelLocator();//ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), () => Container.Resolve<MainWindowViewModel>());// ViewModel注册方式5//base.ConfigureViewModelLocator();//ViewModelLocationProvider.Register<MainWindow>(() => Container.Resolve<MainWindowViewModel>());//ViewModelLocationProvider.Register<GetNameSubcommand>(() => Container.Resolve<GetNameSubcommandViewModel>());}}}
2.2、ViewModel的注册方式。
2.2.1、在xaml中使用prism: ViewModelLocator.AutoWireViewModel 进行注册,先在xaml中引入xmlns: prism = "http://prismlibrary.com/"。
prism: ViewModelLocator.AutoWireViewModel 有三种赋值方式
prism:ViewModelLocator.AutoWireViewModel = "True", 表示自动绑定View和ViewModel
prism:ViewModelLocator.AutoWireViewModel="False", 表示不自动绑定View和ViewModel,并且其它的绑定也会失效
prism:ViewModelLocator.AutoWireViewModel="{x:Null}",表示不自动绑定View和ViewModel,但其它的绑定不会失效
以MainWindow.xaml为例,代码如下。
<Window x:Class="PrismSimpleLearn.Views.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:PrismSimpleLearn"xmlns:prism="http://prismlibrary.com/"xmlns:i="http://schemas.microsoft.com/xaml/behaviors"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800" prism:ViewModelLocator.AutoWireViewModel="True"><ScrollViewer Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=ActualWidth}" Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=ActualHeight}"VerticalAlignment="Center" HorizontalAlignment="Center" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"><StackPanel><!--简单的命令绑定--><TextBlock Text="{Binding Title}" FontSize="36" HorizontalAlignment="Center" Width="200" Height="50" Margin="0,30,0,0"/><Button x:Name="btnClicke" Command="{Binding ClickCommand}" Height="30" Width="200" Content="DelegateCommand"/></StackPanel></ScrollViewer> </Window>
MainWindowViewModel.cs,绑定MVVM需要ViewModel集成BindableBase代码如下。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using Prism.Commands;
using Prism.Mvvm;
using PrismSimpleLearn.Core;namespace PrismSimpleLearn.ViewModels
{public class MainWindowViewModel:BindableBase{#region DelegateCommandprivate string _title = "Prism";public string Title{get { return _title; }set { _title = value;RaisePropertyChanged(); }//另一种方式//set { SetProperty(ref _title, value); }}private DelegateCommand _clickCommand;public DelegateCommand ClickCommand{get{return _clickCommand ?? (_clickCommand = new DelegateCommand(Click));}}private void Click(){Title = "Clicked";}#endregion }
}
3、运行效果如下图。
4、其它的注册方式可以先设置prism:ViewModelLocator.AutoWireViewModel="{x:Null}"。