热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

prismRegionContext(区域上下文)

1按照《prism项目搭建》搭建prism项目2新建用户控件库ModuleA,并为其创建Views,ViewModels,Model目录

1 按照《prism项目搭建》搭建prism项目

2 新建用户控件库ModuleA,并为其创建Views,ViewModels,Model目录

3 在Model里面新建类

using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA.Model
{public class Person:BindableBase{private string _firstName;public string FirstName{get { return _firstName; }set{_firstName = value;SetProperty(ref _firstName, value);}}private string _lastName;public string LastName{get { return _lastName; }set{_lastName = value;SetProperty(ref _lastName, value);}}private int _age;public int Age{get { return _age; }set{_age = value;SetProperty(ref _age, value);}}public override string ToString(){return String.Format("{0}, {1}", LastName, FirstName);}}
}

4 在Views里面创建PersonListView,PersonDetailView用户控件,在PersonListView定义Region的同时,将RegionContext绑定到ListBox的SelectedItem属性





5 在ViewModels目录里面新建PersonListViewModel,PersonDetailViewModel

using ModuleA.Model;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA.ViewModels
{public class PersonListViewModel:BindableBase{private ObservableCollection _lstPerson &#61; new ObservableCollection();public ObservableCollection LstPerson{get { return _lstPerson; }set { SetProperty(ref _lstPerson, value); }}public PersonListViewModel(){for (int i &#61; 0; i <10; i&#43;&#43;){_lstPerson.Add(new Person(){FirstName &#61; String.Format("First {0}", i),LastName &#61; String.Format("Last {0}", i),Age &#61; i});}}}
}

using ModuleA.Model;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA.ViewModels
{public class PersonDetailViewModel:BindableBase{private Person _selectedPerson &#61; new Person();public Person SelectedPerson{get { return _selectedPerson; }set { SetProperty(ref _selectedPerson, value); }}}
}

7 在ModuleA中新建类ModuleAEntity&#xff0c;并将View注册到对应的Region

using ModuleA.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ModuleA
{public class ModuleAEntity : IModule{public void OnInitialized(IContainerProvider containerProvider){var regionManager &#61; containerProvider.Resolve();regionManager.RegisterViewWithRegion("ContentRegion", typeof(PersonListView));regionManager.RegisterViewWithRegion("DetailRegion", typeof(PersonDetailView));}public void RegisterTypes(IContainerRegistry containerRegistry){}}
}

8 在App里面添加Module

protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog){moduleCatalog.AddModule();}

9 此时运行发现选定LstBox的项时&#xff0c;DetailView没有变化

10 此时需要修改PersonDetailView代码&#xff0c;监控DataContext的变化

using ModuleA.Model;
using ModuleA.ViewModels;
using Prism.Common;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace ModuleA.Views
{///

/// PersonDetailView.xaml 的交互逻辑/// public partial class PersonDetailView : UserControl{public PersonDetailView(){InitializeComponent();RegionContext.GetObservableContext(this).PropertyChanged &#43;&#61; PersonDetailView_PropertyChanged;}private void PersonDetailView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e){var context &#61; (ObservableObject)sender;var selectedPerson &#61; (Person)context.Value;(DataContext as PersonDetailViewModel).SelectedPerson &#61; selectedPerson;}}
}

11 运行 

 


推荐阅读
author-avatar
依然yang梓枫东_811
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有