Shell CollectionView 没有更新 Xamarin Forms

Shell CollectionView is not updating Xamarin Forms

我正在尝试 Collection在 Shell 中查看,但它没有更新。 我有一个连接到 Page 和 AppShell 的视图模型,但是当我更新 Collection 时,仅视图页面被更新。

`public class AppShellViewModel : INotifyPropertyChanged
{
    public Command Load { get; }

    public ObservableCollection<ListData> _lists { get; set; }
    public ObservableCollection<ListData> Lists 
    {   
        get { return _lists; }
        set
        { 
            _lists = value;
            OnPropertyChanged();
        }
    }




    public AppShellViewModel()
    {

        Lists = new ObservableCollection<ListData>()
        {
            new ListData(){id=0,name="test",UserId=0},
            new ListData(){id=1,name="test1",UserId=1},
            new ListData(){id=2,name="test2",UserId=2},
            new ListData(){id=3,name="test3",UserId=3},
            new ListData(){id=4,name="test4",UserId=4}
        };
        Load = new Command(async () => await GetUserLists());
    }



    async Task GetUserLists()
    {
        for (int i = 5; i < 15; i++)
        {
            Lists.Add(new ListData {id=i, name=$"test{ i }", UserId=i });
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged([CallerMemberName] string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}`

然后我有 App Shell Collection View

`<Shell.FlyoutContent>
    <StackLayout BackgroundColor="#34495e">
        <Label Text="YOUR LISTS" FontSize="50" />
        <CollectionView ItemsSource="{Binding Lists}" >
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Padding="10" x:DataType="model:ListData">
                        <Label Text="{Binding name}" 
                            LineBreakMode="NoWrap"
                            FontSize="13" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</Shell.FlyoutContent>`

并且有页面Collection查看

`<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="ToDoApp.Views.AboutPage"
         xmlns:model="clr-namespace:ToDoApp.Models">
<StackLayout>
    <Button Text="Load" Command="{Binding Load}"/>
    <Label Text="{Binding error}"/>

    <CollectionView ItemsSource="{Binding Lists}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout Padding="10" x:DataType="model:ListData">
                    <Label Text="{Binding name}" 
                            LineBreakMode="NoWrap"
                            FontSize="13" />
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>
</ContentPage>`

更新前是这样的 Page before update Shell before update

更新后唯一改变的是内容页面,shell和以前一样 Page after update Shell after update

与 Jason 的评论相关。

不会一起改变

不是同一个实例 - BindingContexts 类似于这些:

// In AppShell.xaml.cs.
public AppShell()
{
    InitializeComponent();
    BindingContext = new AppShellViewModel();
}

// In AboutPage.xaml.cs.
public AboutPage()
{
    InitializeComponent();
    BindingContext = new AppShellViewModel();
}

好(在两个地方共享)

BindingContexts 是同一个实例:

// In AppShellViewModel.cs.
public class AppShellViewModel ...
{
    private static AppShellViewModel _it;
    public static AppShellViewModel It
    {
        get {
            if (_it == null)
                _it = new AppShellViewModel();
            return _it;
        }
    }
}

// In AppShell.xaml.cs.
public AppShell()
{
    InitializeComponent();
    BindingContext = AppShellViewModel.It;
}

// In AboutPage.xaml.cs.
public AboutPage()
{
    InitializeComponent();
    BindingContext = AppShellViewModel.It;
}