[英]OnPropertyChanged is null in WP8
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,以将Left值的更改通知给PropertyChange中的用户界面,该用户界面始终返回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我有一个具有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>
此操作是用户界面上按钮的click事件,我不知道该如何执行此操作,OnPropertyChanged始终为null,我想在运行程序时反复更改界面Left中的变量值
ActionCommand getWordsCommand;
public ActionCommand GetWordsCommand
{
get
{
if (getWordsCommand == null)
{
getWordsCommand = new ActionCommand(() =>
{
Left = 10;
}
});
}
return getWordsCommand;
}
}
你有几个问题
首先,您需要在后面的视图代码中设置数据上下文
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.