XAML CollectionView未使用ObservableCollection作为源进行更新

zte4gxcn  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(133)

我在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个项目。我不知道为什么它不工作。谢谢你帮助我。

js5cn81o

js5cn81o1#

我测试了你的代码并做了一些改进。它现在可以工作了。
下面是我的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"  BackgroundColor="Red">
                    <!-- I changed the style format you can change the label style here-->
                    <StackLayout.Resources>
                        <!-- Implicit style -->
                        <Style TargetType="Label">
                            <Setter Property="FontSize" Value="14" />
                            <Setter Property="BackgroundColor" Value="#1976D2" />
                            <Setter Property="TextColor" Value="White" />
                        </Style>
                    </StackLayout.Resources>

                    <Frame HeightRequest="200" >
                        <StackLayout>
                            <Label  Text="{Binding Stunde}" FontSize="20"/>
                            <Label  Text="{Binding Fach}" FontSize="20"/>
                            <Label  Text="{Binding Raum}" FontSize="20"/>
                            <Label  Text="{Binding Lehrer}" FontSize="20"/>
                            <Label  Text="{Binding Art}" FontSize="20"/>
                        </StackLayout>
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</RefreshView>

下面是Xaml.cs中的代码:

public MainPage()
    {
        InitializeComponent();
        
        BindingContext = new TodayViewModel();
    }

我使用静态数据进行测试,您可以删除它们。

internal class TodayViewModel
    {
       /* public Command LoadStandInsCommand { get; }*/
        public ObservableCollection<StandIn> StandIns { get;  }
        public TodayViewModel()
        {
            StandIns = new ObservableCollection<StandIn>();

            StandIns.Add(new StandIn() { Art = "art1", Fach = "fach1", Lehrer = "lehrer1", Raum = "raum1", Stunde = "stunde1" });
            StandIns.Add(new StandIn() { Art = "art1", Fach = "fach1", Lehrer = "lehrer1", Raum = "raum1", Stunde = "stunde1" });
            StandIns.Add(new StandIn() { Art = "art1", Fach = "fach1", Lehrer = "lehrer1", Raum = "raum1", Stunde = "stunde1" });
            StandIns.Add(new StandIn() { Art = "art1", Fach = "fach1", Lehrer = "lehrer1", Raum = "raum1", Stunde = "stunde1" });
            /*  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;
            }
        }*/
    }

下面是我的Model

internal class StandIn
    {
        public string Stunde { get; set; }
        public string Fach { get; set; }
        public string Raum { get; set; }
        public string Lehrer { get; set; }
        public string Art { get; set; }
    }

这是项目的最终视图:

相关问题