MahAppsDialogの表示

2020-06-07

MainView.xaml

<Controls:MetroWindow 
        x:Class="WpfApp12.View.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp12.View"
        mc:Ignorable="d"
        
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:ei="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
        xmlns:vm="clr-namespace:WpfApp12.ViewModel"
        
        xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
        Dialog:DialogParticipation.Register="{Binding}"
        
        TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        TextElement.FontWeight="Regular"
        TextElement.FontSize="13"
        TextOptions.TextFormattingMode="Ideal" 
        TextOptions.TextRenderingMode="Auto"        
        Background="{DynamicResource MaterialDesignPaper}"
        FontFamily="{DynamicResource MaterialDesignFont}"
        ResizeMode="CanResizeWithGrip"
        WindowStartupLocation="CenterScreen"
        GlowBrush="{DynamicResource AccentColorBrush}"
        WindowTransitionsEnabled="False"
        TitleCharacterCasing="Normal"
        ShowIconOnTitleBar="True"
        
        Title="MainView" Height="450" Width="800">

    <Window.DataContext>
        <vm:MainViewModel>
            <vm:MainViewModel.MahAppsDialogCoordinator>
                <Dialog:DialogCoordinator/>
            </vm:MainViewModel.MahAppsDialogCoordinator>
        </vm:MainViewModel>
    </Window.DataContext>

    <Grid>

        <Button Content="MessageBox" Margin="10,10,657,359" Height="Auto">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="ShowMessageBox" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
        
        <Button Content="Progress" Margin="155,10,512,359" Height="Auto">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="ShowProgress" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

    </Grid>
</Controls:MetroWindow>

MainView.xaml.cs

namespace WpfApp12.View
{
    /// <summary>
    /// MainView.xaml の相互作用ロジック
    /// </summary>
    public partial class MainView : MahApps.Metro.Controls.MetroWindow
    {
        public MainView()
        {
            InitializeComponent();
        }
    }
}

MainViewModel.cs

using System;
using System.Threading.Tasks;
using MahApps.Metro.Controls.Dialogs;

namespace WpfApp12.ViewModel
{
    public class MainViewModel:Livet.ViewModel
    {
        public IDialogCoordinator MahAppsDialogCoordinator { get; set; }

        /// <summary>
        /// メッセージボックスの表示
        /// </summary>
        public async void ShowMessageBox()
        {
            await MahAppsDialogCoordinator.ShowMessageAsync(this, "ShowMessageBox", "ShowMessageBoxText");
        }

        /// <summary>
        /// プログレスダイアログの表示
        /// </summary>
        public async void ShowProgress()
        {
            MetroDialogSettings objDialogSetting = new MetroDialogSettings()
            {
                NegativeButtonText = "閉じる"
            };

            ProgressDialogController objController = await MahAppsDialogCoordinator.ShowProgressAsync(this, "ShowProgress", "ShowProgressTest", false, objDialogSetting);
            objController.Canceled += async (object sender, EventArgs e) =>
            {
                await objController.CloseAsync();
            };

            objController.Minimum = 1;
            objController.Maximum = 10;

            await Task.Run(() =>
            {
                for (int i = 1; i <= 10; i++)
                {
                    System.Threading.Thread.Sleep(100);

                    objController.SetProgress(i);
                    objController.SetMessage("Progress:" + i.ToString());

                    if (i == 10)
                        objController.SetCancelable(true);
                }
            });
        }
    }
}