我在ScrollView中有一个CollectionView。我希望将源绑定到ViewModel中的ObservableCollection。
下面是我的XAML代码:
<RefreshView x:DataType="vm:TodayViewModel" Command="{Binding LoadStandInsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
<CollectionView ItemsSource="{Binding StandIns}" SelectionMode="None">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="10" x:DataType="model:StandIn" BackgroundColor="Red">
<Frame HeightRequest="50" >
<StackLayout>
<Label Style="{StaticResource BaseLabel}" Text="{Binding Stunde}" FontSize="10"/>
<Label Style="{StaticResource BaseLabel}" Text="{Binding Fach}" FontSize="10"/>
<Label Style="{StaticResource BaseLabel}" Text="{Binding Raum}" FontSize="10"/>
<Label Style="{StaticResource BaseLabel}" Text="{Binding Lehrer}" FontSize="10"/>
<Label Style="{StaticResource BaseLabel}" Text="{Binding Art}" FontSize="10"/>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</RefreshView>
下面是我的Xaml.cs:
TodayViewModel _viewModel;
public TodayPage()
{
InitializeComponent();
BindingContext = _viewModel = new TodayViewModel();
}
protected override void OnAppearing()
{
base.OnAppearing();
_viewModel.OnAppearing();
}
以下是我的视图模型:
public Command LoadStandInsCommand { get; }
public ObservableCollection<StandIn> StandIns { get; }
public TodayViewModel()
{
StandIns = new ObservableCollection<StandIn>();
LoadStandInsCommand = new Command(async ()=> await ExecuteLoadStandInsCommand());
}
async Task ExecuteLoadStandInsCommand()
{
IsBusy = true;
try
{
StandIns.Clear();
if (loadListToday)
{
sIList.AddRange(gh.GetStandInsToday());
}
else
{
sIList.AddRange(gh.GetStandInsTomorrow());
}
foreach (StandIn item in sIList)
{
if (item.Klasse == pN.Klasse || item.Lehrer == pN.Klasse)
{
StandIns.Add(item);
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
作为一个信息,我正在写一个.netMaui应用程序。问题是,收藏视图不显示任何项目,但替身充满了39个项目。我不知道为什么它不工作。谢谢你帮助我。
1条答案
按热度按时间js5cn81o1#
我测试了你的代码并做了一些改进。它现在可以工作了。
下面是我的
Xaml
的代码:下面是
Xaml.cs
中的代码:我使用静态数据进行测试,您可以删除它们。
下面是我的
Model
:这是项目的最终视图: