嗯。。最近回家去了,2个月没写过代码了,面试只能吹牛,基础都忘了,今天回顾一下,分享一篇经过附加属性去处理窗体弹出的状况。面试
或许老司机已经想到了,经过设置附加属性值,值变动的回调函数去处理窗体弹出,是的,很简单,想法的问题。函数
public static readonly DependencyProperty IsModalProperty = DependencyProperty.RegisterAttached("IsModal", typeof(bool), typeof(WindowHelper), new PropertyMetadata(true)); public static readonly DependencyProperty OpenWindowTypeProperty = DependencyProperty.RegisterAttached("OpenWindowType", typeof(Type), typeof(WindowHelper), new PropertyMetadata(null, OnOpenWindowTypeChanged)); public static readonly DependencyProperty ParameterProperty = DependencyProperty.RegisterAttached("Parameter", typeof(object), typeof(WindowHelper), new PropertyMetadata(null));
三个附加属性,是否模态窗口,窗口类型,传递到窗口的参数,事实上其实仍是经过反射处理的。ui
private static void OnOpenWindowTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
这是OpenWindowType属性的变动回调函数spa
var type = GetOpenWindowType(d); if (type == null && type != typeof(Window)) { return; } Window window = Activator.CreateInstance(type) as Window; if (window == null) { return; } if (GetParameter(d) != null) { window.Tag = GetParameter(d); } var isModel = GetIsModal(d); window.Closed += (win, closeArgs) => { window = null; }; if (isModel) { window.ShowDialog(); } else { window.Show(); }
是吧,函数实现仍是很简单的,看着明白了,那还少一点东西,什么呢? 怎么触发这个变动函数!code
在说触发的问题的时候,怎么想一想怎么使用它blog
attached:WindowHelper.IsModal="True" attached:WindowHelper.OpenWindowType="{x:Type local:Window1}"
是吧,添加引用而后设置属性继承
这个附加属性咱们添加到哪里呢?固然是哪里用加哪里了。 因此多是点击button弹窗,也多是menuitem接口
因此,咱们要添加下面这段代码,在属性变动函数以前事件
dynamic control = null; switch (d.GetType().Name.ToString()) { case "Button": control = d as Button; break; case "Hyperlink": control = d as Hyperlink; break; case "MenuItem": control = d as MenuItem; break; default: return; }
var type = GetOpenWindowType(d); if (type == null && type != typeof(Window)) { return; } Window window = null; var clickEventHandler = new RoutedEventHandler((s, arg) => { if (window == null) { window = Activator.CreateInstance(type) as Window; if (window == null) { return; } } if (GetParameter(d) != null) { window.Tag = GetParameter(d); } var isModel = GetIsModal(d); window.Closed += (win, closeArgs) => { window = null; }; if (isModel) { window.ShowDialog(); } else { window.Show(); } }); control.Click += clickEventHandler;
事实上,这个属性变动只会有一次,就是初始化的时候,因此咱们在初始化的时候给按钮注册了事件,每次点击的时候去弹出窗体,作到这一步其实其余的就很好处理了,好比给vm传递参数,是否是用window.DataContext as VM,而后传递就能够了?固然更好的方式是写一个公共的接口,让VM去继承作处理。好比窗口关闭后须要调用某个函数作一些功能,是否是好实现多了,再加一个ICommand类型的附加属性不就能够了。回调函数
分享就到这里,有更好的方案的同窗能够加页面下方的群,欢迎讨论