2016-04-07 112 views
0

我試圖在Xamarin.Forms中實現Azure Active Directory B2C。如果我只是複製他們的例子,我可以毫無問題地開展工作。但是當我嘗試使用棱鏡時,我遇到了問題。頁面第一次加載時,ViewModel中的OnNavigatedTo未觸發

我把這個代碼正坐在XAML的代碼隱藏:

protected override async void OnAppearing() 
{ 
    base.OnAppearing(); 
    App.PCApplication.PlatformParameters = platformParameters; 

    try { 
     var ar = await App.PCApplication.AcquireTokenSilentAsync(
      AuthenticationInfo.Scopes, string.Empty, AuthenticationInfo.Authority, 
      AuthenticationInfo.SignUpSignInpolicy, false); 

     AuthenticationInfo.UserAuthentication = ar; 
    } catch {    
    } 
} 

async void OnSignUpSignIn(object sender, EventArgs e) 
{ 
    try { 
     var ar = await App.PCApplication.AcquireTokenAsync(
      AuthenticationInfo.Scopes, string.Empty, UiOptions.SelectAccount, 
      string.Empty, null, AuthenticationInfo.Authority, 
      AuthenticationInfo.SignUpSignInpolicy); 

     AuthenticationInfo.UserAuthentication = ar; 
    } catch (Exception ex) { 
     if (ex != null) { 
     }    
    } 
} 

,並移動到視圖模型的的OnNavigatedTo:

public async void OnNavigatedTo (NavigationParameters parameters) 
     { 
      if (parameters.ContainsKey ("title")) 
       Title = (string)parameters ["title"]; 

      listen2asmr.App.PCApplication.PlatformParameters = platformParameters; 

      try { 
       var ar = await listen2asmr.App.PCApplication.AcquireTokenSilentAsync(
        AuthenticationInfo.Scopes, string.Empty, AuthenticationInfo.Authority, 
        AuthenticationInfo.SignUpSignInpolicy, false); 

       AuthenticationInfo.UserAuthentication = ar; 
      } catch { 

      } 
     } 

這是在引導程序:

protected override Xamarin.Forms.Page CreateMainPage() 
     { 
      return Container.Resolve<LoginPage>(); 
     } 

     protected override void RegisterTypes() 
     { 
      Container.RegisterTypeForNavigation<LoginPage>(); 
     } 

OnNavigatedTo似乎永遠不會被調用。還有其他方法我應該使用,還是我錯過了其他的東西?我唯一能想到的另一件事是從ViewModel構造函數中調用OnNavigatedTo中的代碼,但async/await對構造函數起作用。

+0

我目前正在做一些非常相似。你提出的解決方案是否工作?我說這是因爲我遇到了一個問題,它理解我將如何將邏輯複製到視圖模型中? – whiskeycoder

回答

1

這已經在Xamarin.Forms的Prism的最新預覽版中修復。嘗試使用這些包來代替:

https://www.nuget.org/packages/Prism.Forms/6.1.0-pre4 https://www.nuget.org/packages/Prism.Unity.Forms/6.2.0-pre4

而且引導過程發生了變化。閱讀以瞭解更多信息:

0

我的建議是使用查看視圖模型觸發事件。

比如:

View.xaml.cs

protected override async void OnAppearing() { 
    base.OnAppearing(); 
    viewModel.OnAppearing(); 
} 

async void OnSignUpSignIn(object sender, EventArgs e) { 
    viewModel.OnSignUpSignIn(sender, e); 
} 

ViewModel.cs

protected override async void OnAppearing() { 
    App.PCApplication.PlatformParameters = platformParameters; 

    try { 
     var ar = await App.PCApplication.AcquireTokenSilentAsync(
      AuthenticationInfo.Scopes, string.Empty, 
      AuthenticationInfo.Authority, 
      AuthenticationInfo.SignUpSignInpolicy, false); 

     AuthenticationInfo.UserAuthentication = ar; 
    } catch {    
    } 
} 

async void OnSignUpSignIn(object sender, EventArgs e) { 
    try { 
     var ar = await App.PCApplication.AcquireTokenAsync(
      AuthenticationInfo.Scopes, string.Empty, 
      UiOptions.SelectAccount, 
      string.Empty, null, AuthenticationInfo.Authority, 
      AuthenticationInfo.SignUpSignInpolicy); 

     AuthenticationInfo.UserAuthentication = ar; 
    } catch (Exception ex) { 
     if (ex != null) { 
     }    
    } 
} 

原因:

  1. 觀應只涉及視覺效果,和該您的網頁收到的活動。邏輯應該被轉發到ViewModel,除非它處理信息的表示(例如,對於2個選擇使用切換框的邏輯,而對於3+的組合框使用切換框)。

  2. 幾乎相反,ViewModel應跟蹤「模型狀態」(例如,用戶仍然需要輸入他們的付款信息),而不是「查看狀態」(例如,用戶已導航到付款頁)。