Eu manipulo uma grade programaticamente definindo o número de linhas e colunas no código por trás com base no número de linhas em um conjunto de dados. Eu exibo uma imagem, rótulo e um botão na coluna da grade. O conjunto de dados com o qual estou trabalhando tem 2 linhas, então eu crio 1 linha com duas colunas na grade. Eu adiciono os controles dentro de uma VerticalStacklayout
linha da grade especificando a linha e a coluna. No entanto, a grade é exibida com a coluna empilhada. Tudo o que tentei até agora não teve efeito. Eu também tenho um menu flyout definido na página que funciona perfeitamente. Meu 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>
A grade com a qual estou tendo problemas é MyCurrentEmployees
. Meu código:
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++;
}
}
}
Captura de tela:
você tem os
Add
argumentos ao contrárioNÃO