点击Fragment for MVVMCROSS中的事件

时间:2016-01-27 06:03:34

标签: android xamarin xamarin.android mvvmcross

我有一个片段类,就像这个:

public class TMSAccountFragment : MvxFragment
{
    //some code
}

按钮点击事件:

#region OnClick of account button

ImageView acc_Button = view.FindViewById<ImageView>(Resource.Id.accountbutton_orange);
acc_Button.Click += delegate
{
    OnClick();
};

#endregion

public void OnClick()
{
    FragmentTransaction ft;
    Fragment frag;

    frag = new TMSSlidingTabsFragment();
    ft = FragmentManager.BeginTransaction();
    ft.Replace(Resource.Id.sample_content_fragment, frag);
    ft.AddToBackStack(null);
    ft.Commit();
}

FragmentManager.BeingTransaction();中发生以下错误。

  

无法隐式转换类型'android.support.v4.App.FragmentTransaction' to 'Android.App.FragmentTransaction'

2 个答案:

答案 0 :(得分:0)

使用getSupportFragmentManager()代替getFragmentManager()。你应该改变你的onclick,如下所示

public void OnClick()
 {
 Fragment frag = null;
 FragmentManager fragmentManager = getSupportFragmentManager();   
 FragmentTransaction fragmentTransaction =   fragmentManager.beginTransaction();
 frag = new TMSSlidingTabsFragment();
 fragmentTransaction .Replace(Resource.Id.sample_content_fragment, frag);         
 fragmentTransaction .AddToBackStack(null);
 fragmentTransaction .Commit();
 }

答案 1 :(得分:0)

您需要使用SupportFragmentManager代替常规FragmentManager

public void OnClick()
{
    var fragment = new TMSSlidingTabsFragment();
    var transaction = SupportFragmentManager.BeginTransaction();
    transaction.Replace(Resource.Id.sample_content_fragment, frag);
    transaction.AddToBackStack(null);
    transaction.Commit();
}

但是,由于您正在使用MVVMCross,我强烈建议您使用MVVM模式。也就是说,将此Click事件绑定到VM中的IMvxCommand并使用适当的演示者来处理片段的呈现方式(可以找到一些有用的入门资源herehere