コード例 #1
0
        public async Task ShowAsync(FrameworkElement view, string title = "", object parameter = null, DialogParameters dialogParameters = null, bool AlwaysActiveViewOnOpen = true, bool AlwaysDeactiveViewOnClose = true)
        {
            var customDialog = new CustomDialog();

            if (dialogParameters == null)
            {
                dialogParameters = new DialogParameters();
            }

            FillParameters(customDialog, dialogParameters);


            if (view == null)
            {
                throw new Exception("View not found");
            }

            customDialog.Child = view;



            customDialog.Title = title;
            var modal = view.DataContext as IModalDialog;

            if (modal != null)
            {
                customDialog.Title = string.IsNullOrEmpty(title) ? modal.Title : title;
                if (modal.AcceptCommand != null)
                {
                    customDialog.AcceptCommandText = modal.AcceptLabel;
                    customDialog.AcceptCommand     = modal.AcceptCommand;
                }



                if (modal.CancelCloseCommand != null)
                {
                    customDialog.CancelCommandText = modal.CancelLabel;

                    customDialog.CancelCommand = modal.CancelCloseCommand;
                }
            }


            modal = null;
            var activatable = view.DataContext as INavigationAware;

            if (activatable != null)
            {
                activatable.OnNavigatedTo(new NavigatedToEventArgs()
                {
                    NavigationMode = Windows.UI.Xaml.Navigation.NavigationMode.New,
                    Parameter      = parameter
                },
                                          null
                                          );
            }


            var deactivator = view.DataContext as INavigationAware;

            if (deactivator != null)
            {
                if (Window.Current != null)
                {
                    var control = Window.Current.Content as UserControl;
                    if (control == null)
                    {
                        return;
                    }
                    var grid = control.Content as Grid;

                    if (grid == null)
                    {
                        Debug.WriteLine("Control must have a Grid as main container");
                        return;
                    }
                    var popups = grid.Children.OfType <Popup>();
                    if (popups != null && popups.Any())
                    {
                        var modalPopup = popups.FirstOrDefault(x => x.Name == "ModalPopup");
                    }
                    else
                    {
                        var modalPopup = createModalPopup(customDialog, grid, dialogParameters);


                        grid.Children.Add(modalPopup);

                        modalPopup.IsOpen = true;
                        var closedEvent = Observable.FromEventPattern(modalPopup, "Closed");

                        IDisposable subscriber = null;
                        subscriber = closedEvent.Subscribe(x =>
                        {
                            var container = customDialog.FindName("Container") as ContentControl;
                            if (container != null)
                            {
                                container.Content = null;
                            }

                            deactivator.OnNavigatingFrom(new NavigatingFromEventArgs()
                            {
                                NavigationMode = Windows.UI.Xaml.Navigation.NavigationMode.Back,
                            }, null, false);

                            DialogClosed?.Invoke(this, new DialogClosedEventArgs(Result, DialogCloseParameter));


                            deactivator = null;



                            container    = null;
                            customDialog = null;
                            if (view != null)
                            {
                                ((FrameworkElement)view).DataContext = null;
                                view = null;
                            }
                            GC.Collect();



                            subscriber.Dispose();
                            closedEvent = null;
                        });
                    }
                }
            }
        }