MvvmCross:切换标签时出现异常

时间:2015-01-06 10:46:33

标签: android xamarin xamarin.android mvvmcross

我正在使用片段作为标签。切换选项卡时出现以下错误

指定的孩子已经有父母。您必须首先在孩子的父母身上调用removeView()。

错误发生在函数

public override void OnTabChanged(string tag)
{
    base.OnTabChanged(tag);           
}

我在阅读了类似的SO问题(https://stackoverflow.com/q/24741915/1421015)之后发布了这个问题,但没有帮助。一般的答案似乎是我们需要清除父母的观点。然而,自从我完成任何mvvmx以来已经过去了一年,我们将非常感谢任何帮助。这是大约一年前代码的状态,我希望它能够正常运行。视图类在下面,我在android上使用mvvmx v3.1.1。

[Activity(Label = "Manage Articles")]
internal class ManageArticles
    : BaseTabsFragmentActivity<ManageArticlesViewModel>
{
    public ManageArticles() 
        : base(Resource.Layout.ManageArticles, Resource.Id.actualtabcontent)
    {
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        var set = this.CreateBindingSet<ManageArticles, ManageArticlesViewModel>();
        set.Bind(this).For(v => v.ChangeDisplay).To(vm => vm.DisplayChange);
        set.Apply();             
    }

    protected override void AddTabs(Bundle args)
    {
        AddTab<ManageArticlesListTab>("Tab1", "Listing", args, TabViewModel.ListVM);
        AddTab<ManageArticlesEditTab>("Tab2", "Editor", args, TabViewModel.SelectedVM);            
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.managearticles, menu);
        return base.OnCreateOptionsMenu(menu);
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Resource.Id.managearticles__save:
                // TODO
                return true;
        }

        return base.OnOptionsItemSelected(item);
    }

    public override void OnTabChanged(string tag)
    {
        base.OnTabChanged(tag);           
    }

    public override void OnTabFragmentChanging(string tag, FragmentTransaction transaction)
    {
        base.OnTabFragmentChanging(tag, transaction);
    }


    private IDisposable _subscription;
    private IMvxInteraction<Display> _displayInteraction;
    public IMvxInteraction<Display> ChangeDisplay
    {
        get
        {                
            return _displayInteraction;
        }
        set
        {
            if (_subscription != null)
            {
                _subscription.Dispose();
                _subscription = null;
            }
            _displayInteraction = value;
            if (_displayInteraction != null)
            {
                _subscription = _displayInteraction.WeakSubscribe(DoDisplayChange);
            }
        }
    }


    private void DoDisplayChange(Display display)
    {
        var tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);

        switch (display)
        {
            case Display.All:
                tabHost.SetCurrentTabByTag("Tab1");
                break;

            case Display.Selected:
                tabHost.SetCurrentTabByTag("Tab2");
                break;
        }
    }
}

0 个答案:

没有答案