Xamarin Android MvvmCross MvxFragment不会调用OnActivityResult

时间:2018-03-23 09:10:35

标签: xamarin xamarin.android mvvmcross

所以我有这段代码:

[MvxFragmentPresentation(typeof(MainViewModel), Resource.Id.content_frame, false)]
internal class FacebookLoginView : BaseFragment<FacebookLoginViewModel>, IFacebookCallback
{
    protected override int FragmentId => Resource.Layout.FacebookLoginView;
    private ICallbackManager _callbackManager;
    private LoginButton _loginButton;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = base.OnCreateView(inflater, container, savedInstanceState);
        ParentActivity.SupportActionBar.Title = "Login";

        var accessToken = AccessToken.CurrentAccessToken;

        if (accessToken != null)
        {
            (ViewModel).FinishLogin(accessToken.Token, long.Parse(accessToken.UserId)).GetAwaiter().GetResult();
        }
        else
        {
            _loginButton = view.FindViewById<LoginButton>(Resource.Id.loginButton1);
            _loginButton.SetReadPermissions(new string[] { "user_friends", "email", "pages_show_list" });
            _callbackManager = CallbackManagerFactory.Create();
            _loginButton.RegisterCallback(_callbackManager, this);
        }

        return view;
    }

    public override void OnActivityResult(int requestCode, int resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        _callbackManager.OnActivityResult(requestCode, resultCode, data);
    }

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

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

    public void OnSuccess(Object result)
    {
        var loginResult = result as LoginResult;
        (ViewModel).FinishLogin(loginResult.AccessToken.Token, long.Parse(loginResult.AccessToken.UserId)).GetAwaiter().GetResult();
    }
}

这只是一个简单的Facebook登录。问题是,当我点击登录按钮时,会弹出Facebook屏幕,然后输入我的用户名和密码,然后点击登录。在Facebook登录屏幕关闭后没有任何反应。

我在OnActivityResultOnCancelOnErrorOnSuccess方法上放置了断点,并且它们都没有被调用。

问题是我正在尝试将我的xamarin android应用程序更新为片段,而且我是新手。当FacebookLoginView继承MvxActivity一切正常时,我认为它应该在片段上工作相同。

可以请别人解释一下我缺少的东西吗?

更新1 这不是另一个问题的重复。我尝试过,就像@ cheesebaron提供的链接答案一样,但它没有用......在我的MainView中我添加了:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    var fragment = FragmentManager.FindFragmentByTag("facebook_fragment");
    fragment.OnActivityResult(requestCode, resultCode, data);
}

为了使它工作,在我的FacebookLoginView中我不得不重写OnStart方法,添加片段:

public override void OnStart()
{
    base.OnStart();

    if (FragmentManager.FindFragmentByTag("facebook_fragment") == null)
    {
        var transaction = FragmentManager.BeginTransaction();
        transaction.Add(this, "facebook_fragment");
        transaction.Commit();
    }    
}

现在,当我尝试登录时,当app尝试按标签查找片段时,我在MainView中获得了一个引用null异常,并且它始终为null。所以问题仍然存在。

0 个答案:

没有答案