WPF Prism的简单使用

本文详细介绍了如何在WPF项目中使用Prism框架,从简单使用到更深入的实践,包括BootStrapper配置、Region管理、View Discovery和激活/停用视图等。通过实例展示了如何创建PrismApplication、注册View到Region以及如何控制UI元素的显示与隐藏。
摘要由CSDN通过智能技术生成

简单使用

  1. 新建 WPF 项目,我是基于 .net 5.0-windows 创建的。
  2. 引入 Prism.DryIoc(8.1.97) 的 Nuget 包。
  3. App.xaml 中引入命名空间。
xmlns:prism="http://prismlibrary.com/"
  1. 将 App.xaml 中 Application 标签更改成 prism:PrismApplication 并去除 StartUri 属性,将 App.xaml.cs 中 Application 更改成 PrismApplication
  2. 实现 PrismApplication(实际上是 PrismApplicationBase) 中的抽象方法。
protected override Window CreateShell() // Creates the shell or main window of the application
{
   
    return new MainWindow();
}

protected override void RegisterTypes(IContainerRegistry containerRegistry) // Used to register types with the container that will be used by your application.
{
   
    
}
  1. 运行成功(如遇到问题可一起交流)。

更进一步

以下将以 prism-samples-wpf 里的项目为说明对象。

01-BootStrapperShell/BootStrapperShell

此项目中并没有在 App.xamlApp.xaml.cs 中引入 PrismApplication ,而是通过新建继承自 PrismBootstrapperBootStrapper 类型,在该类型中实现了 PrismBootStrapperBase 中的两个方法。

protected override Window CreateShell() // Creates the shell or main window of the application.
{
    
	return Container.Resolve<MainWindow>();
}

protected override void RegisterTypes(IContainerRegistry containerRegistry) // Used to register types with the container that will be used by your application.
{
        
}

简单使用App.xaml.cs 实现的两个方法名称与参数都一样,实际上我猜测作用也可能是一样的。
值得注意的是,在BootStrapper类型中CreateShell方法通过 Container.Resolve < MainWindow >() 返回主窗口, 暂且定义此方法的作用是通过类型返回实例。

02-Regions/Regions

此项目以及后续项目都是采用如 简单使用 中的写法,在 Views/MainWindow.xaml 中采用了如下写法:

<ContentControl prism:RegionManager.RegionName="ContentRegion" /
以下是一个简单WPF Prism 使用范例,该范例演示了如何使用 Prism 构建一个基本的 WPF 应用程序: 1. 创建一个新的 WPF 应用程序项目。 2. 使用 NuGet 安装 Prism 库。 3. 在 App.xaml 文件中添加以下内容: ```xml <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" x:Class="WpfPrismExample.App"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Prism.Mvvm;component/Prism.Mvvm.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> <Application.StartupUri> <Uri xmlns="" /> </Application.StartupUri> </Application> ``` 4. 在 MainWindow.xaml 文件中添加以下内容: ```xml <Window x:Class="WpfPrismExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Title="MainWindow" Height="350" Width="525"> <Grid> <ContentControl prism:RegionManager.RegionName="MainRegion" /> </Grid> </Window> ``` 5. 创建一个名为 MainViewModel 的视图模型类,并在其中添加一些属性和方法。 ```csharp using Prism.Mvvm; namespace WpfPrismExample.ViewModels { public class MainViewModel : BindableBase { private string _title = "WPF Prism Example"; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } public MainViewModel() { } } } ``` 6. 创建一个名为 MainView 的视图类,并在其中绑定视图模型的属性。 ```xml <UserControl x:Class="WpfPrismExample.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True"> <Grid> <TextBlock Text="{Binding Title}" /> </Grid> </UserControl> ``` 7. 在 App.xaml.cs 文件中添加以下内容: ```csharp using Prism.Ioc; using Prism.Unity; using System.Windows; using WpfPrismExample.Views; namespace WpfPrismExample { public partial class App : PrismApplication { protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation<MainView>(); } } } ``` 8. 运行应用程序,应该会看到一个包含“WPF Prism Example”文本的窗口。 这只是一个简单的示例,但它演示了如何使用 Prism 构建可扩展的 WPF 应用程序。通过使用 Prism,您可以轻松地将应用程序拆分为模块,并使用依赖注入来管理它们之间的依赖关系。此外,Prism 还提供了一套强大的命令和事件系统,使您可以轻松地实现 MVVM 模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值