[英]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.