侧边栏壁纸
博主头像
陌上花 博主等级

回首万事皆休

  • 累计撰写 72 篇文章
  • 累计创建 11 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录
WPF

Prsim基本使用

种向日葵的人
2025-06-23 / 0 评论 / 0 点赞 / 7 阅读 / 0 字

Why

  • 最近闲着无聊就研究了一下几个好用点的WPF的框架,发现Prism还是挺好用的,不管是在NetFrameWork还是Core上,大致步骤都是大差不差,以下以Core作为基本介绍。

How

  1. 注意使用NetFrameWork的时候需要选择的版本是8以下。如果是Core的话选择最新的就可以了。(以下是Framework的安装截图)
    prism-unity-framework.png
  2. 更改你的App.xaml.cs
 public partial class App : PrismApplication
 {
     protected override Window CreateShell()
     {
         return Container.Resolve<Main>();

     }

     protected override void RegisterTypes(IContainerRegistry containerRegistry)
     {
         var iRegionManager = containerRegistry.GetContainer().Resolve<IRegionManager>();
         if (iRegionManager == null)
             return;
        //以下是简单使用注册的,忽略
         //iRegionManager.RegisterViewWithRegion(RegionName.MenuRegion, typeof(Menu));
         //iRegionManager.RegisterViewWithRegion(RegionName.MainRegion, typeof(Tools));

     }
 }
  1. 更改你的App.xaml
<prism:PrismApplication
    x:Class="PQToolBoxCore.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:PQToolBoxCore"
    xmlns:prism="http://prismlibrary.com/">
    <!--  以下是如何使用Prism的资源字典 可忽略  -->
    <prism:PrismApplication.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml" />
                <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml" />
                <ResourceDictionary Source="/Common/string.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </prism:PrismApplication.Resources>
</prism:PrismApplication>
  1. 删除默认配置的MainWindow
  2. 在根目录下新建两个文件夹,分别为ViewsViewModels,注意一定需要是这个名字。而且对于新建的视图和绑定的ViewModel需要一一对应,截图如下:
    prism-view-viewmodel.png
  3. 对于Prism,如果需要ViewViewModel自动映射的话,需要在你的XAML下加上一句代码prism:ViewModelLocator.AutoWireViewModel="True"(如果没有prism则需要加上 xmlns:prism="http://prismlibrary.com/"),如下图:
    prism-mainview.png
  4. ViewModel中可以如下使用依赖注入:
private IRegionManager iRegionManager;
private IEventAggregator iEventAggregator;
public MenuViewModel()
{

}
public MenuViewModel(IRegionManager iRegionManager, IEventAggregator iEventAggregator)
{

    this.iRegionManager = iRegionManager;
    this.iEventAggregator = iEventAggregator;
}
  1. ViewModel可以如下使用依赖属性和Command。(需要继承BindableBase)
   public partial class ToolsViewModel : BindableBase
   {
       private IRegionManager iRegionManager;
       private IEventAggregator iEventAggregator;
       public ToolsViewModel()
       {

       }
       public ToolsViewModel(IRegionManager iRegionManager, IEventAggregator iEventAggregator)
       {
           this.iRegionManager = iRegionManager;
           this.iEventAggregator = iEventAggregator;
       }
       private int _id;
       public int ID
       {
           get => _id;
           set => SetProperty(ref _id, value);
       }

       private DelegateCommand<string> _addCommand;
       public DelegateCommand<string> AddCommand => _addCommand = _addCommand ?? new DelegateCommand<string>((id) => Console.WriteLine(id));

   }
  1. 如果你想在任意地方使用DI,你可以先在App.xaml.cs中增加:
 public partial class App : PrismApplication
 {
     private static IUnityContainer _container;
     public static new IUnityContainer Container => _container;
     protected override Window CreateShell()
     {
         return Container.Resolve<Main>();

     }

     protected override void RegisterTypes(IContainerRegistry containerRegistry)
     {
         var iRegionManager = containerRegistry.GetContainer().Resolve<IRegionManager>();
         if (iRegionManager == null)
             return;
         iRegionManager.RegisterViewWithRegion(RegionName.MenuRegion, typeof(Menu));
         iRegionManager.RegisterViewWithRegion(RegionName.MainRegion, typeof(Tools));

         _container = containerRegistry.GetContainer();

     }
 }
 //可以在别的地方直接使用
 T obj= App.Container.Resolve<T>()
  1. 对于事件订阅发布器IEventAggregator,主要稍微了解委托的就很简单。需要继承PubSubEvent
public class AddToolEvent : PubSubEvent
{

}
public class DeleteToolEvent : PubSubEvent<object>
{

}
//在任意地方发布和订阅
this.iEventAggregator.GetEvent<AddToolEvent>().Subscribe(() => Console.WriteLine("AddToolEvent"));
this.iEventAggregator.GetEvent<AddToolEvent>().Publish();
this.iEventAggregator.GetEvent<DeleteToolEvent>().Subscribe((obj) => Console.WriteLine($"DeleteToolEvent{obj}"));
this.iEventAggregator.GetEvent<DeleteToolEvent>().Publish(new object());

Tips

  • 当前只是使用了差不多最基本的功能,其中最主要的有模块的使用还有IRegionManagerIEventAggregator
  • 准备着手重写原来的一部分代码了。

0
博主关闭了所有页面的评论