2

SilverLightには、Winformsと同等のものがありControl.InvokeRequiredますか?

Winforms Invokeはと同等であることがすでにわかりましたControl.Dispatcher.BeginInvokeが、次のようなものは見つかりません。InvokeRequired

4

1 に答える 1

3

次の拡張方法は非常に便利です

public static bool InvokeRequired(this FrameworkElement element)
{
    return !element.Dispatcher.CheckAccess();
}
public static void Invoke(this FrameworkElement element, Action action)
{
    if (element.InvokeRequired())
    {
        using (AutoResetEvent are = new AutoResetEvent(false))
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                action.Invoke();
                are.Set();
            });
            are.WaitOne();
        }
    }
    else
        action.Invoke();
}
于 2012-12-06T11:51:16.360 に答える