逛码头 发表于 2024-1-30 09:47:27

Prism:打造WPF项目的MVVM之选,简化开发流程、提高可维护性

 
概述:探索WPF开发新境界,借助Prism MVVM库,实现模块化、可维护的项目。强大的命令系统、松耦合通信、内置导航,让您的开发更高效、更流畅
在WPF开发中,一个优秀的MVVM库是Prism。以下是Prism的优点以及基本应用示例:
优点:


[*]模块化设计: Prism支持模块化开发,使项目更易维护和扩展。
[*]强大的命令系统: 提供了DelegateCommand等强大的命令实现,简化了用户交互操作的绑定。
[*]松耦合的通信: 通过EventAggregator实现松耦合的组件间通信,提高了代码的可维护性。
[*]内置导航系统: 提供了灵活的导航框架,支持导航到不同的视图和传递参数。
使用步骤:

1. 安装Prism NuGet包

在项目中执行以下命令:
Install-Package Prism.Wpf2. 创建ViewModel

using Prism.Mvvm;

public class MainViewModel : BindableBase
{
    private string _message;

    public string Message
    {
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>get { return _message; }
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>set { SetProperty(ref _message, value); }
    }
}3. 创建View

<Window x:
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>xmlns:prism="http://prismlibrary.com/"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>prism:ViewModelLocator.AutoWireViewModel="True"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>mc:Ignorable="d"
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>Title="MainWindow" Height="350" Width="525">
    <Grid>
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid><TextBlock Text="{Binding Message}" />
    </Grid>
</Window>4. 注册ViewModel

在App.xaml.cs中注册ViewModel:
using Prism.Ioc;
using Prism.Unity;
using YourNamespace.Views;

namespace YourNamespace
{
    public partial class App : PrismApplication
    {
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>protected override Window CreateShell()
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>{
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>    return Container.Resolve<MainWindow>();
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>}

<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>protected override void RegisterTypes(IContainerRegistry containerRegistry)
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>{
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>    containerRegistry.RegisterForNavigation<YourView>();
<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>}
    }
}5. 在View中使用ViewModel

<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>6. 在ViewModel中处理命令

using Prism.Commands;public class MainViewModel : BindableBase{    private string _message;    public string Message    {<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>get { return _message; }<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>set { SetProperty(ref _message, value); }    }    public DelegateCommand UpdateMessageCommand { get; }    public MainViewModel()    {<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>UpdateMessageCommand = new DelegateCommand(UpdateMessage);    }    private void UpdateMessage()    {<Grid>
    <TextBlock Text="{Binding Message}" />
    <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
</Grid>Message = "Hello, Prism!";    }}以上是使用Prism的基本示例。Prism提供了更多的功能,如模块化开发、事件聚合器、导航框架等,以帮助构建结构良好、可维护的WPF应用。
 



来源:https://www.cnblogs.com/hanbing81868164/p/17995756
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Prism:打造WPF项目的MVVM之选,简化开发流程、提高可维护性