Windows phone 7 NavigationService.GoBack()未经授权的导出 void CallBackVerifiedResponse(OAuthAccessToken at,TwitterResponse) { 如果(at!=null) { SerializeHelper.SaveSetting(“TwitterAccess”),新TwitterAccess { AccessToken=at.Token, AccessTokenSecret=at.TokenSecret, ScreenName=at.ScreenName, UserId=at.UserId.ToString() }); } } 私有无效确定\单击\ 1(对象发送方,事件参数e) { if(String.IsNullOrEmpty(pinText.Text)) MessageBox.Show(“请输入PIN”); 其他的 { 尝试 { var cb=新操作(CallBackVerifiedResponse); GetAccessToken(_requestToken,pinText.Text,CallBackVerifiedResponse); } 抓住 { MessageBox.Show(“PIN出现问题,请重试。”,“错误”,MessageBox按钮。确定); } } }
我的问题是,当我在Windows phone 7 NavigationService.GoBack()未经授权的导出 void CallBackVerifiedResponse(OAuthAccessToken at,TwitterResponse) { 如果(at!=null) { SerializeHelper.SaveSetting(“TwitterAccess”),新TwitterAccess { AccessToken=at.Token, AccessTokenSecret=at.TokenSecret, ScreenName=at.ScreenName, UserId=at.UserId.ToString() }); } } 私有无效确定\单击\ 1(对象发送方,事件参数e) { if(String.IsNullOrEmpty(pinText.Text)) MessageBox.Show(“请输入PIN”); 其他的 { 尝试 { var cb=新操作(CallBackVerifiedResponse); GetAccessToken(_requestToken,pinText.Text,CallBackVerifiedResponse); } 抓住 { MessageBox.Show(“PIN出现问题,请重试。”,“错误”,MessageBox按钮。确定); } } },windows-phone-7,Windows Phone 7,我的问题是,当我在CallBackVerifiedResponse方法中使用NavigationService.GoBack()时,我遇到了未经授权的访问异常,如果我在click事件中使用它,则不会触发ICallBackVerifiedResponse。有什么想法吗?已经用UIThread解决了 void CallBackVerifiedResponse(OAuthAccessToken at, TwitterResponse response) { if (at !=
CallBackVerifiedResponse
方法中使用NavigationService.GoBack()
时,我遇到了未经授权的访问异常,如果我在click事件中使用它,则不会触发ICallBackVerifiedResponse
。有什么想法吗?已经用UIThread解决了
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);
}
}
}
公共静态类UIThread
{
专用静态只读调度器;
静态UIThread()
{
//每个应用程序存储一次对当前调度程序的引用
Dispatcher=Deployment.Current.Dispatcher;
}
///
///在UI线程上调用给定的操作-如果当前线程是UI线程,则直接在UI线程上调用该操作
///当前线程,以便可以安全地调用它,而调用方法不知道它在哪个线程上。
///
公共静态无效调用(操作)
{
if(Dispatcher.CheckAccess())
action.Invoke();
其他的
调度员。开始激活(操作);
}
}
在这堂课之后
我把它称为
CallBackVerifiedResponse
这样地
调用(()=>NavigationService.GoBack())
您可以使用Dispatcher.BeginInvoke访问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);
}
}
或者您也可以使用自己的智能调度程序,如下所示:
Dispatcher.BeginInvoke(() =>
{
NavigationService.GoBack();
});
编码如下:
SmartDispatcher.BeginInvoke(() =>
{
MissionAccomplished();
});
{
使用系统组件模型;
使用System.Windows.Threading;
使用System.Windows;
使用制度;
公共静态类SmartDispatcher
{
///
///单个Dispatcher实例向用户发送动作
///接口线程。
///
私有静态调度程序_实例;
///
///用于指示这是否为设计时的值的支持字段
///环境。
///
私人静态布尔?\u设计师;
///
///需要一个实例并尝试查找调度程序(如果有)
///尚未确定。
///
私有静态无效恢复()
{
如果(_designer==null)
{
_设计器=DesignerProperties.IsInDesignTool;
}
//设计时间更多的是无操作,无法解决问题
//如果在这些情况下尚未设置,则为dispatcher。
如果(_designer==true)
{
返回;
}
//尝试使用插件的RootVisual来检索
//dispatcher实例。只有当前
//线程是UI线程。
尝试
{
_实例=Application.Current.RootVisual.Dispatcher;
}
捕获(例外e)
{
抛出新的无效操作异常(“第一次使用SmartDispatcher必须来自用户接口线程。考虑应用程序调用初始化,有或没有实例。”,E);
}
if(_instance==null)
{
抛出新的InvalidOperationException(“无法找到合适的调度程序实例”);
}
}
///
///初始化SmartDispatcher系统,尝试使用
///用于检索Dispatcher实例的插件的RootVisual。
///
公共静态void Initialize()
{
if(_instance==null)
{
要求复职();
}
}
///
///使用调度程序初始化SmartDispatcher系统
///例如。
///
///调度程序实例。
公共静态无效初始化(调度程序)
{
if(dispatcher==null)
{
抛出新的ArgumentNullException(“dispatcher”);
}
_实例=调度程序;
如果(_designer==null)
{
_设计器=DesignerProperties.IsInDesignTool;
}
}
///
///
///
///
公共静态bool CheckAccess()
{
if(_instance==null)
{
要求复职();
}
返回_instance.CheckAccess();
}
///
///在用户界面上异步执行指定的委托
///如果当前线程是用户界面线程,则
///如果不使用调度程序,操作将立即发生。
///
///对不接受参数和参数的方法的委托
///不返回值,该值被推送到调度程序上
///事件队列或立即运行,具体取决于当前线程。
公共静态无效开始触发(动作a)
{
if(_instance==null)
{
要求复职();
}
//如果当前线程是用户界面线程,请跳过
//并直接调用该操作。
如果
{
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);
}
}
}
}