当前位置: 首页 > 科技观察

C#基于.NETCore3.1的开源项目,帮助你彻底了解WPF框架Prism

时间:2023-03-14 19:17:15 科技观察

概述本项目演示了如何在WPF中使用各种Prism功能的示例。如果您刚刚开始使用Prism,建议您从第一个示例开始,按照列表的顺序进行操作。每个示例都建立在前一个示例的概念之上。本项目平台框架:.NETCore3.1Prism版本:8.0.0.1909注意:这些项目都是同一个解决方案下的,需要依次打开运行。可以选择项目-》右键-》设置启动项目,然后运行:DirectoryIntroductionTopicDescriptionBootstrapperandtheShellCreateabasicbootstrapandshellRegionsCreatearegionCustomRegionAdapterCreateacustomregionadapterfortheStackPanelViewDiscoveryUseViewDiscoveryAutomaticallyInjectViewsViewInjectionUseViewInjectionManuallyAddandRemoveViewsViewActivation/DeactivationManualActivationandDisabletheviewModuleswithApp.config加载模块与应用程序。ConfigurationFileModuleswithCodeModuleswithDirectoryLoadingModulesfromDirectoryModulesloadedmanuallyUsingIModuleManagerViewModelLocatorUsingViewModelLocatorViewModelLocator-ChangeConventionChangingViewModelLocatorNamingConventionViewModelLocator-CustomRegistrationsManuallyregisteringViewModelsDelegateCommandforspecificviewsUsingDelegateCommandandDelegateCommandCompositeCommandsLearnhowtouseCompositeCommandstocallmultiplecommandsasasinglecommandIActiveAwareCommandsMakeyourcommandIActiveAwareonly调用活动命令事件聚合器使用IEventAggregatorEventAggregator-过滤事件订阅事件时过滤事件RegionContext使用RegionContext将数据传递到嵌套区域区域导航查看如何实现基本区域导航导航回调在导航完成时收到通知导航参与了解视图和视图模型与INavigationAware模型确认/取消导航使用IConfirmNavigationR用于确认或取消导航的eqest界面控制视图生命周期使用IRegionMemberLifetime自动从内存中删除视图NavigationJournal了解如何使用导航日志:参考Prsim.Unityonnuget。step2:修改App.xaml:设置bootstrap。publicpartialclassApp:Application{protectedoverridevoidOnStartup(StartupEventArgse){base.OnStartup(e);varbootstrapper=newBootstrapper();bootstrapper.Run();}}step3:在bootstrapper中设置启动项。使用Unity;使用Prism.Unity;使用BootstrapperShell.Views;使用System.Windows;使用Prism.Ioc;命名空间BootstrapperShell{classBootstrapper:PrismBootstrapper{}protectedoverridevoidRegisterTypes(IContainerRegistrycontainerRegistry){}}}step4:在MainWindow.xaml中显示一个字符串。②ViewInjection:视频注册MainWindow.xaml:通过ContentControl关联视图:添加视图MainWindow.xaml.cs:鼠标点击后通过IRegion接口注册视图:publicpartialclassMainWindow:Window{IContainerExtension_container;IRegionManager_regionManager;publicMainWindow(IContainerExtensioncontainer,IRegionManagerregionManager){InitializeComponent();_container=容器;_regionManager=区域管理器;}privatevoidButton_Click(objectsender,RoutedEventArgse){varview=_container.Resolve();IRegion区域=_regionManager.Regions["ContentRegion"];地区。添加(视图);}}③ActivationDeactivation:视图激活和停用MainWindow.xaml.cs:这里在window的body构造函数中注入了一个容器扩展接口和一个区域管理器接口,分别用于加载视图和注册区域。窗体的激活和去激活分别通过region的Activate和Deactivate方法实现。publicpartialclassMainWindow:Window{IContainerExtension_container;IRegionManager_regionManager;IRegion_region;查看A_viewA;查看B_viewB;publicMainWindow(IContainerExtensioncontainer,IRegionManagerregionManager){InitializeComponent();_container=容器;_regionManager=区域管理器;this.Loaded+=MainWindow_Loaded;}privatevoidMainWindow_Loaded(objectsender,RoutedEventArgse){_viewA=_container.Resolve();_viewB=_container.Resolve();_region=_regionManager.Regions["ContentRegion"];_region.Add(_viewA);_region.Add(_viewB);}privatevoidButton_Click(objectsender,RoutedEventArgse){//激活视图a_region.Activate(_viewA);}privatevoidButton_Click_1(objectsender,RoutedEventArgse){//停用视图a_region.Deactivate(_viewA);}privatevoidButton_Click_2(objectsender,RoutedEventArgse){//激活视图b_region.Activate(_viewB);}privatevoidButton_Click_3(objectsender,RoutedEventArgse){//停用视图b_region.Deactivate(_viewB);}}④UsingEventAggregator:事件发布订阅事件类定义:publicclassMessageSentEvent:PubSubEvent{}注册两个组件:ModuleA和ModuleBprotectedoverridevoidConfigureModuleCatalog(IModuleCatalogmoduleCatalog){moduleCatalog.AddModule();moduleCatalog.AddModule();}ModuleAModule中注册视图MessageView:publicclassModuleAModule:IModule{publicvoidOnInitialized(IContainerProvidercontainerProvider){varregionManager=containerProvider.Resolve();regionManager.RegisterViewWithRegion("LeftRegion",typeof(MessageView));}publicvoidRegisterTypes(IContainerRegistrycontainerRegistry){}}MessageView.xaml:视图中给button的美女捆绑命令:MessageViewModel.cs:在vm中,将接口绑定的命令委托给SendMessage,然后在方法SendMessage中发布消息:usingPrism.Commands;使用Prism.Events;使用Prism.Mvvm;使用UsingEventAggregator.Core;命名空间ModuleA.ViewModels{publicclassMessageViewModel:BindableBase{IEventAggregator_ea;privatestring_message="要发送的信息";publicstringMessage{get{return_message;}set{SetProperty(ref_message,value);}}publicDelegateCommandSendMessageCommand{get;私有集;}publicMessageViewModel(IEventAggregatorea){_ea=ea;SendMessageCommand=newDelegateCommand(SendMessage);}privatevoidSendMessage(){_ea.GetEvent().Publish(Message);}}}在MessageListViewModel中接收并显示收到的消息:publicclassMessageListViewModel:BindableBase{IEventAggregator_ea;私有ObservableCollection<字符串>_消息;公共ObservableCollection消息{get{return_messages;}set{SetProperty(ref_messages,value);}}publicMessageListViewModel(IEventAggregatorea){_ea=ea;消息=newObservableCollection();_ea.GetEvent().Subscribe(MessageReceived);}privatevoidMessageReceived(stringmessage){Messages.Add(message);}}以上是本开源项目的一些经典例子,其他的就不一一解释了