【问题标题】:Xamarin Android emulator using grid inside scrollview, only first page of grid is drawnXamarin Android模拟器在滚动视图中使用网格,仅绘制网格的第一页
【发布时间】:2021-10-19 17:55:25
【问题描述】:

我正在使用标准 Android 模拟器在 Visual Studio 2019 中开发 Xamarin Forms 应用。 XAML 包含一个包含 Grid 的 ScrollView。网格只有两列。我有一个用于连接到数据库的 OnStart 事件的处理程序,获取包含 64 个对象的响应,将两个标签添加到每个对象的网格中,并填充标签。 ScrollView 的背景颜色设置为黄色。当我使用模拟器调试应用程序时,初始屏幕绘制正确。但是当我向下滚动时,网格没有被填充。背景仍然是黄色的,表明滚动视图的大小是正确的(或者至少,大到足以容纳更多的标签)。我做错了什么?

当我在第二个 for 循环的底部放置断点时,添加标签后,我看到我拥有所有我期望的标签对象,并且它们的文本属性设置符合我的期望。

这是 XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CapsBases.MainPage">

    <ScrollView BackgroundColor="Yellow"
                VerticalOptions="FillAndExpand">
        <Grid Margin="10"
              x:Name="BasesGrid">
            <!-- two columns:  base ID and status -->
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="60" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

        </Grid>
    </ScrollView>
</ContentPage>

事件处理程序调用 MainPage.xaml.cs 文件中名为 ShowBases() 的函数。这里是:

public async void ShowBases()
{
    string url = "http://10.0.2.2:5000/caps/bases";
    //HttpClient client = new HttpClient();
    //string info = await client.GetStringAsync(url);
    //// await DisplayAlert("Coil Count", message, "Close");
    //// The base ID and string will be returned in a single string with a space separating them.
    //string[] pieces = info.Split(' ');
    //BaseID.Text = pieces[0];
    //BaseStatus.Text = pieces[1];
    using (HttpClient client = new HttpClient())
    {
        HttpResponseMessage response = await client.GetAsync(url);
        if (response.IsSuccessStatusCode)
        {
            string data = await response.Content.ReadAsStringAsync();
            //IEnumerable<Base> baseList = JsonConvert.DeserializeObject<IEnumerable<Base>>(data);
            List<Base> baseList = JsonConvert.DeserializeObject<List<Base>>(data).OrderBy(b =>b.BaseName).ToList();
            //BaseID.Text = theBase.BaseName;
            //BaseStatus.Text = theBase.Status;
            string message = $"Retrieved {baseList.Count()} bases.";
            await DisplayAlert("Result", message, "Close");

            Grid theGrid = BasesGrid;
            var rowDefinitions = theGrid.RowDefinitions;
            for (int i = 0; i < baseList.Count(); i++)
            {
                RowDefinition rowDef = new RowDefinition { Height = 20 };
                rowDefinitions.Add(rowDef);
            }

            await DisplayAlert("Row count", $"The grid has {BasesGrid.RowDefinitions.Count()} rows.", "Close");

            // We have the grid with all the rows we need.  Now we have to create a label for each grid cell.

            try
            {
                for (int i = 0; i < baseList.Count(); i++)
                {
                    BasesGrid.Children.Add
                    (
                        new Label
                        {
                            Text = baseList[i].BaseName,
                            HorizontalOptions = LayoutOptions.Start
                        },
                        0, i
                    );
                    BasesGrid.Children.Add
                    (
                        new Label
                        {
                            Text = baseList[i].Status,
                            HorizontalOptions = LayoutOptions.Start
                        },
                        1, i
                    );
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", $"Failed to add row data: {ex.Message}", "Close");

            }
        }
        else
        {
            await DisplayAlert("Get Coils failure",
                                $"Failed to retrieve coils; status code: {response.StatusCode}",
                                "Close");
        }
    }

}

}

【问题讨论】:

  • 已报告此问题。您可以尝试 FedorSulaev 提供的方式。 github.com/xamarin/Xamarin.Forms/issues/4180
  • 我看不出 Fedor 的解决方案与我的不同。他正在回答对固定片段和可滚动片段的要求。他的可滚动部分由一个包含 Grid 的 ScrollView 组成,和我的一样。

标签: xamarin xamarin.forms


【解决方案1】:

在垂直方向的 StackLayout 中围绕网格。 Grid 没有在 ScrollView 内展开。

【讨论】:

  • 谢谢。我会试试的。 StackLayout、ScrollView 和 Grid 应该使用哪些 VerticalOptions?
  • 成功了!我不需要 StackLayout 或 ScrollView 上的任何属性,而 Grid 仅设置了 Margin 和 x:Name。非常感谢!
  • 不客气。您不需要设置 VerticalOptions,因为 StackLayout 将使用默认值 Fill 垂直扩展自身以适应其子级。考虑将我的答案标记为将来帮助他人的解决方案。
  • 感谢您接受您的回答的提醒。已经完成了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
相关资源
最近更新 更多