我是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"/span>>
<ScrollView x: 名稱="myScrollView" BackgroundColor="LightBlue" VerticalOptions="FillAndExpand">
<StackLayout x: 名稱="MainStack" BackgroundColor="YellowGreen" VerticalOptions="FillAndExpand">
<Label Text="Start of StackLayout"/span>> </Label>>
< Button Text="Populate Grid" Clicked="Button_Clicked" x: Name="btnFill" BackgroundColor="Black" TextColor="White"> </Button>>
<Grid x: 名稱="myGrid" BackgroundColor="Yellow" VerticalOptions="FillAndExpand"> </Grid>>
<Label Text="Base of StackLayout"> </Label>>
</StackLayout>/span>
</ScrollView>/span>
</ContentPage>/span>
我試圖在點擊按鈕時動態地將行添加到網格中:
我試圖將行添加到網格中。
using System;
using Xamarin.Forms.Definition.Definition.Definition.Definition
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() title">FillMyGrid(int theCounter)。
{
myGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto, });
var testLabel = new Label()。
testLabel.Text = "測驗行數:" theCounter;
testLabel.TextColor = Color.Black;
Grid.SetRow(testLabel, theCounter);
Grid.SetColumn(testLabel, 0);
myGrid.Children.Add(testLabel)。
}
}
}
不幸的是,當Grid增長時,StackLayout的大小并沒有出現增長。
| Before | After |
|---|---|
![]() |
![]() |
我在這里錯過了什么?誰能給我解釋一下為什么這沒有用,正確的方法是什么?
uj5u.com熱心網友回復:
我建議像這樣重新安排你的用戶界面(使用一個Grid)。
<Grid BackgroundColor="YellowGreen"> /span>
<Grid.RowDefinitions>/span>
<RowDefinition Height="Auto"/span> />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto"/span> />
</Grid.RowDefinitions>
<Label Grid. Row="0" Text="Start of StackLayout"> </Label>>
<Button Grid. Row="1" Text="Populate Grid" Clicked="Button_Clicked" x。 Name="btnFill" BackgroundColor="Black" TextColor="White"> </Button>>
<ScrollView Grid.Row="2">/span>
<Grid x: Name="myGrid" BackgroundColor="Yellow"> </Grid>>
</ScrollView>/span>
<Label Grid. Row="3" Text="Base of StackLayout"> </Label>>
</Grid>/span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312692.html
標籤:



