WPF Prism(三)ViewModelLocator

本文介绍了WPF Prism框架中如何通过MVVM模式实现View与ViewModel的关联。具体包括基于命名约定自动匹配、使用AutoWireViewModel属性简化配置及通过ViewModelLocationProvider自定义映射的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF Prsim(一)Region
WPF Prism(二)Module
WPF Prism(三)ViewModelLocator
WPF Prism(四)MVVM
WPF Prism(五)Navigation
WPF Prism(六)Dialog

一、命名约定

在Prism当中,你可以基于命名约定,便能够轻松的将View和ViewMode建立关联。前提是你已经为项目创建了Views和ViewModels文件夹(这里就突出了使用模板创建的好处了)。

例如:你的页面为ViewA,那么对应的ViewModel名称为ViewAViewModel。

​ 你的页面为HeaderView,那么对应的ViewModel名称为HeaderViewModel。

二、允许View自动装配ViewModel

<UserControl
    x:Class="Menu.Views.MenuView"
    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:Menu.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Background="Blue"
    mc:Ignorable="d">
    <Grid>
        <TextBlock
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Text="{Binding Message}" />
    </Grid>
</UserControl>

需要将添加这一句设置:

prism:ViewModelLocator.AutoWireViewModel="True"

经过上述两个步骤,我们就可以将View和View Model建立关联了。但是如果我们很任性,就是不想创建那样的文件夹,也不想要那样的名称,该怎么办呢?

三、使用ViewModelLocationProvider指定View与ViewModel的类型

不知道为什么我没有实验成功。。。所以暂不做解释。。。

### WPFPrism 9 框架使用教程 #### 初始化项目结构 为了在 WPF 中集成 Prism 9,需创建一个新的 WPF 应用程序,并通过 NuGet 安装 `Prism.Wpf` 包。这一步骤确保了所有必要的依赖项被正确引入。 ```xml <ItemGroup> <PackageReference Include="Prism.Wpf" Version="9.0.0" /> </ItemGroup> ``` #### 修改 App.xaml 文件 更新 `App.xaml` 来支持 Prism 特定的功能: - 添加 Prism 命名空间声明; - 继承自 `PrismApplication` 类而非默认的应用类; - 移除原有的 `StartupUri` 属性以启用由代码控制启动逻辑的方式[^1]。 ```xml <Application x:Class="YourNamespace.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" Startup="OnStartup"> <!-- ... --> </Application> ``` #### 实现应用入口配置 调整 `App.xaml.cs` 文件中的内容如下所示: - 调整基类为 `PrismApplication`; - 复写 `CreateShell()` 方法指明主窗口位置; - 在 `RegisterTypes()` 函数里登记所需的服务实例。 ```csharp protected override Window CreateShell() { return Container.Resolve<MainWindow>(); } protected override void RegisterTypes(IContainerRegistry containerRegistry) { // 注册服务和其他组件... } ``` #### 设置视图模型关联机制 对于每一个 XAML 页面而言,在其根节点处加入属性 `prism:ViewModelLocator.AutoWireViewModel="True"` 可让框架依据约定自动匹配对应的 View Model 对象[^3]。 ```xml <UserControl ... prism:ViewModelLocator.AutoWireViewModel="True"> <!-- UI Elements Here... --> </UserControl> ``` #### 利用区域管理功能构建模块化界面布局 借助于 Region Manager 提供的支持,能够轻松地向页面内嵌入可替换的内容区块。下面的例子展示了怎样定义名为 "MainRegion" 的 ContentControl 并赋予它作为容器的角色[^2]。 ```xml <ContentControl Name="MainRegion" prism:RegionManager.RegionName="MainRegion"/> ``` #### 使用 DelegateCommand 构造交互指令 当涉及到响应用户的操作请求时,推荐采用来自 `Prism.Commands` 名字空间下的 `DelegateCommand<T>` 或者无泛型版本来封装事件处理逻辑[^4]。 ```csharp public class MainViewModel : BindableBase { public ICommand MyCommand { get; private set; } public MainViewModel() { MyCommand = new DelegateCommand(ExecuteMyCommand); } private void ExecuteMyCommand() => MessageBox.Show("Hello!"); } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值