在WP8中,OnPropertyChanged为null

时间:2014-03-24 01:18:57

标签: c# windows-phone-8 null viewmodel icommand

AcctionCommand

  

我的问题是,因为变量的变化反映在用户界面中,所以PropertyChanged的值应该与null(左)不同,因为我指定了一个值。

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

 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;

}

视图模型 我有一个具有Left属性的ViewModel类。

public class WordsViewModel : NotificationEnabledObject
{

    string left;
    public string Left
    {
        get { return left; }
        set
        {
            left = value;
            OnPropertyChanged();
        }

    }
  

MainPage.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始终为null,我想在运行程序时重复更改接口Left中变量的值< / p>

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

           return getWordsCommand;
          }

}

1 个答案:

答案 0 :(得分:0)

你有一些问题

首先,您的数据上下文需要在

后面的视图代码中设置
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");