【问题标题】:RowSpan and ColumnSpan in Xamarin.FormsXamarin.Forms 中的 RowSpan 和 ColumnSpan
【发布时间】:2015-05-05 10:20:31
【问题描述】:

有没有人可以向我解释RowSpanColumnSpanXamarin.Forms 中的工作原理?
参数 right、left、top、bottom 有点混乱。

让我们用这个代码 sn-p 表示RowSpan

grid.Children.Add(new Label
{
   Text = "Span two rows (or more if you want)",
   TextColor = Color.Yellow,
   BackgroundColor = Color.Navy,
   XAlign = TextAlignment.Center,
   YAlign = TextAlignment.Center
}, 2, 3, 1, 3);

这里的数字 2、3、1、3 在列和行中是什么意思?
这是一个四行三列的网格。

【问题讨论】:

    标签: c# layout grid xamarin.forms


    【解决方案1】:

    这个答案是由 Till Balandat 从this answer at Xamarin's forums (archived) 复制和粘贴的,但在这里很有用,因为 Xamarin 的文档似乎没有解释 Add 方法的附加参数。

    采用 4 个参数的 Add 重载有点令人困惑,但最终允许您定义 Row、RowSpan、Column 和 Columnspan: 所以上面的例子

    var label = new Label { Text = "Row 1" };
    myGrid.Children.Add(label,0,0);
    Grid.SetColumnSpan(label,2);
    

    翻译为:

    var label = new Label { Text = "Row 1" };
    myGrid.Children.Add(label, 0, 2, 0, 1);
    

    这是 Xamarin 在内部所做的:

    public void Add(View view, int left, int right, int top, int bottom)
    {
        //..exceptionhandling removed
        Grid.SetRow((BindableObject) view, top);
        Grid.SetRowSpan((BindableObject) view, bottom - top);
        Grid.SetColumn((BindableObject) view, left);
        Grid.SetColumnSpan((BindableObject) view, right - left);
        this.Add(view);
    }
    

    所以你可能会更喜欢这样的事情:

    public static class GridExtension
    {
        public static void AddChild(this Grid grid, View view, int row, int column, int rowspan = 1, int columnspan = 1)
        {
            if (row < 0)
                throw new ArgumentOutOfRangeException("row");
            if (column < 0)
                throw new ArgumentOutOfRangeException("column");
            if (rowspan <= 0)
                throw new ArgumentOutOfRangeException("rowspan");
            if (columnspan <= 0)
                throw new ArgumentOutOfRangeException("columnspan");
            if (view == null)
              throw new ArgumentNullException("view");
            Grid.SetRow((BindableObject)view, row);
            Grid.SetRowSpan((BindableObject) view, rowspan);
            Grid.SetColumn((BindableObject) view, column);
            Grid.SetColumnSpan((BindableObject) view, columnspan);
            grid.Children.Add(view);      
        }
    }
    

    【讨论】:

    • 注意:(我只花了半个小时) - 如果 Grid 没有 RowDefinitions,即使在单行网格上,将 ColumnSpan 设置为高于 1 的值也不起作用......(至少在 Xamarin Forms UWP 应用中)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 2023-03-17
    相关资源
    最近更新 更多