【发布时间】:2021-11-05 10:39:32
【问题描述】:
我是 Xamarin 的新手,我正在尝试找出一些基本功能。
我有一个简单的内容页面,其中包含一个带有嵌套 StackLayout 的 ScrollView,然后是 StackLayout 中的其他嵌套控件:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.Views.ScrollGrow">
<ScrollView x:Name="myScrollView" BackgroundColor="LightBlue" VerticalOptions="FillAndExpand">
<StackLayout x:Name="MainStack" BackgroundColor="YellowGreen" VerticalOptions="FillAndExpand">
<Label Text="Start of StackLayout"></Label>
<Button Text="Populate Grid" Clicked="Button_Clicked" x:Name="btnFill" BackgroundColor="Black" TextColor="White"></Button>
<Grid x:Name="myGrid" BackgroundColor="Yellow" VerticalOptions="FillAndExpand"></Grid>
<Label Text="Bottom of StackLayout"></Label>
</StackLayout>
</ScrollView>
</ContentPage>
我正在尝试在单击按钮时向网格动态添加行:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TestApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ScrollGrow : ContentPage
{
public ScrollGrow()
{
InitializeComponent();
}
private void Button_Clicked(object sender, EventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
for (int i = 0; i <= 35; i++)
{
FillMyGrid(i);
}
});
}
private void FillMyGrid(int theCounter)
{
myGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto, });
var testLabel = new Label();
testLabel.Text = "Test row number: " + theCounter;
testLabel.TextColor = Color.Black;
Grid.SetRow(testLabel, theCounter);
Grid.SetColumn(testLabel, 0);
myGrid.Children.Add(testLabel);
}
}
}
不幸的是,当 Grid 增长时,StackLayout 的大小并没有出现增长。
我在这里缺少什么?有人可以向我解释为什么这不起作用以及正确的方法是什么?
【问题讨论】:
标签: xaml xamarin xamarin.forms xamarin.android xamarin.ios