我以编程方式操作网格,根据数据集中的行数在后台代码中设置行数和列数。我在网格的列中显示图像、标签和按钮。我正在处理的数据集有 2 行,因此我在网格中创建了 1 行和 2 列。我将控件添加到VerticalStacklayout
指定行和列的网格行中。但是网格显示时列是堆叠的。到目前为止,我尝试过的所有方法都没有效果。我在页面上还定义了一个弹出菜单,它运行良好。我的 xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.DeptUser"
x:DataType="Line"
Title="DeptUser">
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="IconLabelStyle" TargetType="Label">
<Setter Property="FontSize" Value="Medium" />
<Setter Property="WidthRequest" Value="50" />
<Setter Property="VerticalTextAlignment" Value="Center" />
<Setter Property="HorizontalTextAlignment" Value="Center" />
<Setter Property="TextColor" Value="Black" />
<Setter Property="BackgroundColor" Value="Gold" />
</Style>
<Style x:Key="LabelStyle" TargetType="Label">
<Setter Property="VerticalTextAlignment" Value="Center" />
<Setter Property="Padding" Value="5,5" />
<Setter Property="FontSize" Value="Medium" />
<Setter Property="BackgroundColor" Value="Gold" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ScrollView>
<Grid
RowDefinitions="50,*" >
<VerticalStackLayout
Grid.Row = "0"
BackgroundColor="Gold"
>
<Button
Text="☰"
FontSize="Medium"
CornerRadius="0"
HorizontalOptions="Start"
WidthRequest="50"
TextColor="Black"
BackgroundColor="Gold"
Clicked="ShowFlyout"/>
</VerticalStackLayout>
<VerticalStackLayout
Grid.Row="1"
Padding="5,0"
Spacing="10">
<Image
Source="banner2.jpg"
HeightRequest="75"
Aspect="AspectFit"
SemanticProperties.Description="Company Personnel"
/>
<Line Stroke="Black" X2="{Binding Width, Source={RelativeSource Self}}" HorizontalOptions="Fill" StrokeThickness="2" />
<Label x:Name="lblNumberOfPersonnel"
Text="Personnel in your department:"
Style="{StaticResource Headline}"
TextColor="Blue"
HorizontalOptions="Center"
HeightRequest="25"
FontSize="16"
SemanticProperties.HeadingLevel="Level1"/>
<Grid x:Name="MyCurrentEmployees">
</Grid>
</VerticalStackLayout>
<Border
x:Name="Overlay"
IsVisible="false"
Grid.Row="1"
BackgroundColor="DarkGray"
Opacity="0.8">
<Border.GestureRecognizers>
<TapGestureRecognizer Tapped="OnOverlayTap"/>
</Border.GestureRecognizers>
</Border>
<VerticalStackLayout
x:Name="Flyout"
WidthRequest="260"
HorizontalOptions="Start"
Spacing="2"
Grid.Row="1"
BackgroundColor="Gold"
TranslationX="-250">
<HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50" >
<Image Source="addEmployee.jpg" Style="{StaticResource IconLabelStyle}" />
<Label x:Name="AddEmployee" Text="Add Employee" Style="{StaticResource LabelStyle}" />
<HorizontalStackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="Add Another Participant"/>
</HorizontalStackLayout.GestureRecognizers>
</HorizontalStackLayout>
<HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
<Image Source="assign.jpg" Style="{StaticResource IconLabelStyle}" />
<Label x:Name="Assign" Text="Assign Employee To Section" Style="{StaticResource LabelStyle}" />
<HorizontalStackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="Re-Register"/>
</HorizontalStackLayout.GestureRecognizers>
</HorizontalStackLayout>
<HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
<Image Source="text.jpg" Style="{StaticResource IconLabelStyle}" />
<Label x:Name="sendtext" Text="Send Text" Style="{StaticResource LabelStyle}" />
<HorizontalStackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="ChatRoom"/>
</HorizontalStackLayout.GestureRecognizers>
</HorizontalStackLayout>
<HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
<Image Source="myaccount.jpg" Style="{StaticResource IconLabelStyle}" />
<Label x:Name="MyAccount" Text="My Account" Style="{StaticResource LabelStyle}" />
<HorizontalStackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="My Account"/>
</HorizontalStackLayout.GestureRecognizers>
</HorizontalStackLayout>
<HorizontalStackLayout HorizontalOptions="Start" HeightRequest="50">
<Image Source="myaccount.jpg" Style="{StaticResource IconLabelStyle}" />
<Label x:Name="LogOut" Text="Log Off" Style="{StaticResource LabelStyle}" />
<HorizontalStackLayout.GestureRecognizers>
<TapGestureRecognizer Tapped="OnMenuItemTap" CommandParameter="Log Off"/>
</HorizontalStackLayout.GestureRecognizers>
</HorizontalStackLayout>
</VerticalStackLayout>
</Grid>
</ScrollView>
</ContentPage>
我遇到问题的网格是MyCurrentEmployees
。我的代码:
protected override async void OnAppearing()
{
myCurrentemployees = Utils.ToDataSet(myService.GetEmployeeNames(MyUser.Username, MyUser.Password));
for (int x = 0; x < myCurrentemployees.Tables[0].Rows.Count/2; x+=2)
{
MyCurrentEmployees.RowDefinitions.Add(new RowDefinition());
}
ColumnDefinition c1 = new ColumnDefinition();
c1.Width = new GridLength(50, GridUnitType.Star);
ColumnDefinition c2 = new ColumnDefinition();
c2.Width = new GridLength(50, GridUnitType.Star);
MyCurrentEmployees.ColumnDefinitions.Add(c1);
MyCurrentEmployees.ColumnDefinitions.Add(c2);
foreach (DataRow MyDataRow in myCurrentemployees.Tables[0].Rows)
{
Image Myimage = new Image();
Myimage.HeightRequest = 150;
Myimage.WidthRequest = 150;
byte[] imageArray = new byte[1];
MyPhotoDataSet = Utils.ToDataSet(myService1.GetPhoto(MyUser.Username, MyUser.Password, MyDataRow["EmployeeID"].ToString()));
if (MyPhotoDataSet.Tables[0].Rows[0][0].ToString() != null)
{
imageArray = (byte[])MyPhotoDataSet.Tables[0].Rows[0][0];
}
else
{
MyPhotoDataSet = Utils.ToDataSet(myService.GetDummyPhoto(MyUser.Username, MyUser.Password));
imageArray = (byte[])MyPhotoDataSet.Tables[0].Rows[0][0];
}
MemoryStream memory = new MemoryStream(imageArray);
Myimage.Source = Microsoft.Maui.Controls.ImageSource.FromStream(() => (Stream)memory);
Label Mylabel = new Label();
Mylabel.Text = MyDataRow["Name"].ToString();
Mylabel.HorizontalTextAlignment = TextAlignment.Center;
Mylabel.WidthRequest = 150;
Button MyDeleteButton = new Button();
MyDeleteButton.Text = "Delete Employee";
MyDeleteButton.TextColor = Colors.Black;
MyDeleteButton.FontAttributes = FontAttributes.Bold;
MyDeleteButton.BackgroundColor = Colors.Gold;
MyDeleteButton.WidthRequest = 150;
VerticalStackLayout MyLayOut = new VerticalStackLayout();
MyLayOut.Children.Add(Myimage);
MyLayOut.Children.Add(Mylabel);
MyLayOut.Children.Add(MyDeleteButton);
MyCurrentParticipants.Add(MyLayOut, RowNumber, ColumnNumber);
if (ColumnNumber == 1)
{
ColumnNumber = 0;
RowNumber++;
} else
{
ColumnNumber++;
}
}
}
截屏:
你的
Add
观点是错误的不是