如何从其他appdomain获取UI线程调度程序?

时间:2017-08-07 17:13:24

标签: c# wpf appdomain dispatcher

我曾经在常规WPF应用程序中使用Application.Current.Dispatcher获取Dispatcher。但是当我尝试从不同的AppDomain获取它时,它不起作用,因为Application.Current在那里返回null。

1 个答案:

答案 0 :(得分:0)

当我过去需要访问DispatcherApplication.Current为空时,我使用了构造函数注入来注入Window的Dispatcher

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var objectNeedingDispatcher = new ClassNeedingDispatcher(this.Dispatcher);
    }
}


public class ClassNeedingDispatcher
{
    private readonly Dispatcher _injectedDispatcher;

    public ClassNeedingDispatcher(Dispatcher injectedDispatcher)
    {
        _injectedDispatcher = injectedDispatcher;
    }
}