Exemple #1
0
    public override Task <MessageBoxResult> ShowMessageBoxAsync(object context, string message, string title, string trueText, BorderStyle?trueStyle, string falseText, BorderStyle?falseStyle, MessageBoxButton button, MessageBoxImage icon)
    {
        if (GetWindow(context) is MetroWindow mw)
        {
            Task <MessageBoxResult> showCore()
            {
                if (DialogParticipation.GetRegister(mw) != null)
                {
                    switch (button)
                    {
                    case MessageBoxButton.YesNo:
                        return(mw.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings
                        {
                            AffirmativeButtonText = !string.IsNullOrEmpty(trueText) ? trueText : "はい",
                            NegativeButtonText = !string.IsNullOrEmpty(falseText) ? falseText : "いいえ",
                            OwnerCanCloseWithDialog = true
                        }).ContinueWith(t => t.Status != TaskStatus.RanToCompletion ? MessageBoxResult.None : t.Result == MessageDialogResult.Affirmative ? MessageBoxResult.Yes : MessageBoxResult.No));

                    case MessageBoxButton.OK:
                        return(mw.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, new MetroDialogSettings
                        {
                            AffirmativeButtonText = !string.IsNullOrEmpty(trueText) ? trueText : "OK",
                            OwnerCanCloseWithDialog = true
                        }).ContinueWith(t => t.Status != TaskStatus.RanToCompletion ? MessageBoxResult.None : MessageBoxResult.OK));
                    }
                }

                return(base.ShowMessageBoxAsync(context, message, title, trueText, trueStyle, falseText, falseStyle, button, icon));
            }

            if (mw.Dispatcher.Thread != Thread.CurrentThread)
            {
                var tcs = new TaskCompletionSource <MessageBoxResult>();
                InvokeAsync(context, () => showCore().ContinueWith(t =>
                {
                    if (t.Status == TaskStatus.RanToCompletion)
                    {
                        tcs.TrySetResult(t.Result);
                    }
                    else if (t.Exception != null)
                    {
                        tcs.TrySetException(t.Exception);
                    }
                    else
                    {
                        tcs.TrySetCanceled();
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext()));

                return(tcs.Task);
            }
            return(showCore());
        }

        return(base.ShowMessageBoxAsync(context, message, title, trueText, trueStyle, falseText, falseStyle, button, icon));
    }
Exemple #2
0
    protected override Task ShowModalAsync(object context, FrameworkElement frameworkElement)
    {
        if (GetWindow(context) is MetroWindow mw)
        {
            Task showCore()
            {
                if (DialogParticipation.GetRegister(mw) != null &&
                    !(frameworkElement is Window))
                {
                    ConfigureViewModel(frameworkElement.DataContext);

                    var tcs = new TaskCompletionSource <object>();

                    if (frameworkElement is BaseMetroDialog d)
                    {
                        d.Unloaded += (s, e) => tcs.TrySetResult(null);
                        mw.ShowMetroDialogAsync(d);
                    }
                    else
                    {
                        var gd = new GenericMetroDialog()
                        {
                            DataContext = frameworkElement.DataContext,
                            Content     = frameworkElement,
                        };
                        gd.Unloaded += (s, e) => tcs.TrySetResult(null);
                        mw.ShowMetroDialogAsync(gd);
                    }
                    return(tcs.Task);
                }
                return(base.ShowModalAsync(context, frameworkElement));
            }

            if (mw.Dispatcher.Thread != Thread.CurrentThread)
            {
                return(InvokeAsync(context, showCore));
            }
            return(showCore());
        }
        return(base.ShowModalAsync(context, frameworkElement));
    }
Exemple #3
0
    protected override bool CloseModal(object context, object viewModel)
    {
        var app = Application.Current;

        if (app != null)
        {
            foreach (var mw in app.Windows.OfType <MetroWindow>().ToList())
            {
                if (DialogParticipation.GetRegister(mw) != null)
                {
                    var dc = mw.FindChild <Panel>("PART_MetroActiveDialogContainer");
                    if (dc != null)
                    {
                        foreach (var d in dc.Children.OfType <BaseMetroDialog>().Where(e => e.DataContext == viewModel).ToList())
                        {
                            mw.HideMetroDialogAsync(d).GetHashCode();
                        }
                    }
                }
            }
        }
        return(base.CloseModal(context, viewModel));
    }