翼度科技»论坛 编程开发 .net 查看内容

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

7

主题

7

帖子

21

积分

新手上路

Rank: 1

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


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

1. 安装Prism NuGet包

在项目中执行以下命令:
  1. Install-Package Prism.Wpf
复制代码
2. 创建ViewModel
  1. using Prism.Mvvm;
  2. public class MainViewModel : BindableBase
  3. {
  4.     private string _message;
  5.     public string Message
  6.     {
  7. <Grid>
  8.     <TextBlock Text="{Binding Message}" />
  9.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  10. </Grid>get { return _message; }
  11. <Grid>
  12.     <TextBlock Text="{Binding Message}" />
  13.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  14. </Grid>set { SetProperty(ref _message, value); }
  15.     }
  16. }
复制代码
3. 创建View
  1. <Window x:
  2. <Grid>
  3.     <TextBlock Text="{Binding Message}" />
  4.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  5. </Grid>xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. <Grid>
  7.     <TextBlock Text="{Binding Message}" />
  8.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  9. </Grid>xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  10. <Grid>
  11.     <TextBlock Text="{Binding Message}" />
  12.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  13. </Grid>xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  14. <Grid>
  15.     <TextBlock Text="{Binding Message}" />
  16.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  17. </Grid>xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  18. <Grid>
  19.     <TextBlock Text="{Binding Message}" />
  20.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  21. </Grid>xmlns:prism="http://prismlibrary.com/"
  22. <Grid>
  23.     <TextBlock Text="{Binding Message}" />
  24.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  25. </Grid>prism:ViewModelLocator.AutoWireViewModel="True"
  26. <Grid>
  27.     <TextBlock Text="{Binding Message}" />
  28.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  29. </Grid>mc:Ignorable="d"
  30. <Grid>
  31.     <TextBlock Text="{Binding Message}" />
  32.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  33. </Grid>Title="MainWindow" Height="350" Width="525">
  34.     <Grid>
  35. <Grid>
  36.     <TextBlock Text="{Binding Message}" />
  37.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  38. </Grid><TextBlock Text="{Binding Message}" />
  39.     </Grid>
  40. </Window>
复制代码
4. 注册ViewModel

在App.xaml.cs中注册ViewModel:
  1. using Prism.Ioc;
  2. using Prism.Unity;
  3. using YourNamespace.Views;
  4. namespace YourNamespace
  5. {
  6.     public partial class App : PrismApplication
  7.     {
  8. <Grid>
  9.     <TextBlock Text="{Binding Message}" />
  10.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  11. </Grid>protected override Window CreateShell()
  12. <Grid>
  13.     <TextBlock Text="{Binding Message}" />
  14.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  15. </Grid>{
  16. <Grid>
  17.     <TextBlock Text="{Binding Message}" />
  18.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  19. </Grid>    return Container.Resolve<MainWindow>();
  20. <Grid>
  21.     <TextBlock Text="{Binding Message}" />
  22.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  23. </Grid>}
  24. <Grid>
  25.     <TextBlock Text="{Binding Message}" />
  26.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  27. </Grid>protected override void RegisterTypes(IContainerRegistry containerRegistry)
  28. <Grid>
  29.     <TextBlock Text="{Binding Message}" />
  30.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  31. </Grid>{
  32. <Grid>
  33.     <TextBlock Text="{Binding Message}" />
  34.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  35. </Grid>    containerRegistry.RegisterForNavigation<YourView>();
  36. <Grid>
  37.     <TextBlock Text="{Binding Message}" />
  38.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  39. </Grid>}
  40.     }
  41. }
复制代码
5. 在View中使用ViewModel
  1. <Grid>
  2.     <TextBlock Text="{Binding Message}" />
  3.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  4. </Grid>
复制代码
6. 在ViewModel中处理命令
  1. using Prism.Commands;public class MainViewModel : BindableBase{    private string _message;    public string Message    {<Grid>
  2.     <TextBlock Text="{Binding Message}" />
  3.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  4. </Grid>get { return _message; }<Grid>
  5.     <TextBlock Text="{Binding Message}" />
  6.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  7. </Grid>set { SetProperty(ref _message, value); }    }    public DelegateCommand UpdateMessageCommand { get; }    public MainViewModel()    {<Grid>
  8.     <TextBlock Text="{Binding Message}" />
  9.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  10. </Grid>UpdateMessageCommand = new DelegateCommand(UpdateMessage);    }    private void UpdateMessage()    {<Grid>
  11.     <TextBlock Text="{Binding Message}" />
  12.     <Button Command="{Binding UpdateMessageCommand}" Content="Update Message" />
  13. </Grid>Message = "Hello, Prism!";    }}
复制代码
以上是使用Prism的基本示例。Prism提供了更多的功能,如模块化开发、事件聚合器、导航框架等,以帮助构建结构良好、可维护的WPF应用。
 



来源:https://www.cnblogs.com/hanbing81868164/p/17995756
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具