[英]Shake Gesture Library in WP8
我正在嘗試設計一個在搖動手機時生成隨機數的游戲。 我正在使用Shake Gesture Library ,請參考下面的鏈接:
我將默認示例代碼從:
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
_lastUpdateTime.Dispatcher.BeginInvoke(
() =>
{
_lastUpdateTime.Text = DateTime.Now.ToString();
CurrentShakeType = e.ShakeType;
});
}
至:
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
PlayButton_Click(null, null);
}
PlayButton_Click()是我的方法,其中包含rest語句以生成隨機數。
我搖了搖手機,但進入PlayButton_Click()的第一條語句后卻顯示錯誤:
System.Windows.ni.dll中發生類型為'System.UnauthorizedAccessException'的異常,但未在用戶代碼中處理
問題在於PlayButton_Click正在訪問UI組件,對Instance_ShakeGesture的調用是通過非UI線程進行的,您不能訪問除主UI線程以外的任何線程上的UI組件。 Dispatcher.BeginInvoke用於將請求放在UI線程上。 您需要使用Dispatcher.BeginInvoke來調用您的PlayButton_Click
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
PlayButton_Click(null, null);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.