Android DataBinding - 如何绑定 ViewGroup 的属性



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

我是安卓开发的新手。无论如何,我创建了一个片段,里面只有一个LinearLayout和一个TextView。当我为它创建后台类时,我不是从Fragment类继承(在 JAVA 的话中,extend)它,而是从LinearLayout类继承它。

因此,MyFragment.cs文件是这样开始的:

public class MyFragment : LinearLayout

JAVA 等价物将是

public class MyFragment extends LinearLayout

(附言我对 JAVA 及其 sytax 的了解有限)。

无论如何,一切正常。我有一个Initialize方法(在 JAVA 中,它应该是Init方法),它夸大了片段的视图。从视图中,它尝试查找具有给定IdTextView

因此,代码如下所示:

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);
}

到目前为止,一切运作良好。然后,我继续使用MVVMLight库实现 MVVM 模式。我创建一个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";
});
}
}

不过,一切都很好。当我尝试将TextView的文本属性绑定到ViewModelGoalTitle属性时,问题开始了,如下所示:

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来自GalaSoft.MvvmLight.Helpers命名空间。

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

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

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

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

那么,我应该如何将TextView绑定到ViewModel呢?而且,我是否在将片段视为LinearLayout,因为我从它继承了它?

我不熟悉 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;
}

}

最新更新