WPF pattern -MVVM the real case with additional attributes in a pop-up form in Xaml

Ok. . Recently went home, two months have not written the code, the interview can only boast basis forgot to review it today, was shared by an additional property to deal with the pop-up window.

Perhaps the old drivers have thought, by setting the attached property value, value change callback function to handle pop-up window, yes, quite simply, the idea of ​​the problem.

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

Three additional properties, whether modal window, window type, parameters passed to the window, but it is still in fact treated by reflection.

 

 private static void OnOpenWindowTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

This is a change callback OpenWindowType property

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

 

Is not it, to realize the function is very simple, looking to understand, it is also a little less stuff, what is it? How to change trigger this function!

In the said problems triggered when, how to think about how to use it

attached:WindowHelper.IsModal="True"
                                    attached:WindowHelper.OpenWindowType="{x:Type local:Window1}"

Is not it, and then set the properties to add a reference

This additional property where we add to it? Of course, is where where with added. Click the button so may be pop, it may be menuitem

So, we have to add the following code before the function attribute change

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;

In fact, this property will only have to change once, is to initialize the time, so we registered the event to the button at initialization time, every time to click pop-up form, do this step is actually another very good deal such as vm to pass parameters, is not used window.DataContext as VM, and then passed on it? Of course, a better way is to write a common interface that allows VM to inherit done deal. For example, after closing the window needs to call a function to do some function, it is not a good achieve more, plus a ICommand type of additional property is not on it.

Share on here, there are better options students can be added to the bottom of the page group, welcomed the discussion

 

Guess you like

Origin www.cnblogs.com/BeiJing-Net-DaiDai/p/11505733.html