MaterialDesignInXAML / MaterialDesignInXamlToolkit

Google's Material Design in XAML & WPF, for C# & VB.Net.
http://materialdesigninxaml.net
MIT License
15.12k stars 3.42k forks source link

When I open a new window in WPF, the control does not appear in real time. #982

Closed highspeeder closed 6 years ago

highspeeder commented 6 years ago

When I add a button click event in the main window and then "Show" the new window, the controls of the new window are not displayed. If the operation of the button click event function is not completed, the control of the new window is not displayed.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;

namespace Wpf { public partial class MainWindow : Window {

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        Window1 win1 = new Window1();

        textBox.Text = "";
        textBox2.Text = "";

        win1.Show();

        Task.Delay(3000).Wait();
        textBox.Text = "HI";
        textBox2.Text = "HELLO";

        win1.Close();
        Task.Delay(3000).Wait();

    }

}

}

<Window x:Class="Wpf.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:Wpf" mc:Ignorable="d"

    Title="MainWindow" Height="350" Width="525" 
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
    Background="{DynamicResource MaterialDesignPaper}"
    TextElement.FontWeight="Medium"
    TextElement.FontSize="14"
    FontFamily="pack://application:,,,/MaterialDesignThemes.Wpf;component/Resources/Roboto/#Roboto" >

<Grid>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="117,216,0,0" VerticalAlignment="Top" Width="210" Height="80" Click="button_Click"/>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="29,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="29,73,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

</Grid>

<Window x:Class="Wpf.Window1" 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:Wpf" mc:Ignorable="d"

    Title="Window1" Height="300" Width="300" WindowStyle="None">

<Grid>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"/>
        <Label x:Name="label" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes;

namespace Wpf { public partial class Window1 : Window { public Window1() { InitializeComponent(); } } }

jespersh commented 6 years ago

If I recall correctly, then your calls likeTask.Delay(3000).Wait(); actually blocks the thread. Change it to private async void button_Click and await Task.Delay(3000); (both of them)

highspeeder commented 6 years ago

The above Issue has been solved, but now the same thing happens in other code.

I do not use this theme, it works without any problems using the default.

How do I resolve the issue?

The following is the code of the problem I have.

namespace Wpftest
{
    public partial class MainWindow : Window
    {
        uint handle = 0;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Window1 win1 = new Window1();
            win1.Show();

            if (handle == 0)      <<---I hope the controls are displayed at this timing.
                handle = 10;

            if (handle == 0)
            {
                MessageBox.Show("Handle is 0!!", "", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            else
            {
                handle = 20;
            }
            win1.Close();
        }

    }
}    <<----But at this timing, the controls are displayed.

<Window x:Class="Wpftest.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:Wpftest"
        xmlns:gif="http://wpfanimatedgif.codeplex.com"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
                TextElement.Foreground="{DynamicResource MaterialDesignBody}"
        Background="{DynamicResource MaterialDesignPaper}"
        TextElement.FontWeight="Medium"
        TextElement.FontSize="14"
        FontFamily="pack://application:,,,/MaterialDesignThemes.Wpf;component/Resources/Roboto/#Roboto">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="469,291,0,0" VerticalAlignment="Top" Width="210" Height="80" Click="button_Click"/>
        <Image x:Name="image" gif:ImageBehavior.RepeatBehavior="Forever" gif:ImageBehavior.AnimatedSource="Resource\loading.gif" HorizontalAlignment="Left" Height="100" Margin="190,116,0,0" VerticalAlignment="Top" Width="100" Visibility="Hidden"/>

    </Grid>
</Window>

namespace Wpftest
{

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }
}

<Window x:Class="Wpftest.Window1"
        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:Wpftest"
        mc:Ignorable="d"
        Title="Window1" Height="262.387" Width="402.909">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="270,169,0,0"/>
        <Label x:Name="label" Content="TEST" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="41,66,0,0" Width="162"/>
    </Grid>
</Window>
Keboo commented 6 years ago

@highspeeder I am not sure I understand. How are you determining if a window is displayed at a given line of code? Your end timing appears to reference the closing brace } of the namespace which is not actually a debuggable line of code.

You are correct in your assumption that the Window should be displayed directly after the call to Show(). However if you have the debugger attached and are stepping through the code, you will not be able to interact with it, since the debugger will have frozen the UI thread that is controlling the window. This blocking of the UI thread is also what is happening in your first example with the the Task.Delay(3000).Wait();. Because .Wait() is a blocking call, the UI thread is not able to spend time processing window events. It is also worth mentioning that MessageBox.Show is also a blocking call.

You can read more about the WPF threading model here. That documentation uses the older style threading model to illustrate its point; newer code should instead favor using async/await and a Task Parallel Library and use Tasks like you have done. But that is a much larger discussion that does not really pertain to this library. There are lots of good resources out there for learning more about it; like Essential C# (full disclosure; I work for IntelliTect).