0
void CallBackVerifiedResponse(OAuthAccessToken at, TwitterResponse response)
    {
        if (at != null)
        {

            SerializeHelper.SaveSetting<TwitterAccess>("TwitterAccess", new TwitterAccess
            {
                AccessToken = at.Token,
                AccessTokenSecret = at.TokenSecret,
                ScreenName = at.ScreenName,
                UserId = at.UserId.ToString()
            });


        }    
    }

    private void ok_Click_1(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(pinText.Text))
            MessageBox.Show("Please enter PIN");
        else
        {
            try
            {
                var cb = new Action<OAuthAccessToken, TwitterResponse>(CallBackVerifiedResponse);
                service.GetAccessToken(_requestToken, pinText.Text, CallBackVerifiedResponse);
            }
            catch
            {
                MessageBox.Show("Something is wrong with the PIN. Try again please.", "Error", MessageBoxButton.OK);
            }

        }


    }

私の問題はNavigationService.GoBack()CallBackVerifiedResponseメソッド内で使用すると不正なアクセス例外が発生し、クリックイベント内で使用するとCallBackVerifiedResponseトリガーされないことです。何か案は?

4

3 に答える 3

0

または、次のように使用して、独自のスマート ディスパッチャを使用することもできます。

SmartDispatcher.BeginInvoke(() =>
{                    
    MissionAccomplished();                    
});

次のようにコーディングしました。

{
    using System.ComponentModel;
    using System.Windows.Threading;
    using System.Windows;
    using System;

    public static class SmartDispatcher
    {
        /// <summary>
        /// A single Dispatcher instance to marshall actions to the user
        /// interface thread.
        /// </summary>
        private static Dispatcher _instance;

        /// <summary>
        /// Backing field for a value indicating whether this is a design-time
        /// environment.
        /// </summary>
        private static bool? _designer;

        /// <summary>
        /// Requires an instance and attempts to find a Dispatcher if one has
        /// not yet been set.
        /// </summary>
        private static void RequireInstance()
        {
            if (_designer == null)
            {
                _designer = DesignerProperties.IsInDesignTool;
            }

            // Design-time is more of a no-op, won't be able to resolve the
            // dispatcher if it isn't already set in these situations.
            if (_designer == true)
            {
                return;
            }

            // Attempt to use the RootVisual of the plugin to retrieve a
            // dispatcher instance. This call will only succeed if the current
            // thread is the UI thread.
            try
            {
                _instance = Application.Current.RootVisual.Dispatcher;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("The first time SmartDispatcher is     used must be from a user interface thread. Consider having the application call Initialize, with or without an instance.", e);
            }

            if (_instance == null)
            {
                throw new InvalidOperationException("Unable to find a suitable Dispatcher instance.");
            }
        }

        /// <summary>
        /// Initializes the SmartDispatcher system, attempting to use the
        /// RootVisual of the plugin to retrieve a Dispatcher instance.
        /// </summary>
        public static void Initialize()
        {
            if (_instance == null)
            {
                RequireInstance();
            }
        }

        /// <summary>
        /// Initializes the SmartDispatcher system with the dispatcher
        /// instance.
        /// </summary>
        /// <param name="dispatcher">The dispatcher instance.</param>
        public static void Initialize(Dispatcher dispatcher)
    {
        if (dispatcher == null)
        {
            throw new ArgumentNullException("dispatcher");
        }

        _instance = dispatcher;

        if (_designer == null)
        {
            _designer = DesignerProperties.IsInDesignTool;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static bool CheckAccess()
    {
        if (_instance == null)
        {
            RequireInstance();
        }

        return _instance.CheckAccess();
    }

    /// <summary>
    /// Executes the specified delegate asynchronously on the user interface
    /// thread. If the current thread is the user interface thread, the
    /// dispatcher if not used and the operation happens immediately.
    /// </summary>
    /// <param name="a">A delegate to a method that takes no arguments and 
    /// does not return a value, which is either pushed onto the Dispatcher 
    /// event queue or immediately run, depending on the current thread.</param>
    public static void BeginInvoke(Action a)
    {
        if (_instance == null)
        {
            RequireInstance();
        }

        // If the current thread is the user interface thread, skip the
        // dispatcher and directly invoke the Action.
        if (_instance.CheckAccess() || _designer == true)
        {
            a();
        }
        else
        {
            _instance.BeginInvoke(a);
        }
    }
}
}
于 2012-12-14T16:53:47.410 に答える
0

UIThread を使用して解決しました

public static class UIThread
{
    private static readonly Dispatcher Dispatcher;

    static UIThread()
    {
        // Store a reference to the current Dispatcher once per application
        Dispatcher = Deployment.Current.Dispatcher;
    }

    /// <summary>
    ///   Invokes the given action on the UI thread - if the current thread is the UI thread this will just invoke the action directly on
    ///   the current thread so it can be safely called without the calling method being aware of which thread it is on.
    /// </summary>
    public static void Invoke(Action action)
    {
        if (Dispatcher.CheckAccess())
            action.Invoke();
        else
            Dispatcher.BeginInvoke(action);
    }
}

この静的クラスの後

私はそれを内部で呼び出しました

この UIThread.Invoke(()=>NavigationService.GoBack()); のような Cal​​lBackVerifiedResponse;

于 2012-12-14T11:19:46.660 に答える
0

Dispatcher.BeginInvoke を使用して UIThread にアクセスできます

Dispatcher.BeginInvoke(() =>
              {
                   NavigationService.GoBack();
              });
于 2012-12-14T11:24:44.353 に答える