AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79488164
Accepted
Prescott Chartier
Prescott Chartier
Asked: 2025-03-06 11:09:48 +0800 CST2025-03-06 11:09:48 +0800 CST 2025-03-06 11:09:48 +0800 CST

Maui 网格中的列显示为堆叠的,而不是位于同一行

  • 772

我以编程方式操作网格,根据数据集中的行数在后台代码中设置行数和列数。我在网格的列中显示图像、标签和按钮。我正在处理的数据集有 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++;
        }
    }
}

截屏:

在此处输入图片描述 我做错什么了?

c#
  • 1 1 个回答
  • 37 Views

1 个回答

  • Voted
  1. Best Answer
    Jason
    2025-03-06T23:36:35+08:002025-03-06T23:36:35+08:00

    你的Add观点是错误的

    Grid.Add(View, Column, Row);
    

    不是

    Grid.Add(View, Row, Column);
    
    • 2

相关问题

  • Polly DecorlatedJitterBackoffV2 - 如何计算完成所有重试所需的最长时间?

  • Wpf。在 ScrollViewer 中滚动 DataGrid

  • 我在使用 .NET MAUI MVVM 的游戏页面上获得的分数在其他页面上不可见。如何在本地设备中保存分数数据

  • 从 DataTemplate 内部将 TreeView 层次结构与 HierarchicalDataTemplate 结合使用

  • 如何改进 .NET 中的验证接口?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve