【问题标题】:Xamarin.Forms: Can't dinamically Add layouts to content of a scrollViewXamarin.Forms:无法动态地将布局添加到滚动视图的内容
【发布时间】:2015-09-23 14:04:54
【问题描述】:

基本上,我正在尝试动态地将布局添加到滚动视图的内容中。我做了一个示例代码,但它只能在第二次点击时工作,由于某种原因它不会呈现添加的第一个布局!!!我做错了吗?

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

    }
}

【问题讨论】:

    标签: c# scrollview xamarin.forms


    【解决方案1】:

    添加

    ((RelativeLayout)_page.Content).ForceLayout ();
    

    到您的 OnButtonClicked(object sender, EventArgs e) 结束

    看起来这是相对布局渲染中的错误。滚动视图似乎不是问题。我看到您已经使用 Xamarin 放置了一个错误报告,很可能可以做更多的事情来“修复”这个问题。正如我在错误报告中指出的那样。这些错误不仅适用于布局中的布局,还包括在第一次点击时添加到相对布局中的任何视觉元素。添加对 ForceLayout 的调用应该可以使您的应用正常运行,但可能会影响性能。

    【讨论】:

    • 我有点担心性能,但它有效!!!因此,在 Xamarin 团队解决此问题之前,这是一个解决方案。谢谢。
    猜你喜欢
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多