创建基于Prism框架的WPF应用(NET Framework)项目
1、创建WPF(NET Framework)项目并整理结构
(1)、创建WPF(NET Framework)项目;
(2)、添加Views和ViewModels文件夹;
(3)、将MainWindow.xaml移到Views文件夹;
(4)、在ViewModels文件中新建 MainWindowViewModel.cs
(5)、修改App.xaml
StartupUri="MainWindow.xaml"
改为
StartupUri="Views/MainWindow.xaml"
(6)、修改MainWindow.xaml
x:Class="DemoTest.MainWindow"
改为
x:Class="DemoTest.Views.MainWindow"
(7)、修改MainWindow.xaml.cs
namespace HscpTest
改为
namespace HscpTest.Views
(8)、项目编译、运行正常。
2、升级为Prism框架
(1)、安装依赖包Prism.Unity 8.1.97
注意依赖包的版本,其它版本未必兼容。
(2)、在MainWindow.xaml中添加Prism引用,并自动开启
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
(3)、修改MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var vm = DataContext as MainWindowViewModel;
//throw new NotImplementedException();
}
(4)、修改App.xaml
<prism:PrismApplication x:Class="HscpTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:HscpTest"
xmlns:prism="http://prismlibrary.com/"
StartupUri="Views/MainWindow.xaml">
<!--xmlns:prism="http://prismlibrary.com/"-->
<Application.Resources>
</Application.Resources>
</prism:PrismApplication>
(5)、修改App.xaml.cs
protected override Window CreateShell()
{
return Container.Resolve<Views.MainWindow>();
//throw new NotImplementedException();
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
//containerRegistry.Register<Views.MainWindow>();
//throw new NotImplementedException();
}
(6)、删除App.xaml的启动设置
StartupUri="Views/MainWindow.xaml"