Xamarin.Forms: 无法将布局动态添加到 scrollView 的内容
Xamarin.Forms: Can't dinamically Add layouts to content of a scrollView
基本上,我试图动态地将布局添加到 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()
};
}
}
添加
((RelativeLayout)_page.Content).ForceLayout ();
到你的 OnButtonClicked(object sender, EventArgs e) 的结尾
看起来这是相对布局渲染中的错误。滚动视图似乎不是问题。我看到你已经用 Xamarin 提交了一个错误报告,这很可能是 "fix" 可以做的更多。正如我在错误报告中指出的那样。这些错误不仅适用于布局中的布局,还适用于第一次单击时添加到相关布局中的任何视觉元素。添加对 ForceLayout 的调用应该会使您的应用正常运行,但可能会影响性能。
基本上,我试图动态地将布局添加到 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()
};
}
}
添加
((RelativeLayout)_page.Content).ForceLayout ();
到你的 OnButtonClicked(object sender, EventArgs e) 的结尾
看起来这是相对布局渲染中的错误。滚动视图似乎不是问题。我看到你已经用 Xamarin 提交了一个错误报告,这很可能是 "fix" 可以做的更多。正如我在错误报告中指出的那样。这些错误不仅适用于布局中的布局,还适用于第一次单击时添加到相关布局中的任何视觉元素。添加对 ForceLayout 的调用应该会使您的应用正常运行,但可能会影响性能。