我試圖在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對構造函數起作用。
我目前正在做一些非常相似。你提出的解決方案是否工作?我說這是因爲我遇到了一個問題,它理解我將如何將邏輯複製到視圖模型中? – whiskeycoder