c# - 2 个 View 模型在同一个 ObservableCollection 上工作,1 个 View 模型未更新

标签 c# windows-phone-8.1 win-universal-app

我有 2 个 View 模型继承自同一个 BaseViewModel,它有一个 ObservableCollection 作为公共(public)属性。 第一个 View 模型显示 ObservableCollection,而第二个 View 模型允许更新集合。

为什么第一个 View 看不到更新的集合?

public class BaseViewModel : INotifyPropertyChanged
{
    private Playlist _currentPlaylist;
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public Playlist CurrentPlaylist
    {
        get
        {
            if (_currentPlaylist == null)
            {
                _currentPlaylist = _library.CurrentPlaylist;
            }
            return _currentPlaylist;
        }
        set
        {
            _currentPlaylist = value;
            NotifyPropertyChanged("CurrentPlaylist");
        }
    }

    public BaseViewModel()
    {
        _library = new Library();
        _dbContext = new MusicTrackerDataContext();
    }
}

第一个使用继承的 BaseViewModel 的 View 使用 CurrentPlaylist 数据绑定(bind)。 第二个 View 再次设置 CurrentPlaylist:

public class ArtistPageViewModel : BaseViewModel
{

    public void PlaylistBtn_Clicked(ListView albumListView)
    {
        Library.AddSelectionToCurrentPlaylist(albumListView.SelectedItems.Cast<Album>());
        CurrentPlaylist = Library.CurrentPlaylist;
    }
}

当我设置 CurrentPlaylist 时,看到我的 BaseViewModel 引发了 INotifyPropertyChanged 事件,我希望我的 ListView >CurrentPlaylist 已绑定(bind),已更新。

我做错了什么?

编辑

显示旧集合的 View 代码

public sealed partial class HubPage : Page
{
    private HubPageViewModel _hubPageViewModel;

    public HubPageViewModel HubPageViewModel
    { 
        get
        {
            return _hubPageViewModel;
        }
    }
}

我的 HubPageViewModel 的 XAML 代码

<Page
x:Class="MyProject.HubPage"
DataContext="{Binding HubPageViewModel, RelativeSource={RelativeSource Self}}"
mc:Ignorable="d">

<HubSection x:Uid="HubSection5" Header="Now Playing"
            DataContext="{Binding CurrentPlaylist}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
    <DataTemplate>
        <ListView 
            SelectionMode="None"
            IsItemClickEnabled="True"
            ItemsSource="{Binding Tracks}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Title}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </DataTemplate>
</HubSection>
        </HubSection>

编辑2

我已将我的代码更改为以下内容(根据评论)

public sealed partial class HubPage : Page
{
    private readonly NavigationHelper navigationHelper;
    private static HubPageViewModel _hubPageViewModel; // Made it static

    public HubPageViewModel HubPageViewModel
    { 
        get
        {
            return _hubPageViewModel;
        }
    }
}

<Page
x:Class="MyProject.HubPage"
DataContext="{Binding HubPageViewModel, RelativeSource={RelativeSource Self}}"
xmlns:vm="using:MyProject.ViewModels" // Added reference to my VM
mc:Ignorable="d">
    <ResourceDictionary>
        <vm:HubPageViewModel x:Name="hubPageViewModel"/> // Added the key
    </ResourceDictionary>
        <HubSection x:Uid="HubSection5" Header="Now Playing"
                    DataContext="{Binding Source={StaticResource hubPageViewModel}}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
            <DataTemplate>
                <ListView 
                    SelectionMode="None"
                    IsItemClickEnabled="True"
                    ItemsSource="{Binding CurrentPlaylist.Tracks}"
                    ItemClick="ItemView_ItemClick">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,27.5" Holding="StackPanel_Holding">
                                <TextBlock Text="{Binding Title}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </HubSection>

但是当我从我的第二个 View 模型返回时它仍然没有更新。

最佳答案

即使两个 View 模型继承了相同的基类,基类的状态也不会在两个 View 模型之间共享。也就是说,HubPageViewModelArtistPageViewModel 不共享相同的播放列表实例。它们是完全不同的属性。

由于两个 View 模型都指向同一个播放列表实例,并且该实例是一个ObservableCollection,因此对ObservableCollection 实例的所有更改都将在两个 View 模型之间共享,因为两个 View 都绑定(bind)到他们正在观看的实例的“集合已更改”通知。在您的示例中,ArtistPageViewModel.PlaylistBtn_Clicked 不会更改 ObservableCollection 中的数据,它会更改集合本身。这会更改第二个 View 正在观看的集合,但不会更改第一个 View 中的一个。

关于c# - 2 个 View 模型在同一个 ObservableCollection 上工作,1 个 View 模型未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26873177/

相关文章:

c# - 使用 TimeZoneInfo 调整时区的夏令时偏移

winrt-xaml - PasswordCredentials.Password 在 PasswordVault 中始终为空

c# - 通用Windows App C#-导航到同一页面的新实例时缓存页面的实例(和状态)

c# - 在树层次结构中具有深度限制的通用深度克隆

c# - LINQ 等于 - 它在幕后调用什么?

c# - Windows 通用应用程序中的反向地理编码

c# - 如何在通用 Windows 应用程序中垂直对齐 StackPanel?

c# - 通用 Windows 平台和 SignalR(无法加载文件或程序集“System.Net,版本 = 2.0.5.0”)

c# - UWP 中的振动电话

c# - 滚动时保持菜单项固定,而不是之前