punker76 / MahApps.Metro.SimpleChildWindow

A simple child window for MahApps.Metro
MIT License
374 stars 55 forks source link

await ChildWindowAsync does not block main thread #40

Closed AlanWills closed 7 years ago

AlanWills commented 7 years ago
public async virtual void Execute(object parameter)
        {
            Debug.Assert(CanExecute(parameter));

            Part part = parameter as Part;

            ViewerWindow window = ViewerWindows.SelectPartTypeWindow;
            await (Application.Current.MainWindow as MetroWindow).ShowChildWindowAsync(window);

            if (window.DialogResult)
            {
                part.PartType = (window.ViewFrame.Content as SelectPartTypeView).SelectedPartTypeVM.SelectedPartType;
            }
            else
            {
                Debug.Fail("No part type selected, so reverting to SOLID");
                ViewerWindows.MessageWindow("Part Type was not selected so will default to Solid");
                part.PartType = PartType.Solid;
            }

            // Set the render colour based on the part type the user has (or has not!) chosen
            part.DefaultRenderColour = Part.RenderColours[part.PartType];
        }

In the following code I attempt to show a ChildWindow using 'ShowChildWindowAsync' and expect the await call to block until the window is closed. However, execution just continues on.

Have I used the API incorrectly? How do create a Modal ChildWindow which blocks execution until closed like the classic MessageBox.Show()?

The XAML for the ViewerWindow is:

<SimpleChildWindow:ChildWindow x:Class="Viewer.AM.Windows.ViewerWindow"
        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:Metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:SimpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
        xmlns:local="clr-namespace:Viewer.AM.Windows"
        mc:Ignorable="d"
        Style="{StaticResource ModalWindow}">
    <Frame x:Name="ViewFrame"/>
</SimpleChildWindow:ChildWindow>

and the ModalWindow style is:

<Style x:Key="ModalWindow" TargetType="SimpleChildWindow:ChildWindow">
        <Setter Property="Background" Value="{StaticResource GrowLightColourBrush}"/>
        <Setter Property="IconTemplate" Value="{DynamicResource WindowIconTemplate}"/>
        <Setter Property="IsModal" Value="True"/>
        <Setter Property="TitleBarBackground" Value="{StaticResource GrowLightColourBrush}"/>
        <Setter Property="TitleForeground" Value="Black"/>
    </Style>
AlexGimo commented 7 years ago

same problem here...