gpt4 book ai didi

android - 如何从 Xamarin Android Activity 调用 MvxViewModel?

转载 作者:行者123 更新时间:2023-11-30 00:21:33 26 4
gpt4 key购买 nike

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

所以我的情况是:

我有 Facebook 登录名。在我的 Activity 中,我在尝试登录时有两个选项。如果用户在他的 Facebook 帐户中发送了电子邮件,我会得到一些关于用户的信息并想将其保存到数据库中,如果用户在 Facebook 帐户中没有电子邮件,我会打开新的带有表单的内容 View 填写电子邮件并按下保存按钮,按下保存按钮后我需要将用户保存到数据库。

Activity 代码:

[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 并向他发送 emailfacebookId。我怎样才能从 Activity 做到这一点?

最佳答案

如果您使用通用 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);
}

关于android - 如何从 Xamarin Android Activity 调用 MvxViewModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46195075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com