Android DataBinding-如何绑定ViewGroup的属性

时间:2019-04-29 07:17:15

标签: c# android xamarin.android mvvm-light android-databinding

我正在Xamarin.Android(C#)中开发一个Android应用程序。但是,我确实认为任何Java开发人员都可以回答这个问题。

我是android开发的新手。无论如何,我创建了一个片段,里面只有一个LinearLayout和一个TextView。当我为它创建背景类时,我不是从extend类继承(以Java的话Fragment继承),而是从LinearLayout类继承。

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

public class MyFragment : LinearLayout

相当于JAVA的

public class MyFragment extends LinearLayout

(P.S。我对JAVA的了解有限,它是语法)。

无论如何,一切正常。我有一个Initialize方法(在JAVA中,它应该是Init方法),可以扩大片段的视图。从视图中,它尝试查找具有给定TextView的{​​{1}}。

所以,代码看起来像这样:

Id

到目前为止,一切都很好。然后,我继续使用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); } 库来实现MVVM模式。我创建一个MVVMLight,如下所示:

ViewModel

仍然,一切都很好。当我尝试将 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"; }); } } 的text属性绑定到TextView的{​​{1}}属性时,问题开始了,如下所示:

ViewModel
  

注意:GoalTitle来自 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)); } 命名空间。

我将片段添加到主视图(我的意思是Binding的视图)中,然后调试应用程序。执行后,出现以下错误:

  

无法将Java类型'md55bfae9a06327fa0fdf207b4f768604b1 / MyFragment'的Java类型的JNI句柄0xfff02a68(key_handle 0x339790a)激活为托管类型'TestApp.MyFragment'。

搜索google时,我意识到我试图在创建视图之前就绑定属性(如果我错了,请更正我)。我在其他SO答案中发现的建议是将代码放入GalaSoft.MvvmLight.Helpers方法中,或者以某种方式延迟了绑定部分的代码的执行。

第一个解决方案对我不起作用,因为MainActivity又名OnCreateView没有这样的方法LinearLayout我可以覆盖。

那么,我该如何将View绑定到OnCreateView呢?而且,在将片段作为TextView继承时,我是否处于正确的轨道上?

1 个答案:

答案 0 :(得分:1)

我不熟悉MVVMLIght扩展,但是如果您按预期使用一个片段(即,在布局中),则应该从这样的片段继承(这是v4支持片段):

public class CategoryFragment : SupportFragment {
RecyclerView _recyclerView;
private View _view;

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    _view = inflater.Inflate (Resource.Layout._CategoryLayout, container, false);

    // Get our RecyclerView layout:
    _recyclerView = _view.FindViewById<RecyclerView> (Resource.Id.categoryRecyclerView);

    // Instantiate the layout manager
    var linearLayoutManager = new LinearLayoutManager (Context, LinearLayoutManager.Vertical, false);
    _recyclerView.SetLayoutManager (linearLayoutManager);

    // Instantiate the adapter and pass in its data source:
    _adapter = new CategoryAdapter (_categories);
    //Register the item click handler with the adapter:
    _adapter.ItemClick += OnItemClick;
    // Plug the adapter into the RecyclerView:
    _recyclerView.SetAdapter (_adapter);
    return _view;
}

}