PropertyChanged事件处理程序在OneWay规范中也始终为null

时间:2017-08-25 05:44:00

标签: c# mvvm uwp inotifypropertychanged

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

func refresh() -> Observable<Status> {
    let condition = { (status: Status) -> Bool in
        return status == .exists
    }
    let then = { (status: Status) -> Observable<Status> in
        return Observable.just(status)
    }
    return checkLocalStatus()
        .flatMapLatest(condition: condition, then: then, otherwise: self.attemptRemoteStatusOverride)
        .flatMapLatest(condition: condition, then: then, otherwise: self.attemptRemoteStatusUpdate)
}

我的ViewModel的命名空间由

导入
<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>

和页面资源我添加了我的ViewModel:

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

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

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

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

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:

private void RaisePropertyChanged(string propertyName)
        {

            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

我错在哪里?我知道我第34次问这个问题,但我找不到解决办法。在大多数情况下,他们忘记指定OneWay或TwoWay,但事实并非如此。

提前致谢....

戴夫

1 个答案:

答案 0 :(得分:0)

您的应用中至少有两个 ConfigurationViewModel个实例。

  1. App.ConfigurationViewModel
  2. 在页面ressources中定义为configurationVM
  3. 视图绑定到2.实例,在后面的代码中修改1.实例。