C# Android数据绑定-如何绑定视图组';s属性

C# Android数据绑定-如何绑定视图组';s属性,c#,android,xamarin.android,mvvm-light,android-databinding,C#,Android,Xamarin.android,Mvvm Light,Android Databinding,我正在Xamarin.Android(C#)中开发一个Android应用程序。然而,我确实觉得这个问题也可以由任何Java开发人员来回答 我是android开发的新手。总之,我创建了一个片段,里面只有一个LinearLayout和一个TextView。当我为它创建后台类时,我不会(用JAVA的话说,extend)从Fragment类继承它,而是从LinearLayout类继承它 因此,MyFragment.cs文件的开头如下: public class MyFragment : LinearLa

我正在Xamarin.Android(C#)中开发一个Android应用程序。然而,我确实觉得这个问题也可以由任何Java开发人员来回答

我是android开发的新手。总之,我创建了一个片段,里面只有一个
LinearLayout
和一个
TextView
。当我为它创建后台类时,我不会(用JAVA的话说,
extend
)从
Fragment
类继承它,而是从
LinearLayout
类继承它

因此,
MyFragment.cs
文件的开头如下:

public class MyFragment : LinearLayout
public class MyFragment : LinearLayout
{
 Context mContext;
  private void Initialize(Context ctx)
  {
        //Inflating the layout
        mContext = ctx;
        var inflatorService = (LayoutInflater)ctx.GetSystemService(Context.LayoutInflaterService);
        View v = inflatorService.Inflate(Resource.Layout.MyFragmentView, this, false);
        this.AddView(v);
        GoalHeader = v.FindViewById<TextView>(Resource.Id.GoalHeader);
  }
JAVA的等价物是

public class MyFragment extends LinearLayout
(另外,我对JAVA及其Sytax的了解有限)

不管怎样,一切都很好。我有一个
Initialize
方法(在JAVA中,它应该是
Init
方法),用于放大片段的视图。从视图中,它尝试查找具有给定
Id
TextView

代码如下所示:

public class MyFragment : LinearLayout
public class MyFragment : LinearLayout
{
 Context mContext;
  private void Initialize(Context ctx)
  {
        //Inflating the layout
        mContext = ctx;
        var inflatorService = (LayoutInflater)ctx.GetSystemService(Context.LayoutInflaterService);
        View v = inflatorService.Inflate(Resource.Layout.MyFragmentView, this, false);
        this.AddView(v);
        GoalHeader = v.FindViewById<TextView>(Resource.Id.GoalHeader);
  }
不过,一切都很好。当我尝试将
TextView
的text属性绑定到
ViewModel
GoalTitle
属性时,问题就出现了,如下所示:

 public class Vm_MyFragment : ViewModelBase
{
    private string _goaltitle = "";
    public string GoalTitle
    {
        get { return _goaltitle; }
        set { Set(ref _goaltitle, value); }
    }
    public void SetTest()
    {
        DispatcherHelper.CheckBeginInvokeOnUI(() =>
        {
            GoalTitle = "Test";
        });
    }
}
 private readonly List<Binding> _bindings = new List<Binding>();

 private void Initialize(Context ctx)
    {
        //Inflating the layout
        mContext = ctx;
        var inflatorService = (LayoutInflater)ctx.GetSystemService(Context.LayoutInflaterService);
        View v = inflatorService.Inflate(Resource.Layout.MyFragmentView, this, false);
        this.AddView(v);
        Vm_MyFragmentView viewmodel = new Vm_MyFragmentView();
        GoalHeader = v.FindViewById<TextView>(Resource.Id.GoalHeader);
        _bindings.Add(
            this.SetBinding(
                () => mainViewModel.GoalTitle,
                () => GoalHeader.Text));
    }
private readonly List_bindings=new List();
私有无效初始化(上下文ctx)
{
//扩大布局
mContext=ctx;
var inflatorService=(LayoutInflater)ctx.GetSystemService(Context.LayoutInflaterService);
视图v=充气器服务。充气(Resource.Layout.MyFragmentView,this,false);
这是AddView(v);
Vm_MyFragmentView视图模型=新Vm_MyFragmentView();
GoalHeader=v.findviewbyd(Resource.Id.GoalHeader);
_绑定。添加(
这是挫折(
()=>mainViewModel.GoalTitle,
()=>GoalHeader.Text));
}
注意:
Binding
来自
GalaSoft.MvvmLight.Helpers
命名空间

我在主视图(我的意思是,
MainActivity
的视图)中添加片段并调试应用程序。执行时,我得到以下错误:

无法将Java类型为“md55bfae9a06327fa0fdf207b4f768604b1/MyFragment”的JNI句柄0xfff02a68(密钥句柄0x339790a)激活为托管类型“TestApp.MyFragment”

搜索google时,我意识到我正在试图在视图创建之前绑定属性(如果我错了,请纠正我)。我在其他SO答案中找到的建议是要么将代码放在
OnCreateView
方法中,要么以某种方式延迟绑定部分代码的执行

第一个解决方案对我不起作用,因为
LinearLayout
aka
View
没有我可以覆盖的方法
OnCreateView


那么,我应该如何将
TextView
绑定到
ViewModel
?另外,我是否正确地将片段视为继承自它的
LinearLayout

我不熟悉MVVMLIght扩展,但如果您使用的是一个应该使用的片段(即,在tablayout中),您应该继承自这样的片段(这是一个v4支持片段):

公共类CategoryFragment:SupportFragment{
回收视图(RecyclerView);
私有视图(u视图),;
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态){
_视图=充气机。充气(Resource.Layout.\u CategoryLayout,container,false);
//获取我们的RecyclerView布局:
_RecycleView=\u view.FindViewById(Resource.Id.categoryRecycleView);
//实例化布局管理器
var linearLayoutManager=新的linearLayoutManager(上下文,linearLayoutManager.Vertical,false);
_recyclerView.SetLayoutManager(linearLayoutManager);
//实例化适配器并传入其数据源:
_适配器=新类别适配器(_类别);
//向适配器注册项目单击处理程序:
_adapter.ItemClick+=OnItemClick;
//将适配器插入RecyclerView:
_recyclerView.SetAdapter(_adapter);
返回视图;
}

}

我可以这样做,但它不能解决我的主要问题。无论如何,指出这一点做得很好+1是否调试出现错误的行?我认为这个错误可能在这里
View v=inflatorService.Inflate(Resource.Layout.MyFragmentView,this,false)错误出现在
\u绑定上。添加
行。