【发布时间】:2020-05-25 04:23:00
【问题描述】:
我正在尝试在 Android 和 WPF 平台中学习 Xamarin.Form。我使用一个非常简单的可观察集合作为 ViewModel 文件中的数据绑定。数据绑定在Android平台上可以正常工作,但是在WPF平台上什么都没有!其他控件(如按钮和标签)在 WPF 平台中正常工作。在调试模式下,当我将 WPF 作为默认项目启动时,填充集合的按钮也可以工作。
ViewModel 文件代码
public sealed class MainPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<string> Test { get; set; } = new ObservableCollection<string>();
public Command LoadCommand { get; }
public MainPageViewModel()
{
LoadCommand = new Command(() =>
{
for (int i = 0; i < 10; i++)
{
Test.Add(i.ToString());
}
});
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
MainPage.xaml 文件代码
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage.BindingContext>
<local:MainPageViewModel />
</ContentPage.BindingContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalOptions="Center"
Text="Select one of the items below."
VerticalOptions="CenterAndExpand" />
<Button
Grid.Row="1"
Grid.Column="1"
Command="{Binding LoadCommand}"
Text="test" />
<CollectionView
Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="2"
ItemsSource="{Binding Test}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame>
<Label Text="{Binding .}" />
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
有什么建议吗?
【问题讨论】:
-
目前不支持 WPF!
标签: c# wpf xamarin.forms data-binding