.Net Maui ListView 在 Android 上未显示

huangapple go评论86阅读模式
英文:

.Net Maui ListView is not appearing in Android

问题

问题在于我的Android设备和Android模拟器上没有显示listview的标题和内容,请问有什么解决方法吗?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Listv.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <ListView>
                <ListView.Header>TheHeader</ListView.Header>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="Hi"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
英文:

The problem is that listview header and content is not appearing on my android device and android emulator, so please what is the fix for that?

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
         xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
         x:Class=&quot;Listv.MainPage&quot;&gt;

&lt;ScrollView&gt;
    &lt;VerticalStackLayout
        Spacing=&quot;25&quot;
        Padding=&quot;30,0&quot;
        VerticalOptions=&quot;Center&quot;&gt;

        &lt;ListView&gt;
            &lt;ListView.Header&gt;TheHeader&lt;/ListView.Header&gt;
            &lt;ListView.ItemTemplate&gt;
                &lt;DataTemplate&gt;
                    &lt;ViewCell&gt;
                        &lt;Label Text=&quot;Hi&quot;/&gt;
                    &lt;/ViewCell&gt;
                &lt;/DataTemplate&gt;
            &lt;/ListView.ItemTemplate&gt;
        &lt;/ListView&gt;

    &lt;/VerticalStackLayout&gt;
&lt;/ScrollView&gt;

</ContentPage>

答案1

得分: 1

根据上面的代码,我发现您还没有为ListView设置ItemsSource,也就是说ListView没有数据可供渲染。

您需要为您的页面设置BindingContext,并为ListView设置ItemsSource

您可以参考以下代码:

MainPage.xaml

<ContentPage.BindingContext>
    <XamListViewApp:MyViewModel></XamListViewApp:MyViewModel>
</ContentPage.BindingContext>

<!-- <ScrollView> -->
<StackLayout Orientation="Vertical"
              Spacing="25"
              Padding="30,0"
              VerticalOptions="Center">
    <ListView Grid.Row="1" ItemsSource="{Binding Items}">
        
        <ListView.Header>TheHeader</ListView.Header>
        
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <!-- <Grid BackgroundColor="{Binding BgColor}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="70"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="80"/>
                            <ColumnDefinition Width="80"/>
                        </Grid.ColumnDefinitions>

                        <Label Grid.Row="0" Grid.Column="0" Text="{Binding NumType}" Margin="0,0,0,0" />
                        <Label Grid.Row="0" Grid.Column="0" Text="{Binding LocationCode}" Margin="0,0,0,0" />
                        <Label Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Text="{Binding Barcode}" Margin="0,0,0,0" />
                        <Label Grid.Row="0" Grid.Column="2" Text="{Binding UserName}" Margin="0,0,0,0" />
                        <Label Grid.Row="0" Grid.Column="3" Grid.RowSpan="2" Text="{Binding PickingAdjustementDate}" Margin="0,0,0,0" />

                    </Grid>-->

                    <Label Text="Hi"/>

                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

<!-- </ScrollView> -->

MyViewModel.cs

public class MyViewModel
{
    public ObservableCollection<Item> Items { get; set; }

    public MyViewModel()
    {
        Items = new ObservableCollection<Item>();

        Items.Add(new Item { NumType = "S", LocationCode = "0001" });
        Items.Add(new Item { NumType = "M", LocationCode = "0002" });
        Items Add(new Item { NumType = "L", LocationCode = "0003" });
        Items.Add(new Item { NumType = "S", LocationCode = "0001" });
        Items.Add(new Item { NumType = "M", LocationCode = "0002" });
        Items.Add(new Item { NumType = "L", LocationCode = "0003" });
    }
}

Item.cs

public class Item
{
    public string NumType { get; set; }
    public string LocationCode { get; set; }
    public string Barcode { get; set; }
    public string UserName { get; set; }
    public string PickingAdjustmentDate { get; set; }
}

要获取更多信息,您可以查看:ListView 外观自定义

注意:

一般来说,Xamarin不建议在ScrollView内部使用ListViewWebViewScrollView

英文:

From above code,I found that you haven't set the ItemsSource for ListView, that is to say there is no data for ListView to render.

