【问题标题】:Xamarin Forms display data gridXamarin Forms 显示数据网格
【发布时间】:2020-04-28 08:15:10
【问题描述】:

我是 xamarin 表单的初学者,我必须以网格形式显示动态数据。我的数据有多个列和行,并不总是适合屏幕大小。所以我必须水平和垂直滚动。第一列是数据的描述,我称之为行标题。我的目标是在列“下方”和行标题“下方”移动/滚动数据。列和行标题必须始终可见。
也许有人有同样的问题/解决方案并指出我正确的方向?还是样品?

其他信息:
要更清楚。如果用户向左滚动数据区域,则列标题必须向左滚动。因为并非所有列都可以显示在屏幕上。与行标题相同。如果用户向下滚动数据区域,它必须向下滚动。
在 excel 中,该函数被命名为“冻结窗格”: 我丑陋的简报:

谢谢

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    您可以使用 Grid 作为根布局,使用 ScrollView 来显示数据。

    当数据超出屏幕时,ScrollView 可以滚动。

    <Grid.ColumnDefinitions>
    
    
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
            <ColumnDefinition Width="200" />
    
        </Grid.ColumnDefinitions>
    
    <!--Columns-->
    <Label Text="title0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="0" Grid.Column="1" />
    <Label Text="title1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="0" Grid.Column="2" />
    <Label Text="title2" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="0" Grid.Column="3" />
    <Label Text="title3" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="0" Grid.Column="4" />
    
    
            <!--Row Header-->
    <Label Text="title0" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="1" Grid.Column="0" />
    <Label Text="title1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="2" Grid.Column="0" />
    <Label Text="title2" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="3" Grid.Column="0" />
    <Label Text="title3" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="4" Grid.Column="0" />
    <Label Text="title4" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="5" Grid.Column="0" />
    <Label Text="title5" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="6" Grid.Column="0" />
    <Label Text="title6" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightPink" Grid.Row="7" Grid.Column="0" />
    <Label Text="title7" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" BackgroundColor="LightBlue" Grid.Row="8" Grid.Column="0" />
    
    
     <!--Data-->
    
    
    
    
    </Grid>
    
    </ScrollView>
    

    【讨论】:

    • 感谢您的回复。我觉得你误会了我。 ;) 我更新了问题。抱歉不清楚。
    • 您可以将网格放在滚动视图中。
    • 是否可以使用第三方UI库?
    • 我又更新了。 ;) @Joseph:也许吧。取决于成本和功能。
    • 正如我所说,您可以将网格放在 ScrollView 中。
    【解决方案2】:

    如果您使用 xamarin.forms 同步融合数据网格 (SfDataGrid),您有两个选项可以解决此问题:

    1)-第一个使用 SfDataGrid 的Unbound RowsUnbound Columns 功能。

    2)-第二次使用 SfDataGrid 的 Freeze Panes(也称为 Frozen Views)功能。

    如果是我,我会选择第二个选项,即 Freeze Panes ,它的外观如下:

    syncfusion 有一个免费的社区许可证,您可以使用,一旦您在那里开设一个免费帐户page,就可以生成社区许可证密钥。

    一旦您有了密钥,请按照setup instructions 了解如何设置 SfDataGrid,SfDataGrid 只是一个小的 NuGet 包。

    然后在您的 App 类构造器中将密钥作为 RegisterLicense 方法的参数。

    public App()
    {
        //Register Syncfusion license
        Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
    
        InitializeComponent();
    
        MainPage = new App1.MainPage();
    }
    

    有关licensing的更多信息。

    【讨论】:

    【解决方案3】:

    这适用于我在 Android 和 iOS 上。在 UWP 上滚动不流畅。但这对我来说并不重要:

    <Grid BackgroundColor="Black">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="150" />
    <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
    <RowDefinition Height="80" />
    <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Label HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="Empty" Grid.Row="0" Grid.Column="0" />
    <AbsoluteLayout Grid.Column="1"
    Grid.Row="0"
    VerticalOptions="FillAndExpand" >
    <ScrollView x:Name="colScrollView"
    Orientation="Horizontal"
    VerticalScrollBarVisibility="Never"
    HorizontalScrollBarVisibility="Never"
    AbsoluteLayout.LayoutBounds="0,0,1,1"
    AbsoluteLayout.LayoutFlags="All">
    <Grid BackgroundColor="LightGoldenrodYellow">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Label Text="Column" Grid.Row="0" Grid.Column="0" />
    <Label Text="Column" Grid.Row="0" Grid.Column="1" />
    <Label Text="Column" Grid.Row="0" Grid.Column="2" />
    <Label Text="Column" Grid.Row="0" Grid.Column="3" />
    <Label Text="Column" Grid.Row="0" Grid.Column="4" />
    </Grid>
    </ScrollView>
    <BoxView AbsoluteLayout.LayoutBounds="0,0,1,1"
    AbsoluteLayout.LayoutFlags="All"
    BackgroundColor="Transparent"></BoxView>
    </AbsoluteLayout>
    <AbsoluteLayout Grid.Column="0"
    Grid.Row="1"
    VerticalOptions="FillAndExpand">
    <ScrollView x:Name="rowScrollView"
    Orientation="Vertical"
    VerticalScrollBarVisibility="Never"
    HorizontalScrollBarVisibility="Never"
    AbsoluteLayout.LayoutBounds="0,0,1,1"
    AbsoluteLayout.LayoutFlags="All">
    
    <Grid BackgroundColor="LightBlue">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    </Grid.RowDefinitions>
    <Label Text="Cell" Grid.Column="0" Grid.Row="0" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="1" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="2" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="3" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="4" />
    </Grid>
    </ScrollView>
    <BoxView AbsoluteLayout.LayoutBounds="0,0,1,1"
    AbsoluteLayout.LayoutFlags="All"
    BackgroundColor="Transparent"></BoxView>
    </AbsoluteLayout>
    <AbsoluteLayout Grid.Column="1"
    Grid.Row="1"
    VerticalOptions="FillAndExpand">
    <ScrollView x:Name="dataScrollView"
    Orientation="Both"
    AbsoluteLayout.LayoutBounds="0,0,1,1"
    AbsoluteLayout.LayoutFlags="All">
    <Grid BackgroundColor="LightGreen">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    <RowDefinition Height="100" />
    </Grid.RowDefinitions>
    <Label Text="Cell" Grid.Column="0" Grid.Row="0" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="1" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="2" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="3" />
    <Label Text="Cell" Grid.Column="0" Grid.Row="4" />
    
    <Label Text="Cell" Grid.Column="1" Grid.Row="0" />
    <Label Text="Cell" Grid.Column="1" Grid.Row="1" />
    <Label Text="Cell" Grid.Column="1" Grid.Row="2" />
    <Label Text="Cell" Grid.Column="1" Grid.Row="3" />
    <Label Text="Cell" Grid.Column="1" Grid.Row="4" />
    
    <Label Text="Cell" Grid.Column="2" Grid.Row="0" />
    <Label Text="Cell" Grid.Column="2" Grid.Row="1" />
    <Label Text="Cell" Grid.Column="2" Grid.Row="2" />
    <Label Text="Cell" Grid.Column="2" Grid.Row="3" />
    <Label Text="Cell" Grid.Column="2" Grid.Row="4" />
    
    <Label Text="Cell" Grid.Column="3" Grid.Row="0" />
    <Label Text="Cell" Grid.Column="3" Grid.Row="1" />
    <Label Text="Cell" Grid.Column="3" Grid.Row="2" />
    <Label Text="Cell" Grid.Column="3" Grid.Row="3" />
    <Label Text="Cell" Grid.Column="3" Grid.Row="4" />
    
    <Label Text="Cell" Grid.Column="4" Grid.Row="0" />
    <Label Text="Cell" Grid.Column="4" Grid.Row="1" />
    <Label Text="Cell" Grid.Column="4" Grid.Row="2" />
    <Label Text="Cell" Grid.Column="4" Grid.Row="3" />
    <Label Text="Cell" Grid.Column="4" Grid.Row="4" />
    </Grid>
    </ScrollView>
    </AbsoluteLayout>
    </Grid>
    

    后面的代码:

    public MyPage()
    {
    InitializeComponent();
    dataScrollView.Scrolled += DataScrollView_Scrolled;
    }
    
    private async void DataScrollView_Scrolled(object sender, ScrolledEventArgs e)
    {
    await rowScrollView.ScrollToAsync(0, e.ScrollY, false);
    await colScrollView.ScrollToAsync(e.ScrollX, 0, false);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2020-01-11
      • 2016-06-15
      相关资源
      最近更新 更多