Adorner Not showing

BigH61 481 Reputation points
2023-09-15T09:50:57.5466667+00:00

Hope you can help.

I am continuing to try and learn WPF and my current test project (which I have simplified significantly to demonstrate my problem) is to display an Adorner on a window when a button is clicked to open a new Window. When this window is displayed the Adorner is hidden.

My problem is the Adorner is not displaying.

Code for all but the Adorner below (for the purpose of this test Adorner style can be anything)

MainWindow Xaml

<Grid>     
<Grid.RowDefinitions>         
<RowDefinition Height="30"/>         
<RowDefinition Height="300"/>     
</Grid.RowDefinitions>     
<Button Grid.Row="0" Click="OnCreateNewWindow" Content="Create new Window1" Width="150"/>    
<ad:AdornedControl Grid.Row="1" Name="LoadingAdorner" >         
<ad:AdornedControl.AdornerContent>             
<local:WorkInProgress/>         
</ad:AdornedControl.AdornerContent>     
</ad:AdornedControl> </Grid>
MainWindow Xaml.cs

MainWindow Xaml.cs

private void OnCreateNewWindow(object sender, RoutedEventArgs e)         
{             
LoadingAdorner.ShowAdorner();              
Thread.Sleep(3000);             
Window1 w = new Window1();             
w.Show();              
LoadingAdorner.HideAdorner();         
}

If I remove the HideAdorner the Adorner displays the instant the new Window displays. How can I get the Adorner to display when the button is Clicked? Thank you for your assistance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,477 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,973 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 102.4K Reputation points
    2023-09-15T11:35:56.7466667+00:00

    It is not clear what controls, adorners and functions are you using. Maybe this will work:

    private async void OnCreateNewWindow( object sender, RoutedEventArgs e )
    {          
       LoadingAdorner.ShowAdorner();    
       await Task.Delay( 3000 );
       Window1 w = new Window1();              
       w.Show();   
       LoadingAdorner.HideAdorner();             
    }
    

  2. Hui Liu-MSFT 21,376 Reputation points Microsoft Vendor
    2023-09-18T07:57:02.4066667+00:00

    Hi,@BigH61.Welcome Microsoft Q&A. You could try to use a timer to control the delay and ensure that the animation continues running.

    You can try to see if the following code works for you.

    private async void OnCreateNewWindow(object sender, RoutedEventArgs e)
    {
        LoadingAdorner.ShowAdorner();
    
        // Simulate some asynchronous work with a timer (remove this in your actual code)
        var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(3) };
        timer.Tick += (s, args) =>
        {
            timer.Stop();
            Window1 w = new Window1();
            w.Show();
            LoadingAdorner.HideAdorner();
        };
        timer.Start();
    }
    
    
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.