wpf 从代码中关闭 Material Design DialogHost

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50652084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:10:41  来源:igfitidea点击:

Closing Material Design DialogHost from Code

c#wpfxamlmaterial-design-in-xaml

提问by user3342256

I'm trying to find a way to initiate the close of an active DialogHost from code, but have been unable to find the right syntax. I think the main challenge is accessing the DialogHost from a class outside of the MainWindow code behind.

我试图找到一种方法来从代码启动关闭活动的 DialogHost,但一直无法找到正确的语法。我认为主要的挑战是从背后的 MainWindow 代码之外的类访问 DialogHost。

The entirety of my XAML:

我的 XAML 的全部内容:

<Window x:Class="MaterialDesignTest.MainWindow"
    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:MaterialDesignTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    TextElement.FontWeight="Regular"
    TextElement.FontSize="13"
    TextOptions.TextFormattingMode="Ideal" 
    TextOptions.TextRenderingMode="Auto"        
    Background="{DynamicResource MaterialDesignPaper}"
    FontFamily="{DynamicResource MaterialDesignFont}">

<materialDesign:DialogHost Identifier="RootDialog" Loaded="DialogHost_Loaded">
    <Grid>
        <StackPanel>
            <Button Width="100" x:Name="SearchRestore" Margin="0 150 0 0" Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}" materialDesign:DialogHost.DialogClosingAttached="SearchRestore_OnDialogClosing" 
                Content="Restore" Click="SearchRestore_Click">
                <Button.CommandParameter>
                        <StackPanel Margin="10">
                            <TextBlock Text="Restoring..."/>
                        <Button Name="CircleButton" Margin="0 50 0 0" Style="{StaticResource MaterialDesignFloatingActionButton}"
                        Command="{Binding SaveComand}" IsHitTestVisible="False"
                        materialDesign:ButtonProgressAssist.IsIndicatorVisible="{Binding IsSaving}"
                        materialDesign:ButtonProgressAssist.Value="{Binding SaveProgressButton}">
                            <materialDesign:PackIcon Height="24" Width="24" Foreground="White">
                                <materialDesign:PackIcon.Style>
                                    <Style TargetType="materialDesign:PackIcon" BasedOn="{StaticResource {x:Type materialDesign:PackIcon}}">
                                        <Setter Property="Kind" Value="CloudSync" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsSaveComplete}" Value="True">
                                                <Setter Property="Kind" Value="Check" />
                                                <DataTrigger.EnterActions>
                                                    <BeginStoryboard>
                                                        <Storyboard>
                                                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.8" />
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </DataTrigger.EnterActions>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </materialDesign:PackIcon.Style>
                            </materialDesign:PackIcon>
                        </Button>
                    </StackPanel>
                </Button.CommandParameter>
            </Button>
        </StackPanel>
    </Grid>
</materialDesign:DialogHost>

And the code from which I need to initiate the close (this is in a view model, not the MainWindow code behind):

以及我需要从中启动关闭的代码(这是在视图模型中,而不是后面的 MainWindow 代码):

     private async void progressTimerTick(object sender, EventArgs e)
    {

        if (progress < 100 && cycles < 2)
        {
            if (progress == 99)
            {
                cycles++;
                progress = 0;
            }

            IsSaveComplete = false;
            IsSaving = true;
            progress++;
            SaveProgressButton = progress;
        }
        else
        {
            IsSaveComplete = true;
            IsSaving = false;
            progressTimer.Enabled = false;
            SaveProgressButton = 0;

            await PutTaskDelay();

            //Close dialog here
        }
    }

回答by Talha Talip A??kg?z

There are 3 ways to close a dialog in MDIX:

在 MDIX 中有 3 种关闭对话框的方法:

var dialogResult = await DialogHost.Show(myDialogControl, (sender, args) =>
{
    args.Session.Close(false);
});

or

或者

DialogHost.CloseDialogCommand.Execute(null,null);

or

或者

DialogHostInstance.IsOpen = false;