【问题标题】:new APIs for windows phone 8.1Windows Phone 8.1 的新 API
【发布时间】:2014-06-26 02:34:36
【问题描述】:

我正在尝试在 windows phone 8.1 中使用这两种方法(WP 8),但它给出了错误并且无法编译,很可能是因为它们已被删除。我尝试搜索新的 API,但没有找到。这些还有什么其他选择。

Dispatcher.BeginInvoke( () => {});msdn link

System.Threading.Thread.Sleep();msdn link

【问题讨论】:

    标签: windows-phone-8 windows-phone-8.1


    【解决方案1】:

    它们仍然存在于 Windows Phone 8.1 SIlverlight 应用程序中,但不适用于 Windows Phone 应用商店应用程序。 Windows 应用商店应用的替代品是:

    睡眠(见Thread.Sleep replacement in .NET for Windows Store):

    await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(30));
    

    调度员(见How the Deployment.Current.Dispatcher.BeginInvoke work in windows store app?):

    CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
    await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { });
    

    【讨论】:

      【解决方案2】:

      Dispatcher.BeginInvoke( () => {}); 被替换为

      await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {});
      

      System.Threading.Thread.Sleep(); 被替换为

      await Task.Delay(TimeSpan.FromSeconds(doubleValue));
      

      【讨论】:

        【解决方案3】:

        请注意,不仅 API 发生了变化(采用 WindowsStore 应用程序的 API),而且在 windowsPhone 8.0 中获取 Dispatcher 的方式也发生了变化。

        @Johan Faulk 的建议虽然可行,但在多种情况下可能会返回 null。

        代码来获取调度器:

        var dispatcher = Deployment.Current.Dispatcher;
        or
        Deployment.Current.Dispatcher.BeginInvoke(()=>{
             // any code to modify UI or UI bound elements goes here 
        });
        

        Windows 8.1 中的新功能部署不是可用的对象或命名空间。

        为了确保获得主 UI 线程调度程序,请使用以下内容:

        var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
        or 
        CoreApplication.MainWindow.CoreWindow.Dispatcher.RunAsync(
          CoreDispatcherPriority.Normal,
          ()=>{
              // UI code goes here
        });
        

        另外,虽然方法SAYS 会执行Async,但关键字await 不能在RunAsync 调用的方法中使用。 (在上面的例子中,方法是匿名的)。

        为了在上述匿名方法中执行可等待的方法,请使用 async 关键字装饰 RunAsync() 中的匿名方法。

        CoreApplication.MainWindow.CoreWindow.Dispatcher.RunAsync(
        CoreDispatcherPriority.Normal,
        **async**()=>{
              // UI code goes here
              var response = **await** LongRunningMethodAsync();
        });
        

        【讨论】:

        • 自从这篇文章之后一切都变了还是我错过了什么?您放置 await 会导致语法错误,当我将“await”从 Dispatcher 之前移动到匿名方法之前时,我自己的代码也是如此。私有异步无效 PlayLaser() { DISpatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, await () => {laser.Play(); }); }(我试图完全不等待,但我的音频有时太延迟了。)
        • 请说得更具体一些,Windows 8.1 自从这篇文章以来没有改变。至于 Windows 10,这可能不是适当的线程,因为它被标记为 WindowsPhone 8.1。许多 API 在 WindowsPhone 10 上已完全改变,以统一操作系统。
        • 我使用的是 Windows 10,但使用 VS2015 为 8.1 和 Phone 8.1 构建了一个通用应用程序。不是 Win10 通用版。
        • 请重新阅读我示例的最后一个代码块以及上面的注释。您的“麻烦代码”将 await 放在 *async 应该在的位置,并且不会等待最终被同步调用的laser.play()。这是因为您在匿名方法前面放置了 await,这是不可能的。这应该会有所帮助:---- private async void PlayLaser() { Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { await laser.Play(); }); }
        • 噢!但是 Play() 是不可等待的。我以为我正在使用 Dispatcher.RunAsync() 异步运行 Play() ,不是吗?此外,在我的异步方法中,我仍然收到关于在 Dispatcher.RunAsync() 之前没有等待的警告。哦,好吧,我想我有一些阅读要做。
        【解决方案4】:

        对于 Dispatcher,试试这个。 MSDN

        private async Task MyMethod()
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { });
        }
        

        对于Thread.Sleep(),试试await Task.Delay(1000)MSDN

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 1970-01-01
          • 2014-11-21
          • 1970-01-01
          • 2014-09-21
          • 2016-12-01
          • 1970-01-01
          相关资源
          最近更新 更多