如何从Xamarin Android Activity中调用MvxViewModel?

问题描述 投票:1回答:1

如何从我的xamarin android MvxViewModel课程中调用Activity?在我的情况下,我不能像local: MvxBind=" Click MethodFromMvxViewModel"那样去MvxViewModel

所以我的情况是:

我有Facebook登录。在我的活动中,我尝试登录时有两个选项。如果用户通过他的Facebook帐户发送电子邮件,我会收到一些关于用户的信息,并希望将其保存到数据库中,如果用户没有Facebook帐户中的电子邮件,我会打开新的内容视图与表单填写电子邮件,然后按保存按钮,按下保存按钮后我需要将用户保存到数据库。

活动代码:

[Activity(Label = "Facebook login")]
class FacebookLoginView : MvxActivity, IFacebookCallback, GraphRequest.IGraphJSONObjectCallback
{
    private LoginButton loginButton;
    private ICallbackManager callbackManager;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        RequestWindowFeature(WindowFeatures.NoTitle);
        SetContentView(Resource.Layout.FacebookLoginView);

        loginButton = FindViewById<LoginButton>(Resource.Id.loginButton1);
        loginButton.SetReadPermissions(new string[] { "user_friends", "email" });
        callbackManager = CallbackManagerFactory.Create();
        loginButton.RegisterCallback(callbackManager, this);
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        callbackManager.OnActivityResult(requestCode, (int)resultCode, data);
    }

    public void OnCancel()
    {
        //Does nothing, just closes facebook login screen
    }

    public void OnError(FacebookException error)
    {
        //For now does nothing
    }

    public void OnSuccess(Java.Lang.Object result)
    {
        var loginresult = result as LoginResult;

        GraphRequest graphRequest = GraphRequest.NewMeRequest(loginresult.AccessToken, this);
        graphRequest.Parameters.PutString("fields", "id,name,email");
        graphRequest.ExecuteAsync();

        SetContentView(Resource.Layout.SlideListView);
    }

    public void OnCompleted(JSONObject @object, GraphResponse response)
    {
        var facebookId = (long)@object.Get("id");
        var email = @object.Get("email").ToString();

        if (string.IsNullOrEmpty(email))
        {               
            SetContentView(Resource.Layout.CompleteRegistrationView);
            //GetEmail from form
        }
        //save user
    }
}

在最后一个方法OnCompleted我想打电话给MvxViewModel并给他发电子邮件和facebookId。我怎样才能从Activity那里做到这一点?

android xamarin mvvm xamarin.android mvvmcross
1个回答
1
投票

如果您使用通用MvxActivity<MyViewModel>,您将能够在ViewModel上调用任何方法。

public void OnCompleted(JSONObject @object, GraphResponse response)
{
    var facebookId = (long)@object.Get("id");
    var email = @object.Get("email").ToString();

    if (string.IsNullOrEmpty(email))
    {               
        SetContentView(Resource.Layout.CompleteRegistrationView);
        //GetEmail from form
    }
    //save user
    ViewModel.SaveUserMethod(object);
}
© www.soinside.com 2019 - 2024. All rights reserved.