首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当第一次加载页面时,ViewModel中的ViewModel不会触发

当第一次加载页面时,ViewModel中的ViewModel不会触发
EN

Stack Overflow用户
提问于 2016-04-07 02:22:54
回答 2查看 1.3K关注 0票数 0

我正在尝试在Azure B2C中实现Xamarin.Forms。如果我只是照搬他们的例子,我就能使它没有问题地发挥作用。但是当我尝试使用棱镜时,我会遇到一些问题。

我把这段代码放在XAML的代码后面:

代码语言:javascript
运行
复制
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中:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
protected override Xamarin.Forms.Page CreateMainPage ()
        {
            return Container.Resolve<LoginPage> ();
        }

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

不过,OnNavigatedTo似乎从来没有接到过电话。我是否应该使用其他的方法,或者我错过了其他的方法?我唯一能想到的另一件事是从OnNavigatedTo构造函数调用ViewModel中的代码,但是异步/等待确实与构造函数一起工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-07 13:48:04

这已经修复在最新的预览版本的棱镜为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

此外,引导过程也发生了变化。有关更多信息,请阅读此链接:

  • Prism.Forms 5.7.0预览- http://brianlagunas.com/first-look-at-the-prism-for-xamarin-forms-preview/
  • Prism.Forms 6.2.0预览- http://brianlagunas.com/prism-for-xamarin-forms-6-2-0-preview/
  • Prism.Forms 6.2.0预览3- http://brianlagunas.com/prism-for-xamarin-forms-6-2-0-preview-3/
  • 预览4即将到来
票数 1
EN

Stack Overflow用户

发布于 2016-04-07 08:22:05

我的建议是使用您的视图事件作为触发器用于ViewModel。

例如:

View.xaml.cs

代码语言:javascript
运行
复制
protected override async void OnAppearing () {
    base.OnAppearing ();
    viewModel.OnAppearing();
}

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

ViewModel.cs

代码语言:javascript
运行
复制
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,除非它处理信息的表示(例如,对于两个选择使用切换框的逻辑,而对于3+使用一个组合框)。
  2. 反之亦然,ViewModel应该跟踪“模型状态”(例如。用户仍然需要输入他们的支付信息),而不是“查看状态”(例如。用户已导航到支付页面)。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36465611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档