WPF UI rendering error

I have a basic dialog box that allows users to enter some information. When they click the AddButton button, the application checks the user information and displays a message if the operation failed. Next to the button, which is initially hidden, there is a static label. When the button is pressed, the mark becomes visible before , the confirmation operation begins and again becomes invisible after . Please note that this verification operation is performed on a remote machine, so the user interface freezes during this time (by design).

Here is the XAML code:

 <Label Name="lblStatus" Content="Verifying connection..." Visibility="Hidden" Grid.Column="0" /> <StackPanel Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right"> <Button Content="Add" Width="80" Margin="0,0,5,0" Click="AddButton_Click"/> <Button Content="Cancel" Width="80" IsCancel="True"/> </StackPanel> 

And the code:

 private void AddButton_Click(object sender, RoutedEventArgs e) { lblStatus.Visibility = Visibility.Visible; // Validation occurs here. The UI is freezed for about 3 seconds Foo.Validate(); lblStatus.Visibility = Visibility.Hidden; } 

The problem is that when you click the button, the shortcut becomes invisible before the user interface freezes. When the operation fails, a pop-up dialog box appears. At this point, you see a shortcut, but this happens when the user interface wakes up. This makes me think that the user interface does not have enough time for reprocessing labile by the time foo.Validate() called. I was wondering if my theory is right or wrong, and what would be the “right” way to do this?

+4
source share
2 answers
 private void AddButton_Click(object sender, RoutedEventArgs e) { lblStatus.Visibility = Visibility.Visible; Task.Factory.StartNew(()=> { Foo.Validate(); OnUi(() => lblStatus.Visibility = Visibility.Hidden); } }; } public static void OnUi (Action action) { if (_dispatchService == null) _dispatchService = ServiceLocator.Current.GetInstance<IDispatchService>(); if (_dispatchService.CheckAccess()) action.Invoke (); else _dispatchService.Invoke(action); } 
+4
source

I had this problem before, here is how I solved it:

 public class CsUtil { public static void DoEvents() { Application.Current.Dispatcher.Invoke( DispatcherPriority.Background, new ThreadStart(DoNothing)); } private static void DoNothing() { // Just as it says, this method does nothing :-P } } 

This class will pretty much give you the old DoEvents () method from WinForms. This basically creates a new thread, calls a method that does nothing, and then kills the thread. One way or another, it gives focus back the UI thread, and updates happen a little faster.

To use this, just do it (in encoding):

 private void AddButton_Click(object sender, RoutedEventArgs e) { lblStatus.Visibility = Visibility.Visible; // Validation occurs here. The UI is freezed for about 3 seconds CsUtil.DoEvents(); Foo.Validate(); lblStatus.Visibility = Visibility.Hidden; } 

Good luck let me know if this doesn't work,

Hello,

Kyle

-1
source

Source: https://habr.com/ru/post/1485109/


All Articles