Equivalent to InvokeRequired in WPF

前端 未结 4 1205
醉酒成梦
醉酒成梦 2021-02-15 22:40

Is there an equivalent to Form.InvokeRequired in WPF, e.g. Dispatcher.InvokeRequired?

4条回答
  •  我在风中等你
    2021-02-15 23:26

    If you're building a Windows Store app, the above example won't work. Here's an example that does work. Modify as needed, of course :)

    /// 
    /// Updates the UI after the albums have been retrieved. This prevents the annoying delay when receiving the albums list.
    /// 
    /// 
    public void UpdateUiAfterAlbumsRetrieved(System.Collections.ObjectModel.ObservableCollection albums)
    {
        if (!Dispatcher.HasThreadAccess)
        {
            Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                ddlAlbums.DataContext = albums;
                ddlAlbums.IsEnabled = true;
                tbxProgress.Text = String.Empty;
                ProgressBar.IsIndeterminate = false;
                ProgressBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            });
        }
        else
        {
            ddlAlbums.DataContext = albums;
            ddlAlbums.IsEnabled = true;
            tbxProgress.Text = String.Empty;
            ProgressBar.IsIndeterminate = false;
        }
    }
    

提交回复
热议问题