原味丝袜文章,爱国主义图片,刺刀1937txt
主要是通过一个windowmanager管理类,在window后台代码中通过windowmanager注册需要弹出的窗体类型,在viewmodel通过windowmanager的show方法,显示出来。
windowmanager代码如下:
public static class windowmanager { private static hashtable _registerwindow = new hashtable(); public static void regiter<t>(string key) { _registerwindow.add(key, typeof(t)); } public static void regiter(string key, type t) { if (!_registerwindow.containskey(key)) _registerwindow.add(key, t); } public static void remove(string key) { if (_registerwindow.containskey(key)) _registerwindow.remove(key); } public static void showdialog(string key, object vm) { if (!_registerwindow.containskey(key)) { throw (new exception("没有注册此键!")); } var win = (window)activator.createinstance((type)_registerwindow[key]); win.datacontext = vm; win.showdialog(); } }
做一个扩展方法,将子窗体注册方法扩展到window类型的对象上。
public static class windowext { public static void register(this window win, string key) { windowmanager.regiter(key, win.gettype()); } public static void register(this window win,string key,type t) { windowmanager.regiter(key,t); } public static void register<t>(this window win, string key) { windowmanager.regiter<t>(key); } }
添加一个viewmodelbase,并在类中添加showdialog方法,这样所有继承的viewmodel都有这个方法
public class viewmodelbase { public void showdialog(string key,object vm) { windowmanager.showdialog(key,vm); } public void showmessage(string mes,string title="",messageboxbutton buttons= messageboxbutton.ok) { messagebox.show(mes,title,buttons); } }
添加一个窗体,并注册子窗体, this.register<window1>("window1");
public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.datacontext = new mainwindowviewmodel(); this.register<window1>("window1"); } }
添加viewmodel,继承自viewmodelbase,并在对应的命令中弹出子窗体window1
public class mainwindowviewmodel:viewmodelbase { public mainwindowviewmodel() { btncommand = new delegatecommand(executebtn); } public delegatecommand btncommand { get; set; } private void executebtn() { showdialog("window1",this); } }
这样子窗体就弹出来了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论