new APIs for windows phone 8.1

后端 未结 4 440
小蘑菇
小蘑菇 2021-02-05 22:23

I am trying to use these two methods (of WP 8) in windows phone 8.1, but it gives error and doesn\'t compile, most probably becasue they are removed. I tried searching the new A

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 23:14

    Be aware that not only has the API changed (adopting the API from WindowsStore apps), but the way that the Dispatcher was obtained in windowsPhone 8.0 has changed as well.

    @Johan Faulk's suggestion, although will work, may return null under a multitude of conditions.

    Old code to grab the dispatcher:

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

    New in Windows 8.1 Deployment is not an available object or namespace.

    In order to make sure the Main UI Thread dispatcher is obtained, use the following:

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

    Additionally, although the method SAYS it will be executed Async the keyword await can not be used in the method invoked by RunAsync. (in the above example the method is anonymous).

    In order to execute an awaitable method inside anonymous method above, decorate the anonymous method inside RunAsync() with the async keyword.

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

提交回复
热议问题