Frage

AcctionCommand

My problem is that for the change of the variable are reflected in the user interface, the value of PropertyChanged should be different from null (Left) because I'm assigning a value.

I have a generic class to handle the click events of the buttons

 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 I have a class NotificationEnabledObject to notify the Left value change to the user interface in PropertyChange which always returns null, I do not know what I'm doing wrong??

  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 I have a ViewModel class that has the Left property.

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>

This action is the click event of the button on the user interface, I do not know how to make this work, OnPropertyChanged is always null, I want to change the value of the variable in interface Left repeatedly while running the program

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

           return getWordsCommand;
          }

}

War es hilfreich?

Lösung

You have a few problems

First, your data context needs to be set in your view code behind

this.Datacontext = //your view model 

Second, Your WordsViewModel class needs to implement INotifyPropertyChanged

Third your OnPropertyChanged Signature is wrong.

It should look like the below example

Also you shouldn't be using your actual PropertChanged event handler. Its not thread safe.

Instead, clone it within your OnPropertyChanged event

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

}  

Finally in your Left property invoke OnPropertyChanged("Left");

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top