You need to set BindingContext for your page and set the ItemsSource for ListView.

You can refer to the following code:

MainPage.xaml

    &lt;ContentPage.BindingContext&gt;
        &lt;XamListViewApp:MyViewModel&gt;&lt;/XamListViewApp:MyViewModel&gt;
    &lt;/ContentPage.BindingContext&gt;

    &lt;!--&lt;ScrollView&gt;--&gt;
        &lt;StackLayout  Orientation=&quot;Vertical&quot;
                      Spacing=&quot;25&quot;
                      Padding=&quot;30,0&quot;
                      VerticalOptions=&quot;Center&quot;
                     &gt;
            &lt;ListView Grid.Row=&quot;1&quot;   ItemsSource=&quot;{Binding Items}&quot;  &gt;
                
                &lt;ListView.Header&gt;TheHeader&lt;/ListView.Header&gt;
                
                &lt;ListView.ItemTemplate&gt;
                    &lt;DataTemplate&gt;
                        &lt;ViewCell&gt;
                            &lt;!--&lt;Grid BackgroundColor=&quot;{Binding BgColor}&quot;&gt;
                                &lt;Grid.ColumnDefinitions&gt;
                                    &lt;ColumnDefinition Width=&quot;70&quot;/&gt;
                                    &lt;ColumnDefinition Width=&quot;100&quot;/&gt;
                                    &lt;ColumnDefinition Width=&quot;80&quot;/&gt;
                                    &lt;ColumnDefinition Width=&quot;80&quot;/&gt;
                                &lt;/Grid.ColumnDefinitions&gt;

                                &lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Text=&quot;{Binding NumType}&quot; Margin=&quot;0,0,0,0&quot; /&gt;
                                &lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Text=&quot;{Binding LocationCode}&quot;  Margin=&quot;0,0,0,0&quot; /&gt;
                                &lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;1&quot; Grid.RowSpan=&quot;2&quot; Text=&quot;{Binding Barcode}&quot;  Margin=&quot;0,0,0,0&quot; /&gt;
                                &lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;2&quot; Text=&quot;{Binding UserName}&quot;  Margin=&quot;0,0,0,0&quot; /&gt;
                                &lt;Label Grid.Row=&quot;0&quot; Grid.Column=&quot;3&quot; Grid.RowSpan=&quot;2&quot; Text=&quot;{Binding PickingAdjustementDate}&quot;  Margin=&quot;0,0,0,0&quot; /&gt;

                            &lt;/Grid&gt;--&gt;

                        &lt;Label Text=&quot;Hi&quot;/&gt;

                    &lt;/ViewCell&gt;
                    &lt;/DataTemplate&gt;
                &lt;/ListView.ItemTemplate&gt;
            &lt;/ListView&gt;
        &lt;/StackLayout&gt;

    &lt;!--&lt;/ScrollView&gt;--&gt;

MyViewModel.cs

public class MyViewModel 
{
    public ObservableCollection&lt;Item&gt; Items { get; set; }


    public MyViewModel() { 
     Items = new ObservableCollection&lt;Item&gt;();

        Items.Add( new Item { NumType = &quot;S&quot; , LocationCode = &quot;0001&quot;});
        Items.Add(new Item { NumType = &quot;M&quot;, LocationCode = &quot;0002&quot; });
        Items.Add(new Item { NumType = &quot;L&quot;, LocationCode = &quot;0003&quot; });
        Items.Add(new Item { NumType = &quot;S&quot;, LocationCode = &quot;0001&quot; });
        Items.Add(new Item { NumType = &quot;M&quot;, LocationCode = &quot;0002&quot; });
        Items.Add(new Item { NumType = &quot;L&quot;, LocationCode = &quot;0003&quot; });

    }
}

Item.cs

public class Item
{

    public string NumType { get; set; }

    public string LocationCode { get; set; }

    public string Barcode { get; set; }
    public string UserName { get; set; }

    public string PickingAdjustementDate { get; set; }

}

For more information, you can check: ListView appearance

Note:

In general, Xamarin doesn't recommend using the ListView, WebView, or ScrollView inside a ScrollView.

huangapple
  • 本文由 发表于 2023年2月8日 20:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定