【发布时间】: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