Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 2个viewmodels在同一个ObservableCollection上工作,1个viewmodel未更新_C#_Windows Phone 8.1_Win Universal App - Fatal编程技术网

C# 2个viewmodels在同一个ObservableCollection上工作,1个viewmodel未更新

C# 2个viewmodels在同一个ObservableCollection上工作,1个viewmodel未更新,c#,windows-phone-8.1,win-universal-app,C#,Windows Phone 8.1,Win Universal App,我有两个viewmodels,它们继承自同一个BaseViewModel,该BaseViewModel具有一个作为公共属性的ObservableCollection。 第一个viewmodel显示ObservableCollection,而第二个viewmodel允许更新集合 为什么第一个视图看不到更新的集合 public class BaseViewModel : INotifyPropertyChanged { private Playlist _currentPlaylist;

我有两个viewmodels,它们继承自同一个BaseViewModel,该BaseViewModel具有一个作为公共属性的ObservableCollection。 第一个viewmodel显示ObservableCollection,而第二个viewmodel允许更新集合

为什么第一个视图看不到更新的集合

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的第一个视图使用
CurrentPlaylist
databound。 第二个视图再次设置
CurrentPlaylist

public class ArtistPageViewModel : BaseViewModel
{

    public void PlaylistBtn_Clicked(ListView albumListView)
    {
        Library.AddSelectionToCurrentPlaylist(albumListView.SelectedItems.Cast<Album>());
        CurrentPlaylist = Library.CurrentPlaylist;
    }
}
my
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

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

公共密封部分类Hubbage:第页
{
私有只读NavigationHelper NavigationHelper;
私有静态HubPageViewModel _HubPageViewModel;//将其设置为静态
公共HubPageViewModel HubPageViewModel
{ 
得到
{
返回hubPageViewModel;
}
}
}
//添加了密钥

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

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


由于两个视图模型都指向相同的播放列表实例,并且该实例是一个
ObservaleCollection
,因此对
ObservaleCollection
实例的所有更改都将在两个视图模型之间共享,因为两个视图都绑定到它们正在观看的实例的“collection changed”通知。在您的示例中,
ArtistPageViewModel.playlibtn\u单击
不会更改
可观察集合
中的数据,而是更改集合本身。这会更改第二个视图正在查看的集合,但不会更改第一个视图的集合。

如何将viewmodel绑定到视图?@user1522548我已添加了相关代码。好的,这就是问题所在。你不能像那样绑定(或者至少我在过去没有成功地这么做过,除非很久以前MSFT改变了DTE绑定工具)。。。请尝试此操作,在视图中设置对视图模型的静态引用。您需要引用名称空间,然后在页面中找到它。从那里,您可以使用VS中的属性页来查找要绑定的集合。这是最好的方法,因为没有任何理由它不起作用。如果设计器可以看到属性,运行时也可以看到。您还可以在启动应用程序时通过查看输出窗口更好地了解此问题,您应该会看到一些绑定错误。是的,我说的是ViewModel。。。。无论如何,您都不希望此代码出现在视图的代码隐藏中。只需创建一个名为VMHubPage的新类,将所有代码放在那里。那么我应该更新我的
CurrentPlaylist
实例,并将这些更改推送到我的模型中?为什么我的收藏没有登记?我的
NotifyPropertyChanged(“CurrentPlaylist”)<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>
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>