英文:
.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?
<?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>
答案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
内部使用ListView
、WebView
或ScrollView
。
英文:
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
<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 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论