WPF实战案例-MVVM模式下用附加属性在Xaml中弹出窗体

嗯。。最近回家去了,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));

三个附加属性,是否模态窗口,窗口类型,传递到窗口的参数,事实上其实还是通过反射处理的。

 

 private static void OnOpenWindowTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

这是OpenWindowType属性的变更回调函数

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();
                 }

 

是吧,函数实现还是很简单的,看着明白了,那还少一点东西,什么呢? 怎么触发这个变更函数!

在说触发的问题的时候,怎么想想怎么使用它

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类型的附加属性不就可以了。

分享就到这里,有更好的方案的同学可以加页面下方的群,欢迎讨论

 

转载于:https://www.cnblogs.com/BeiJing-Net-DaiDai/p/11505733.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值