C # WPF - Long Click operation

I have Buttonone that calls another method when clicked. Another method performs an operation, which can take a lot of time ... Therefore, I decided to create Labelone that appears at the beginning of the operation and tells the user to wait until it disappears after the operation is completed Label. The only problem is that since it Buttonis an element UI(this is what I think is the reason), calls to change Labelinside Buttonclick only activate after clicking Button(therefore, it was mostly Labelinvisible before a click and cannot change during it, so it stays that way) .

Here is my code:

private void SearchButtonActions()
{
        UI.InvokeA(() => lstFiles.ItemsSource = FDItems);
        bool SearchAndListing = false;
        //UI.InvokeA(() => lblWait.Height = double.NaN);
        //UI.InvokeA(() => lblWait.Visibility = Visibility.Visible);
        //UI.InvokeA(() => lblWait.Content = "Search Started...");
        int index = cbTypes.SelectedIndex;
        string selecteditem = cbSearchOption.SelectedItem.ToString();
        SearchAndListing = FD.Search(index, selecteditem);
        FDItems = new ObservableCollection<Item>(FD.Items);
        //UI.InvokeA(() => lblWait.Height = 0);
        //UI.InvokeA(() => lblWait.Visibility = Visibility.Hidden);
        //UI.InvokeA(() => lblWait.Content = "Search Ended.");
        if (SearchAndListing)
        {
            UI.InvokeA(() => lstFiles.ItemsSource = FDItems);
            UI.InvokeA(() => lblCount.Content = string.Format("Items: {0}", FDItems.Count));
        }
}

I'm talking about change methods lblWait... btw: UI.Invokeis a shortcut toDispatcher.Current.InvokeAsync(Action)

Tasks, BackGroundWorker UI.Invoke Invoke (synchronically asynchronically), ...

- ?

+4
2

.

, Click, .

, ui .

:

   lbl.Visibility = Visibility.Visible;
   Thread.Sleep(3000);

:

    Dispatcher.Invoke(() => lbl.Visibility = Visibility.Visible);
    Dispatcher.Invoke(() => Thread.Sleep(3000));

, ui .

XAML:

    <StackPanel>   
       <Button Click="Button_Click" Content="Click"/>
       <Label x:Name="lbl" Content="Label" Visibility="Hidden" Foreground="Red" FontSize="22" HorizontalAlignment="Center"/>
     </StackPanel>

CS:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await Dispatcher.InvokeAsync(() => 
        {
            Debug.WriteLine("Visibility");
            lbl.Visibility = Visibility.Visible;                
        });

        await Task.Run(() =>
        {
            return Dispatcher.InvokeAsync(() => Thread.Sleep(3000));
        });            
    }

FYI: , , . , DispatcherObject ( DependencyObject, DispatcherObject) .

0
  • ", ", .
  • : FD.Search(index, selecteditem); async Task.Run()
  • ContinueWith(), .

    private void SearchButtonActions() {
        lblWait.Visibility = Visibility.Visible;
        Task.Run(() => {
            return FD.Search(index, selecteditem);
        }).ContinueWith((Task<bool> task) => {
            Dispatcher.Invoke(() => { 
              FDItems = new ObservableCollection<Item>(FD.Items));
              lblWait.Visibility = Visibility.Hidden;
            });
        });
    }
    

FD.Search() - ContinueWith(), task.Result .

0

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


All Articles