[Universal App]_解決Universal App在Windows Phone上面,Mobile Service 身分驗證會有錯誤的問題

在官方的 Mobile Service 身分驗證教學中,有提到在OnNavigatedTo()裡面就可以進行驗證,

官方範例的todo items也是這麼做,驗證過之後再去跟Azure Mobile Service撈資料。

官方文件在:http://azure.microsoft.com/zh-tw/documentation/articles/mobile-services-windows-phone-get-started-users/

 

這樣子的作法在之前Windows Phone 8.0以前是可以這麼做的

但到了Universal App則會出現問題
主要是因為Windows Phone 8.1在OnNavigatedTo()地同時若運作Azure Mobile Service驗證,則會又換一頁
這樣會有UI執行續的問題

 

造成Azure Mobile Service吐一個看似不像UI問題的錯誤:

An exception of type 'System.Exception' occurred in mscorlib.ni.dll but was not handled in user code

Additional information: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

 

解決這個問題讓Universal App Todoitem能夠順利運作必須要變動以下兩處

1.在App.xaml.cs裡面加入

        protected override void OnActivated(IActivatedEventArgs args)
        {
            base.OnActivated(args);

#if WINDOWS_PHONE_APP
            if (args.Kind == ActivationKind.WebAuthenticationBrokerContinuation)
            {
                App.MobileService.LoginComplete(args as WebAuthenticationBrokerContinuationEventArgs);
            }
#endif
        }

 

複寫OnActivated,讓App啟動過程中Windows Phone檢查是否已經登入過。

 

2.原先寫在MainPage.cs裡面的內容,通通先註解移除吧!

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {

            ////FB AUTH
            //var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);

            ////SHOW USER ID
            //await (new MessageDialog(App.MobileService.CurrentUser.UserId)).ShowAsync();
            //if (App.MobileService.CurrentUser.UserId != null)
            //{
            //    //await InitLocalStoreAsync(); // offline sync
            //    await RefreshTodoItems();
            //}

        }

 

 

3.在開頭,讓頁面載入完之後開一個dispatcher讓他執行登入程序(auth)

        public MainPage()
        {
            this.InitializeComponent();

            Loaded += async (sender, args) =>
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, auth);
            };
        }

 

 

4.把登入程序另外寫成一個名為auth的副程式(名子隨便你)

        private async void auth()
        {

            //FB AUTH
            var user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);

            //SHOW USER ID
            await (new MessageDialog(App.MobileService.CurrentUser.UserId)).ShowAsync();
            if (App.MobileService.CurrentUser.UserId != null)
            {
                //await InitLocalStoreAsync(); // offline sync
                await RefreshTodoItems();
            }

        }

 

 

就大功告成啦!

 

2015-03-03_22-49-36

 

thx for:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/95c6569e-2fa2-43c8-af71-939e006a9b27/mobile-services-loginasync-remote-procedure-call-failed-hresult-0x800706be?forum=azuremobile

Leave a comment

Your email address will not be published.




這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

Bitnami