其实逻辑很简单,在前端的cs中注册子窗体,在viewmodel中resolve并show出来。这需要通过一个子窗体注册类来进行操作。这种弹出子窗体的方式,我们用到了mef框架。将导出的子窗体注册类分别导入到WindowBase和ViewModelBase,且他们是同一个实例。
我们定义一个子窗体操作类。
<pre>
[Export]
public class UIShowManager
{
private readonly Hashtable _registeredWindowsEx = new Hashtable();
public void Register(string key, Type t)
{
_registeredWindowsEx.Add(key, t);
}
public void Register<T>(string key)
{
_registeredWindowsEx.Add(key, typeof(T));
}
public void Show(string key, object vm)
{
Window win = CreateWindow(key, vm );
if (win != null)
{
win.Show();
}
}
public void ShowDialog(string key, object vm)
{
Window win = CreateWindow(key, vm);
if (win != null)
{
win.ShowDialog();
}
}
private Window CreateWindow(string key, object vm)
{
if (!_registeredWindowsEx.ContainsKey(key))
throw new Exception("不存在这个key");
object obj = _registeredWindowsEx[key];
Type windowType;
if (obj is Type)
{
windowType = obj as Type;
}
else if (obj is Window)
{
windowType = typeof(object);
}
else
{
throw new Exception("这个key对应的元素不是窗体!");
}
var window=(Window) Activator.CreateInstance(windowType);
if (vm != null)
{
window.DataContext = vm as ViewModelBase;
}
return window;
}
}
</pre>
我们将这个类导出。
因为用到了Mef,我们增加一个MefHelper。
<pre>
public class MefHelper
{
private static MefHelper _singleton=new MefHelper ();
private CompositionContainer container;
public MefHelper()
{
//AggregateCatalog用来合并多个catalog
var catalog = new AggregateCatalog();
AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetEntryAssembly());
DirectoryCatalog directoryCatalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory);
catalog.Catalogs.Add(assemblyCatalog);
catalog.Catalogs.Add(directoryCatalog);
container = new CompositionContainer(catalog);
}
public static MefHelper Instance
{
get { return _singleton; }
}
public void Compose(object obj)
{
container.ComposeParts(obj);
}
public void ComposeOnce(object obj)
{
container.SatisfyImportsOnce(obj);
}
}
</pre>
然后我们建个WindowBase,没有WindowBase,也可以直接写在View中。并导入UIShowManager。
<pre>
public class WindowBase: Window
{
[Import]
public UIShowManager _UIShowManager = null;
}
</pre>
我们在建一个ViewBase,并导入UIShowManager
<pre>
public partial class ViewModelBase : INotifyPropertyChanged
{
[Import]
private UIShowManager _UIShowManager = null;
public ViewModelBase()
{
}
public void Show(string key,object vm)
{
if (_UIShowManager == null)
MefHelper.Instance.Compose(this);
_UIShowManager.Show(key,vm);
}
public void ShowDialog(string key, object vm)
{
if (_UIShowManager == null)
MefHelper.Instance.Compose(this);
_UIShowManager.ShowDialog(key, vm);
}
#region 【实现接口】
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
</pre>
ViewModelBase中有两个方法,Show和ShowDialog,用来显示子窗体。
好了,基本的框架已经搭好了,下面我们进行测试。
我们按照MVVM的方式,添加一些窗体和VM,如下图所示。
<pre>
public partial class MainWindow :WindowBase
{
public MainWindow()
{
InitializeComponent();
if (_UIShowManager == null)
MefHelper.Instance.Compose(this);
this.DataContext = new VM();
_UIShowManager.Register("aaa",typeof(ChildWindow));
}
}
</pre>
MainWindow继承WindowBase。在此界面里注册需要显示的子窗体。注册方法是_UIShowManager.Register("aaa",typeof(ChildWindow));
<pre>
public class VM : ViewModelBase
{
public VM()
{
ShowChildCommand = new DelegateCommand(ExecuteShowChild);
}
public DelegateCommand ShowChildCommand { get; set; }
private string _ChildCaption = "这是子窗体";
public string ChildCaption
{
get { return _ChildCaption; }
set { _ChildCaption = value; RaisePropertyChanged("ChildCaption"); }
}
private void ExecuteShowChild()
{
ShowDialog("aaa",this);
}
}
</pre>
vm继承ViewModelBase,弹出子窗体,调用父类的ShowDialog方法。
这样我们在主窗体中点击按钮,就可以弹窗了。