Cannot access non-static method in static context?

前端 未结 3 2025
春和景丽
春和景丽 2021-02-04 17:02

Given this code....

public class CalibrationViewModel : ViewModelBase
{
    private FileSystemWatcher fsw;

    public CalibrationViewModel(Calibration calibrati         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 17:41

    If this is WPF, System.Windows.Threading.Dispatcher does not have a static BeginInvoke() method.

    If you want to call that statically (this is, without having a reference to the Dispatcher instance itself), you may use the static Dispatcher.CurrentDispatcher property:

    Dispatcher.CurrentDispatcher.BeginInvoke(...etc);
    

    Be aware though, that doing this from a background thread will NOT return a reference to the "UI Thread"'s Dispatcher, but instead create a NEW Dispatcher instance associated with the said Background Thread.

    A more secure way to access the "UI Thread"'s Dispatcher is via the use of the System.Windows.Application.Current static property:

    Application.Current.Dispatcher.BeginInvoke(...etc);
    

提交回复
热议问题