我有一个Picker,我已经从模型中填充了对象数据。我面临的挑战是,当我点击选择器选择一个项目时,列表是空的。也当我指定ItemsDisplayBinding到模型属性之一。这些项目以app-name的形式以奇怪的名字显示。模型,例如,如果应用名称是cool app,项目显示为cool-app.Model. modelname…
我做错了什么?这是我的密码。
//xml文件
<ContentPage
xmlns:model="clr-namespace:SoftFashions.Model"
xmlns:viewmodel="clr-namespace:SoftFashions.ViewModel"
x:DataType="viewmodel:ShoppingBagViewModel">
<Grid RowDefinitions="Auto,200,*"
RowSpacing="0"
ColumnDefinitions="*,*">
<CollectionView
ItemsSource="{Binding ShoppingBagCollection}"
Grid.Row="1"
Grid.ColumnSpan="2"
EmptyView="NO ITEMS ADDED">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Cloth">
<!-- controls displaying shoppingBagCollection data-->
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Grid
Grid.ColumnSpan="2"
Grid.Row="2"
Padding="0"
Grid.ColumnDefinitions="*,*"
Grid.RowDefinitions="*,*,*,*"
ColumnSpacing="0"
HorizontalOptions="FillAndExpand"
Margin="20">
<VerticalStackLayout
Grid.RowSpan="4"
Grid.Column="0">
<Label Grid.Row="0"
Text="LOCATION"
TextColor="{StaticResource Primary}"
Style="{StaticResource MediumLabel}"
FontAttributes="Bold"/>
<!-- picker to display items from the PickupStations collection-->
<Picker
x:Name="picker"
TextColor="Black"
Grid.Row="1"
Title="SELECT LOCATION"
FontAttributes="Italic"
ItemsSource="{Binding PickupStations}"
ItemDisplayBinding="{Binding Location}"
SelectedItem="{Binding SelectedItem}"
/>
<!-- entry to display text depending on selected item from the picker-->
<Entry x:Name="shippingfeeLabel"
Grid.Row="2"
Text="{Binding SelectedItem.Shippingfee}"
WidthRequest="120"
HorizontalOptions="Start"
Placeholder="$0.00"
Margin="0,15,0,0"
IsReadOnly="True"/>
<Entry Grid.Row="2"
Text="shipping fee"
IsReadOnly="True"
Margin="0,0,0,0"/>
</VerticalStackLayout>
</Grid>
</ContentPage>
//查看模型
/*variables*/
[ObservableProperty]
Pickupstations selectedItem;
public ObservableCollection<Pickupstations> PickupStations { get; } = new();
ClothService clothService;
//Constructor
public ShoppingBagViewModel()
{
this.clothService = new();
}
// getting pickup stations from the service
[RelayCommand]
public async Task<ObservableCollection<Pickupstations>> DisplayPickupstationsAsync()
{
try
{
var pickuplists = await clothService.GetPickupstations();
if (PickupStations.Count > 0)
PickupStations.Clear();
foreach (var pickuplist in pickuplists)
{
PickupStations.Add(pickuplist);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return PickupStations;
}
}
//模型public class Pickupstations
{
public string Name { get; }
public string Location { get; }
public string Shippingfee { get; }
}
当然,服务从互联网获取数据,视图模型从那里获取数据
请帮我解决这个…多谢
您可以通过ClothService直接分配PickupStations
在ViewModel构造函数中。我编写了一个pseudo
ClothService.cs来生成选择器的数据源,并提取关键元素以供您在下面参考:
Xaml:
<Picker
x:Name="picker"
TextColor="Black"
Title="SELECT LOCATION"
FontAttributes="Italic"
ItemsSource="{Binding PickupStations}"
ItemDisplayBinding="{Binding Location}"
SelectedItem="{Binding SelectedItem}"/>
ViewModel:
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
Pickupstations selectedItem;
public ObservableCollection<Pickupstations> PickupStations { get; } = new();
public MainViewModel()
{
PickupStations = clothService.GetPickstations();
}
}
ClothService.cs:
public class clothService
{
public static ObservableCollection<Pickupstations> GetPickstations()
{
var locations = new ObservableCollection<Pickupstations>()
{
new Pickupstations{Name="Bob", Location="Italy"},
new Pickupstations{Name="Rock", Location="Greek"},
new Pickupstations{Name="John", Location="Argetina"},
};
return locations;
}
}
模型:
public class Pickupstations
{
public string Name { get; set; }
public string Location { get; set; }
}