OnNavigatedTo在ViewModel中,当页面首次加载时不会触发

时间:2016-04-07 02:22:55

标签: xamarin prism

我试图在Xamarin.Forms中实现Azure Active Directory B2C。如果我只是复制他们的例子,我可以毫无问题地让它工作。但是当我尝试使用Prism时,我遇到了问题。

我把这个代码放在了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) {
        }               
    }
}

并将其移至ViewModel的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 {

            }
        }

这是在Bootstrapper中:

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

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

OnNavigatedTo似乎永远不会被召唤。我应该使用其他一些方法,还是我错过了其他什么?我能想到的另一件事是从ViewModel构造函数中调用OnNavigatedTo中的代码,但是async / await可以与构造函数一起使用。

2 个答案:

答案 0 :(得分:1)

这已在最新预览版Prism for Xamarin.Forms中修复。请尝试使用这些软件包:

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

自举过程也发生了变化。阅读本文以获取更多信息:

答案 1 :(得分:0)

我的建议是将您的查看事件用作 ViewModel 触发器

For Instance:

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应该跟踪“模型状态”(例如,用户仍然需要输入他们的付款信息),而不是“查看状态”(例如,用户已导航到付款页面)。