基本上我正试图在scrollView的内容中添加布局。我做了一个示例代码,但它只在第二次单击时工作,由于某种原因它不会渲染第一个布局!我做错了吗?
public class PageExperiment
{
ScrollView _page;
public PageExperiment()
{
_page = new ScrollView { Orientation = ScrollOrientation.Vertical,
Content = new RelativeLayout { BackgroundColor = Color.White} };
var button = new Button { Text="Add Button"};
button.Clicked += OnButtonClicked;
((RelativeLayout)_page.Content).Children.Add(button,xConstraint:null);
}
public ScrollView getPage()
{
return _page;
}
void OnButtonClicked(object sender, EventArgs e)
{
var relative = AddLayout();
var content = (RelativeLayout)_page.Content;
var v = content.Children[content.Children.Count - 1];
Constraint xConstraint = Constraint.Constant(0);
Constraint yConstraint = Constraint.RelativeToView(v, (parent, sibling) => {
return sibling.Y + sibling.Height;
});
((RelativeLayout)_page.Content).Children.Add((View)relative, xConstraint, yConstraint);
}
public VisualElement AddLayout()
{
var root = new RelativeLayout();
root.BackgroundColor = Color.Red;
var a = new Label { Text = "Button Added!!!!", BackgroundColor = Color.Purple};
a.FontSize = 30;
root.Children.Add(a, xConstraint:null);
return root;
}
}
这是主要的:
public class App : Application
{
public App()
{
var root = new PageExperiment();
MainPage = new ContentPage
{
Content = root.getPage()
};
}
}
答案 0 :(得分:1)
添加
((RelativeLayout)_page.Content).ForceLayout ();
到你的OnButtonClicked(对象发送者,EventArgs e)
的末尾看起来这是相对布局渲染中的错误。它似乎不是滚动视图的问题。我看到你已经在Xamarin上发布了一个错误报告,可能已经做了更多的事情来修复"这个。正如我在bug报告中提到的那样。错误不仅仅是布局中的布局,它的任何可视元素在第一次单击时添加到相对布局中。添加对ForceLayout的调用应该可以使您的应用程序正常运行,但可能会有性能损失。