开发者

How to access Dispatcher when no reference to UI objects and Application.Current.Dispatcher is null?

开发者 https://www.devze.com 2023-02-02 19:55 出处:网络
I\'m calling this from a non-UI thread (another thread than my WPF controls) : public static FontFamily GetDefaultFontFamily()

I'm calling this from a non-UI thread (another thread than my WPF controls) :

public static FontFamily GetDefaultFontFamily()
{
    FontFamily fontFamily = null;

    Application.Current.Dispatcher.Invoke(
        new Action(
            delegate
            {
                fontFamily = new TextBlock().FontFamily;
            }));

    return fontFamily;
}

but Application.Current is null in my case (WPF objects hosted in MFC app)...

The only solution is to pass as a parameter a DispatcherObject instance, but this causes many modifications elsewhere, and makes it not a clean s开发者_如何转开发olution from the calling code.

Any ideas ?


I got around the problem by making the class store a valid reference to a dispatcher the first time the class is used (made it a singleton too).

Not the best solution, but its the less worse...

0

精彩评论

暂无评论...
验证码 换一张
取 消