繁体   English   中英

获取“访问被拒绝。 (在HR8ULT中出现异常:0x80070005(E_ACCESSDENIED))在Windows 8 App中,在DispatchTimer中使用Message对话框时?

[英]Getting “Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))” in Windows 8 App when using Message dialog in DispatchTimer?

我试图在调度计时器中使用消息对话框来在时间完成时更改用户。 但有时会出现以下错误:“访问被拒绝。(HRESULT异常:0x80070005(E_ACCESSDENIED))”。 怎么解决这个?

码:

 public DetailPage()
        {
      timer = new DispatcherTimer();
            timer.Tick += dispatcherTimer_Tick; 
            timer.Interval = new TimeSpan(0, 0, 1);
            this.txtTimer.Text = GlobalVariables.totalTime.Minutes + ":" + GlobalVariables.totalTime.Seconds + "mins";
            timer.Start();
}



  async void dispatcherTimer_Tick(object sender, object e)
    {
        if (GlobalVariables.totalTime.Minutes > 0 || GlobalVariables.totalTime.Seconds > 0)
        {
            GlobalVariables.totalTime = GlobalVariables.totalTime.Subtract(new TimeSpan(0, 0, 1));
            this.txtTimer.Text = GlobalVariables.totalTime.Minutes + ":" + GlobalVariables.totalTime.Seconds + " mins";
        }
        else
        {
            timer.Tick -= dispatcherTimer_Tick;
            timer.Stop();

            MessageDialog signInDialog = new MessageDialog("Time UP.", "Session Expired");

            // Add commands and set their callbacks
            signInDialog.Commands.Add(new UICommand("OK", (command) =>
            {
                this.Frame.Navigate(typeof(HomePage), "AllGroups");
            }));

            // Set the command that will be invoked by default
            signInDialog.DefaultCommandIndex = 1;

            // Show the message dialog
            await signInDialog.ShowAsync();
        }
    }

我收到的错误是:

 // Show the message dialog
        await signInDialog.ShowAsync();

像Jeff所说,计时器Tick事件处理程序代码在与UI线程不同的线程上运行。 您将不得不回到此UI线程来操作UI中的任何内容:消息对话框,更改属性等。

// some code for the timer in your page
timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 1)};
timer.Tick += TimerOnTick;
timer.Start();

// event handler for the timer tick
private void TimerOnTick(object sender, object o)
{
    timer.Stop();
    var md = new MessageDialog("Test");

    this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => md.ShowAsync());
}

请注意,我确实在事件处理程序中停止了计时器。 如果您没有在显示另一个消息对话框之前及时关闭消息对话框,那么您也将在第二个ShowAsync上获得拒绝访问(因为第一个仍处于打开状态)。

dispatcherTimer_Tick方法在与UI不同的线程上运行。 如果要访问绑定到UI线程的内容(如UX),则必须返回到UI线程。 最简单的方法是用你的代码包装

Dispatcher.RunAsync()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM