gpt4 book ai didi

安卓 fragment : View fully from code causes binding to null-object

转载 作者:行者123 更新时间:2023-11-30 03:22:49 27 4
gpt4 key购买 nike

我有很多 fragment ,它们在布局中有 90% 是常见的。因为我真的很喜欢代码布局(我再也没有为 iOS 创建 XIB),所以我想:让我们为 Android 做同样的事情。这并不像我想象的那么容易。问题出在MvvmCross,在网上搜索解决方案后,只能找到指向解决方案的提示。

问题:当显示 fragment 并执行绑定(bind)时,我收到所有无法找到源属性的消息,因为它试图绑定(bind)到空对象。

Unable to bind: source property source not found Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on null-object

布局显示在屏幕上,设置了 ViewModel,设置了绑定(bind)上下文(都是我自己创建的,因为这不是自动完成的)。

我怀疑在 inflation 中做了一些事情来“注册”创建的控件,并且因为我不使用 inflation,所以它们没有注册。我阅读了有关 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 ();

}



}

现在我将创建一个包含我需要的一切的 fragment 布局,并隐藏不需要的元素,这可能会起作用。但我希望我可以使用从代码创建 View 。

感谢您的帮助。

顺便说一句: Activity 和演示者的代码是从 MvvmCross 中的 fragment 示例中获取的自定义演示者代码。在 Android 代码中绑定(bind):MVVMCross for android - how to do binding in code?将上下文插入堆栈:MvvmCross: How to programmatically construct an MvxListView with custom adapter?

最佳答案

已解决

叹息。经过数小时的挫折,终于在这里问了几分钟后,这已经不是第一次发生了。

解决了(至少现在可以用了):

        BindingContext = new MvxAndroidBindingContext (Activity, new MvxSimpleLayoutInflater(inflater), ViewModel);

在我的代码中没有提供最后一个参数。所以可能绑定(bind)上下文没有完全设置。

在 MvvmCross 代码中,我发现了一些警告,指出 EnsureBindingContext 没有工作......

现在测试延迟绑定(bind)是否再次工作......

关于安卓 fragment : View fully from code causes binding to null-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com