簡體   English   中英

CollectionView 不會使用 ObservableCollection 作為 Sourcee 進行更新

[英]CollectionView doesn't update with ObservableCollection as Sourcee

我在 A 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();
}

這是我的觀點 Model:

  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 應用程序。 問題是 CollectionView 沒有顯示任何項目,但 StandIns 充滿了 39 個項目。 我不知道為什么它不起作用。 謝謝你幫助我。

我測試了你的代碼並做了一些改進。 它現在可以工作了。

這是我的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();
    }

我用static數據測試,你可以去掉。 這是我的ViewModel中的代碼:

 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; }
    }

這是項目的最終視圖:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM