属性更改事件处理程序在单向规范中也始终为 null



我在XAML中创建了一个简单的ListView,它应该绑定到ObservablaCollection:

<PivotItem x:Uid="pvItemMusic" Header="Music">
<StackPanel>
<TextBlock Name="tbSelectMusicHeader" Text="Select directories that should be included into your library" FontSize="18" Margin="20"></TextBlock>
<Button Name="btnSelectSourcePath" Content="Add path" Margin="30,10,0,10" Click="btnSelectSourcePath_Click"></Button>
<ListView Name="lvPathConfiguration" DataContext="{StaticResource configurationVM}" ItemsSource="{Binding MusicBasePathList, Mode=OneWay}">
<ListView.ItemTemplate>
<DataTemplate>
<RelativePanel>
<TextBlock Name="tbPath" Text="{Binding Mode=OneWay}" RelativePanel.AlignTopWithPanel="True" VerticalAlignment="Center" Width="400" Margin="20"></TextBlock>
<Button Name="btnRemovePath" x:Uid="btnRemovePath" Content="Remove" RelativePanel.RightOf="tbPath" Margin="10" Height="48"></Button>
</RelativePanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</PivotItem>

我的视图模型的命名空间由

xmlns:applicationVM="using:Crankdesk.CrankHouseControl.ViewModel.Application"

和我添加的视图模型的页面资源:

<Page.Resources>
<applicationVM:ConfigurationViewModel x:Key="configurationVM"></applicationVM:ConfigurationViewModel>
</Page.Resources>

btnSelectSourcePath 应该在存储在 ViewModel 中的源路径列表中添加一个路径,这将在 CodeBehind 中完成:

private async void btnSelectSourcePath_Click(object sender, RoutedEventArgs e)
{
FolderPicker picker = new Windows.Storage.Pickers.FolderPicker();
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
picker.FileTypeFilter.Add("*");
StorageFolder folder = await picker.PickSingleFolderAsync();
if (folder != null)
{
// Save path to configuration
App.ConfigurationViewModel.MusicBasePathList.Add(folder.Path);
}
}

在 ViewModel 中,使用 "INotifyPropertyChanged" 事件,我使用 ObersableCollection 的 "CollectionChanged" 事件来触发 PropertyChanged 事件。当我在调试模式下添加路径时,将执行 RaisePropertyChanged 方法,但"处理程序"属性始终为 NULL。

private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

这是我的整个视图模型:

namespace Crankdesk.CrankHouseControl.ViewModel.Application
{
public class ConfigurationViewModel : INotifyPropertyChanged
{    
private ObservableCollection<string> _musicBasePathList;
public ObservableCollection<string> MusicBasePathList
{
get
{
return _musicBasePathList; 
}
set
{
_musicBasePathList = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public ConfigurationViewModel()
{
_musicBasePathList = new ObservableCollection<string>();
_musicBasePathList.CollectionChanged += _musicBasePathList_CollectionChanged;
}
private void _musicBasePathList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{   
RaisePropertyChanged(nameof(MusicBasePathList));
}

private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

我在这里做错了什么?我知道我在这里第34次问这个问题,但我没有找到解决方案。在大多数情况下,他们忘记指定 OneWay 或 TwoWay,但这里的情况并非如此。

提前谢谢....

戴夫

应用程序中至少有两个ConfigurationViewModel实例。

  1. App.ConfigurationViewModel
  2. 在页面资源中定义为configurationVM

视图绑定到 2. 实例,并在您后面的代码中修改 1. 实例。

最新更新