创建基于Prism框架的WPF应用(NET Framework)项目

创建基于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

csharp 复制代码
StartupUri="MainWindow.xaml"

改为

csharp 复制代码
StartupUri="Views/MainWindow.xaml"

(6)、修改MainWindow.xaml

csharp 复制代码
x:Class="DemoTest.MainWindow"

改为

csharp 复制代码
x:Class="DemoTest.Views.MainWindow"

(7)、修改MainWindow.xaml.cs

csharp 复制代码
namespace HscpTest

改为

csharp 复制代码
namespace HscpTest.Views

(8)、项目编译、运行正常。

2、升级为Prism框架

(1)、安装依赖包Prism.Unity 8.1.97
注意依赖包的版本,其它版本未必兼容。

(2)、在MainWindow.xaml中添加Prism引用,并自动开启

csharp 复制代码
xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"

(3)、修改MainWindow.xaml.cs

csharp 复制代码
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

csharp 复制代码
<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

csharp 复制代码
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的启动设置

csharp 复制代码
StartupUri="Views/MainWindow.xaml"
相关推荐
samble3 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas3 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
dalong105 天前
WPF:Modbus 状态监控
wpf·modbus
He BianGu5 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota8 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota9 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota9 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头9 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
gis开发之家10 天前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4
clz131452111 天前
如何设计一个优雅可靠的金融级联机接口
wpf