如何从Xamarin Andoid Activity调用MvxViewModel?

时间:2017-09-13 10:27:09

标签: android xamarin mvvm xamarin.android mvvmcross

如何从我的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那样做?

1 个答案:

答案 0 :(得分:1)

如果您使用通用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); } ,则可以在ViewModel上调用任何方法。

query = "select a,b from abc";            
SqlCommand cmd = new SqlCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
GridDanish.DataSource = dt;
GridDanish.DataBind();