我有很多片段,在布局中有90%的常见。因为我非常喜欢代码布局(我再也不会为iOS创建XIB),我想:让我们为Android做同样的事情。这并不像我想象的那么容易。麻烦在于MvvmCross,在网上搜索解决方案之后,只能找到指向解决方案的提示。
问题:当显示片段并执行绑定时,我得到所有消息无法找到源属性,因为它尝试绑定到空对象。
Unable to bind: source property source not found Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on null-object
布局显示在屏幕上,ViewModel已设置,bindingcontext已设置(均由我自己创建,因为这不是自动完成的。)
我怀疑在通货膨胀中做了一些事情来“注册”创建的控件,而且因为我不使用通货膨胀,所以他们没有注册。我读了关于MvxBindingContextStackRegistration的其他问题,看了它的来源。 第二个原因可能是bindingcontext本身:不知怎的,它不是自动处理的,并指出了必须缺少的东西,在下面的代码中调用了。
我可能做了一些奇怪的事情,因为这是让它发挥作用的最终尝试:
public class IndexTocFragment : MvxFragment
{
// two statics to save on typing
public static int WRAP = ViewGroup.LayoutParams.WrapContent;
public static int FILL = ViewGroup.LayoutParams.FillParent;
public LinearLayout NewVerticalLinearLayout(Orientation orientation, Boolean fillparent) {
var ll = new LinearLayout (Activity) { Orientation = orientation };
ll.LayoutParameters = new ViewGroup.LayoutParams (FILL, fillparent ? FILL : WRAP);
return ll;
}
public new IndexTocViewModel ViewModel { get { return base.ViewModel as IndexTocViewModel; } set { base.ViewModel = value; } }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var layout = NewVerticalLinearLayout (Orientation.Vertical, true);
if (ViewModel == null)
ViewModel = Mvx.IocConstruct<IndexTocViewModel> ();
BindingContext = new MvxAndroidBindingContext (Activity, new MvxSimpleLayoutInflater(inflater));
using (new MvxBindingContextStackRegistration<IMvxAndroidBindingContext>(((IMvxAndroidBindingContext) this.BindingContext)))
{
//var layout = NewVerticalLinearLayout (Orientation.Vertical, true);
layout.SetBackgroundColor (Color.ParseColor("#FFFFFF"));
var titlelayer = NewVerticalLinearLayout (Orientation.Horizontal, false);
titlelayer.LayoutParameters.Height = 40;
var title = new TextView (Activity) { LayoutParameters = new ViewGroup.LayoutParams (FILL, FILL) };
title.SetTextColor (Color.ParseColor ("#212121"));
var titlebutton = new Button (Activity) { LayoutParameters = new ViewGroup.LayoutParams (WRAP, FILL) };
titlelayer.AddView (title);
titlelayer.AddView (titlebutton);
layout.AddView (titlelayer);
var twobuttonlayer = NewVerticalLinearLayout (Orientation.Horizontal, false);
twobuttonlayer.LayoutParameters.Height = 40;
var twobuttonklein = new Button (Activity) { LayoutParameters = new ViewGroup.LayoutParams (FILL, WRAP) };
twobuttonklein.Text = "Klein";
twobuttonklein.SetBackgroundResource (Resource.Drawable.kleingrootbutton);
var twobuttongroot = new Button(Activity){ LayoutParameters = new ViewGroup.LayoutParams (FILL, WRAP) };
twobuttongroot.Text = "Middel/groot";
twobuttongroot.SetBackgroundResource (Resource.Drawable.kleingrootbutton);
twobuttonlayer.AddView (twobuttonklein);
twobuttonlayer.AddView (twobuttongroot);
layout.AddView (twobuttonlayer);
var zoekvak = new EditText (Activity) { LayoutParameters = new ViewGroup.LayoutParams (FILL, WRAP) };
zoekvak.Hint = "Zoek in inhoudsopgave";
layout.AddView (zoekvak);
var emptytext = new TextView (Activity) { LayoutParameters = new ViewGroup.LayoutParams (FILL, WRAP),
Text = "Er zijn geen resultaten." };
emptytext.SetBackgroundColor (Color.ParseColor ("#BBAA00"));
layout.AddView (emptytext);
var listadapter = new BindableExpandableListAdapter(this.Activity, (IMvxBindingContext) BindingContext);
var list = new BindableExpandableListView (Activity, null, listadapter) { LayoutParameters = new ViewGroup.LayoutParams (FILL, FILL) };
list.SetMinimumHeight (50);
list.ItemTemplateId = Resource.Layout.indextocsectionlistitem;
list.GroupTemplateId = Resource.Layout.indextocitem;
list.SetGroupIndicator (null);
list.SetBackgroundColor (Color.ParseColor ("#AAAA00"));
layout.AddView (list);
this.DoBind += () => {
var bs = this.CreateBindingSet<IndexTocFragment, IndexTocViewModel> ();
if (ViewModel == null) {
Console.WriteLine ("ERROR ViewModel not set");
}
if (BindingContext == null) {
Console.WriteLine ("ERROR BindingContext not set");
}
bs.Bind (title).For (x => x.Text).To (vm => vm.IndexTitle);
// the following title displays some data from the ViewMode, and that works OK.
Console.WriteLine("Index title value: "+ViewModel.IndexTitle);
bs.Bind (twobuttongroot).For ("Click").To (vm => vm.SwitchKleinGrootCommand);
bs.Bind (twobuttonklein).For ("Click").To (vm => vm.SwitchKleinGrootCommand);
bs.Bind (zoekvak).For (x => x.Text).To (vm => vm.SearchText);
bs.Bind (emptytext).For (x => x.Text).To (vm => vm.TextForEmptyList);
bs.Bind (listadapter).For(x => x.ItemsSource).To(vm => vm.Chapters);
bs.Apply ();
};
// the next binding was never called
//this.DelayBind (() => {
//
//});
}
//var view = this.BindingInflate(Resource.Layout.IndexTocView, null);
Console.WriteLine ("LAYOUT RETURNED");
return layout;
}
private Action DoBind;
public override void OnResume()
{
// doing the binding here to make sure everything should have been set,
but it is of course not the logical location
base.OnResume();
Console.WriteLine ("RESUMING");
if (DoBind != null)
DoBind ();
}
}
现在我将创建一个片段布局,其中包含我需要的所有元素,并隐藏不需要的元素,这些元素可能会起作用。但我希望我可以使用代码创建视图。
感谢您的帮助。
BTW:活动和演示者的代码是从MvvmCross中的片段示例中获取的自定义演示者代码。 在Android代码中绑定:MVVMCross for android - how to do binding in code? 在堆栈上推送上下文:MvvmCross: How to programmatically construct an MvxListView with custom adapter?
答案 0 :(得分:1)
解决
叹息。这不是第一次发生这种情况几分钟后,经过数小时的挫折后终于在这里问了一下......
解决了它(至少它现在正在运作):
BindingContext = new MvxAndroidBindingContext (Activity, new MvxSimpleLayoutInflater(inflater), ViewModel);
在我的代码中没有提供最后一个参数。所以可能绑定上下文没有完全设置。
在MvvmCross代码中,我发现了一些警告:EnsureBindingContext无效...
现在测试delaybind是否再次运行......