zoukankan      html  css  js  c++  java
  • Prism.WPF -- Prism框架使用(上)

    本文参考Prism官方示例

    创建Prism项目

    1. 将App.xaml中的WPF标准Application替换为PrismApplication,移除StartupUri属性;
    2. 将App.xaml.cs中的基类改为PrismApplication;
    3. 必须实现PrismApplication中的两个抽象方法:RegisterTypes、CreateShell;
    4. RegisterTypes用来注册类型;
    5. CreateShell用来创建程序主窗口。

    Region使用

    1. 在view xaml文件中使用prism:RegionManager.RegionName="SomeRegion"标记region;
    2. 创建自定义RegionAdapter类,继承自RegionAdapterBase,override Adapt和CreateRegion方法;
    3. 在App.xaml.cs中通过override ConfigureRegionAdapterMappings方法注册自定义RegionAdapter,示例如下:
    protected override void ConfigureRegionAdapterMappings(
        RegionAdapterMappings regionAdapterMappings)
    {
        base.ConfigureRegionAdapterMappings(regionAdapterMappings);
        regionAdapterMappings.RegisterMapping(
            typeof(StackPanel), Container.Resolve<StackPanelRegionAdapter>());
    }
    

    View注入Region

    有两种方法,第一种称为View Discovery,该方法适用于当region加载时就把视图注入到region场景;另外一种方法称为View Injection,该方法适用于当激发某一事件后view注入到region场景。

    View Discovery

    通过如下方法实现:

    regionManager.RegisterViewWithRegion("ContentRegion", typeof(ViewA));
    
    View Injection

    通过如下方法实现,并可通过IRegion的Activate与Deactivate接口实现view的使能:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var view = _container.Resolve<ViewA>();
        IRegion region = _regionManager.Regions["ContentRegion"];
        region.Add(view);
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //activate view a
        _region.Activate(_viewA);
    }
    
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        //deactivate view a
        _region.Deactivate(_viewA);
    }
    

    添加视图模块

    1. 添加项目,在项目中添加继承自IModule的类,实现OnInitialized与RegisterTypes方法。一般在OnInitialized中添加View Discovery代码以将该模块的相关View注入到Region中;
    2. 在程序中添加模块。添加模块的方式很多,本文仅介绍使用代码的方式添加,方法如下:
    // App.xaml.cs
    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<ModuleA.ModuleAModule>();
    }
    

    匹配ViewModels

    如果不修改命名规则,在xaml中为窗口/控件添加如下属性将自动匹配viewmodel:

    prism:ViewModelLocator.AutoWireViewModel="True"
    

    可以通过如下方法修改默认的viewmodel匹配规则,仍需在xaml中配置AutoWireViewModel:

    // App.xaml.cs
    protected override void ConfigureViewModelLocator()
    {
        base.ConfigureViewModelLocator();
    
        ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(
            (viewType) =>
        {
            var viewName = viewType.FullName;
            var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
            var viewModelName = $"{viewName}ViewModel, {viewAssemblyName}";
            return Type.GetType(viewModelName);
        });
    }
    

    若不想修改匹配规则,且viewmodel名称不匹配默认规则,可通过如下方式匹配,仍需在xaml中配置AutoWireViewModel:

    protected override void ConfigureViewModelLocator()
    {
        base.ConfigureViewModelLocator();
        
        ViewModelLocationProvider.Register<MainWindow, CustomViewModel>();
    }
    
    转载请注明出处,欢迎交流。
  • 相关阅读:
    ios设备new Date('2019-07-26 11:00:00')报错
    vue图片压缩(不失真)
    ios微信端网页点击右上角复制链接或在浏览器打开,得不到当前页地址(动态路由)
    ios打开网页,输入框获取焦点键盘弹起,关闭键盘,界面下方空白不回弹
    vue-cli打包后vendor.js文件太大怎么办
    ios点击有300毫秒延迟,输入框必须重压或长按才能获取焦点唤起软键盘
    ios微信端上下滑动页面,若触摸的是input区域,页面内不滚动,整个页面被拖动了
    vue-cli打包优化之分析工具webpack-bundle-analyzer
    vue引入fastclick设置输入框type="number"报错Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.的解决办法
    js/vue图片压缩
  • 原文地址:https://www.cnblogs.com/louzixl/p/14414384.html
Copyright © 2011-2022 走看看