OnPropertyChanged is null in WP8

本文关键字:WP8 in null is OnPropertyChanged | 更新日期: 2023-09-27 17:51:26

AcctionCommand

我的问题是,对于反映在用户界面中的变量的更改,PropertyChanged的值应该不同于null(Left(,因为我正在赋值。

我有一个通用类来处理按钮的点击事件

 public class ActionCommand : ICommand
{
    Action action;
    public ActionCommand(Action action)
    {
        this.action = action;
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public event EventHandler CanExecuteChanged;
    public void Execute(object parameter)
    {
        action();
    }
}

INotifyPropertyChanged我有一个类NotificationEnabledObject来通知PropertyChange中用户界面的Left值更改,它总是返回null,我不知道我做错了什么??

  public class NotificationEnabledObject : INotifyPropertyChanged
{
 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

ViewModel我有一个ViewModel类,它具有Left属性。

public class WordsViewModel : NotificationEnabledObject
{
    string left;
    public string Left
    {
        get { return left; }
        set
        {
            left = value;
            OnPropertyChanged();
        }
    }

主页.xaml

   <phone:PhoneApplicationPage
        x:Class="SpeechRecognition.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:core="http://schemas.microsoft.com/client/2007"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
          xmlns:vm="clr-namespace:SpeechRecognition.ViewModels"
        shell:SystemTray.IsVisible="True"
      DataContext="{Binding Source={StaticResource ViewModel}}">
       <StackPanel>
                       <Grid Height="Auto" Width="Auto">
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                  <TextBlock Grid.Row="0" FontSize="40" x:Name="txtWord" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="center">
                       <core:Run x:Name="rLeft" Text="{Binding Left, Mode=TwoWay}" />                                    
                        </TextBlock>
                            <Button Name="ReadNow" Grid.Row="1" Height="Auto" Content="Read Now" VerticalAlignment="Bottom" Click="ReadNow_Click">
                            </Button>
                        </Grid>     
            </StackPanel>

    </phone:PhoneApplicationPage>

这个动作是用户界面上按钮的点击事件,我不知道如何做到这一点,OnPropertyChanged总是空的,我想在运行程序时重复更改界面中变量的值Left

    ActionCommand getWordsCommand;
    public ActionCommand GetWordsCommand
    {
        get
        {
            if (getWordsCommand == null)
            {
                getWordsCommand = new ActionCommand(() =>
                    {  
                       Left = 10;
                    }
                });
            }
           return getWordsCommand;
          }

}

OnPropertyChanged is null in WP8

有一些问题

首先,需要在后面的视图代码中设置数据上下文

this.Datacontext = //your view model 

其次,WordsViewModel类需要实现INotifyPropertyChanged

第三,您的OnPropertyChanged签名错误。

它应该看起来像下面的示例

此外,您不应该使用实际的PropertChanged事件处理程序。它不安全。

相反,在OnPropertyChanged事件中克隆它

void OnPropertyChanged(String prop){
 PropertyChangedEventHandler handler = PropertyChanged;
 if(handler != null){
   PropertChanged(this,new PropertyChangedEventArgs(prop));
 }
}  

最后,在Left属性中调用OnPropertyChanged("Left");