PropertyChanged 事件处理程序在 Win Phone 8 应用程序中始终为 null
PropertyChanged event handler always null in Win Phone 8 Application
当我在这个简单的 Win Phone 8 应用程序(使用 VS 2012 Pro 构建 - 我拥有的)上单击 'Add Some Thing' 按钮时,什么都没有发生。为什么?
此示例代码的存储库位于 bitbucket.org 上:
TestItemsControlInWinPhone8App
MainPage.xaml
包含:
<phone:PhoneApplicationPage
x:Class="TestItemsControlInWinPhone8App.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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button x:Name="AddSomeThing"
Content="Add Some Thing"
Grid.Row="0"
Click="AddSomeThing_Click"/>
<ItemsControl x:Name="LotsOfThingsItemsControl"
Grid.Row="1"
ItemsSource="{Binding Mode=OneWay}"
FontSize="{StaticResource PhoneFontSizeSmall}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="Auto" Width="Auto"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Orange">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=Id, Mode=OneWay}"/>
<TextBlock Grid.Row="1"
Text="------------------------"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>
</Grid>
</phone:PhoneApplicationPage>
请注意,ItemsControl ItemsSource="{Binding Path=Things}
也已作为普通 ItemsControl ItemsSource="{Binding}"
进行了尝试。
两者的结果相同;显示前五个 Thing
个对象并单击 "Add Some Thing" 按钮将另一个 Thing
添加到 LotsOfThings
。
DataTemplate
的内容是 TextBlock
,实际上显示前 5 个 Thing
对象。
但是单击按钮不会更新显示,它仍然只显示原始的 5 个 Thing
对象。
(MainPage.xaml.cs
) 后面的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TestItemsControlInWinPhone8App.Resources;
namespace TestItemsControlInWinPhone8App
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.DataContext = new LotsOfThings(5);
}
private void AddSomeThing_Click(object sender, RoutedEventArgs e)
{
LotsOfThings lot = this.DataContext as LotsOfThings;
lot.Add(new Thing());
}
}
}
请注意,在 Page
构造函数中,this.DataContext = new LotsOfThings(5);
有效,当 Page
首次显示时,将显示 5 个 Thing
对象。
明确地说,什么不起作用 是 AddSomeThing_click()
按钮处理程序的后一次调用将添加另一个 Thing
到 LotsOfThings
但 仅显示 原来的 5 Thing
个对象;仅此而已,即使根据调试器 LotsOfThings
上存在更多 Thing
个对象。
我在使用调试器时注意到,每当调用 OnPropertyChanged(...)
时,handler
就是 null
。这显然很重要,但我不知道为什么会发生这种情况,此时遵循了我在网上搜索可以找到的所有补救帮助。
为什么?
Thing.cs
的内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestItemsControlInWinPhone8App
{
public class Thing : INotifyPropertyChanged
{
private string _Id = Guid.NewGuid().ToString();
public string Id
{
get
{
return _Id;
}
set { }
}
#region Constructor
public Thing()
{
this.OnPropertyChanged( "Id");
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string pPropertyName)
{
System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(pPropertyName));
}
}
#endregion
}
}
LotsOfThings.cs
的内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestItemsControlInWinPhone8App
{
class LotsOfThings : INotifyPropertyChanged, IList<Thing>
{
private List<Thing> _things = new List<Thing>();
public List<Thing> Things
{
get {
return _things;
}
set { }
}
public LotsOfThings( int pNumberOfThings)
{
for( int x = 0; x < pNumberOfThings; x++){
this.Add( new Thing());
}
OnPropertyChanged("Things");
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string pName)
{
System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(pName));
}
}
#endregion
#region IList<T> methods
public int IndexOf(Thing item)
{
return _things.IndexOf(item);
}
public void Insert(int index, Thing item)
{
_things.Insert(index, item);
}
public void RemoveAt(int index)
{
_things.RemoveAt(index);
}
public Thing this[int index]
{
get
{
return _things[index];
}
set
{
_things[index] = value;
}
}
public void Add(Thing item)
{
_things.Add(item);
OnPropertyChanged("Things");
}
public void Clear()
{
_things.Clear();
}
public bool Contains(Thing item)
{
return _things.Contains(item);
}
public void CopyTo(Thing[] array, int arrayIndex)
{
_things.CopyTo(array, arrayIndex);
}
public int Count
{
get { return _things.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(Thing item)
{
return _things.Remove(item);
}
public IEnumerator<Thing> GetEnumerator()
{
return _things.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _things.GetEnumerator();
}
#endregion
}
}
如果您只需要下载应用程序或使用更好的界面查看它,您可以在这里找到它:
TestItemsControlInWinPhone8App
谢谢。
PS。我已经阅读并且我认为遵循了我在 Whosebug 和网上其他地方找到的关于传递到 OnPropertyChanged()
方法的空处理程序和我能找到的 ItemsControl
用法的所有建议。
_things
需要 ObservableCollection<Thing>
。 List<T>
没有实现 INotifyCollectionChanged
,因此当它的内容改变时不会发出通知。 ObservableCollection<Thing>
会,这将使 UI 知道何时需要将项目添加到列表中。
简单、容易、标准的处理方式是将 ObservableCollection
公开为 属性。如果你用一个新的替换整个集合,raise PropertyChanged("Things")
;当项目为 added/removed 时,ObservableCollection
将引发适当的事件,而无需您执行任何操作。有经验的 WPF 人员在阅读您的代码时会知道他们在看什么。
为了让它按照您的想法工作,您必须在改变 Things
集合的方法中调用 OnPropertyChanged("Things")
;我还没有测试过,但我认为它应该可以工作(它可能不会工作的原因是 Things
返回的实际集合对象没有改变;控件可能会看到并选择不更新;但是作为我说我还没有测试过)。然后您可以将 Things
绑定到控件上的 ItemsSource
,也许它应该可以工作。但是你可以让其他 类 改变 Things
,因为它是 public。试图追查所有未解决的问题将是一团糟。使用起来更容易 ObservableCollection
.
如果你想将 LotsOfThings
本身绑定到 ItemsSource
,你将不得不在 LotsOfThings
上实现 INotifyCollectionChanged
,这将是一个真正的重写麻烦所有这些都是手工完成的,我不确定它能给你带来什么。您可以让 LotsOfThings
成为 ObservableCollection<Thing>
的子类——这样一开始您就可以免费获得完整且可靠的 INotifyCollectionChanged
实现。
Ed Plunkett 让我走上了正确的道路,但答案有点复杂,所以我列出了我在这个答案中学到的知识。
首先,我可以使用 ObservableCollection<T>
- Ed 是对的。但是我会失去一些我想要的 IList<T>
功能。此外,我试图遵循 XAML Deep Dive ... (min 40-49) 中的做法,但他们没有使用 ObservableCollection<T>
。
原来我错误地使用了 INotifyPropertyChanged
而不是 INotifyCollectionChanged
。第二个接口有一个稍微复杂的处理程序,记录在这个关于 calling OnCollectionChanged
的 Whosebug 问题的答案中。
在问这个问题之前,我的研究发现了很多方法也可以获取空事件处理程序。一种是使用拼写错误的 属性 名称调用处理程序(例如 OnPropertyChanged("thing")
,而你应该使用 OnPropertyChanged("Thing")
因为这就是 属性 的实际调用 - 假设你正在处理属性而不是集合。另一种获取空事件处理程序的方法是不将正确的对象绑定到正确的内容或容器控件。在这里,请看一下 "stack overflow C# why is my handler null".
为了最终解决这个问题的核心问题,我按照 Ed 的提示做了一些研究,让我更加熟悉 difference between List<T>
, ObservableCollection<T>
and INotifyPropertyChanged
并找到了那个优秀的页面。
希望对您有所帮助。感谢埃德;我所有的赞成票都给你。
P.S。我已经更新了我的测试代码的存储库以进行修复,并且 git tag
-ged original version as broken and the fixed version as fixed。
当我在这个简单的 Win Phone 8 应用程序(使用 VS 2012 Pro 构建 - 我拥有的)上单击 'Add Some Thing' 按钮时,什么都没有发生。为什么?
此示例代码的存储库位于 bitbucket.org 上: TestItemsControlInWinPhone8App
MainPage.xaml
包含:
<phone:PhoneApplicationPage
x:Class="TestItemsControlInWinPhone8App.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"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button x:Name="AddSomeThing"
Content="Add Some Thing"
Grid.Row="0"
Click="AddSomeThing_Click"/>
<ItemsControl x:Name="LotsOfThingsItemsControl"
Grid.Row="1"
ItemsSource="{Binding Mode=OneWay}"
FontSize="{StaticResource PhoneFontSizeSmall}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Height="Auto" Width="Auto"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Background="Orange">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=Id, Mode=OneWay}"/>
<TextBlock Grid.Row="1"
Text="------------------------"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
</Grid>
</Grid>
</phone:PhoneApplicationPage>
请注意,ItemsControl ItemsSource="{Binding Path=Things}
也已作为普通 ItemsControl ItemsSource="{Binding}"
进行了尝试。
两者的结果相同;显示前五个 Thing
个对象并单击 "Add Some Thing" 按钮将另一个 Thing
添加到 LotsOfThings
。
DataTemplate
的内容是 TextBlock
,实际上显示前 5 个 Thing
对象。
但是单击按钮不会更新显示,它仍然只显示原始的 5 个 Thing
对象。
(MainPage.xaml.cs
) 后面的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TestItemsControlInWinPhone8App.Resources;
namespace TestItemsControlInWinPhone8App
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
this.DataContext = new LotsOfThings(5);
}
private void AddSomeThing_Click(object sender, RoutedEventArgs e)
{
LotsOfThings lot = this.DataContext as LotsOfThings;
lot.Add(new Thing());
}
}
}
请注意,在 Page
构造函数中,this.DataContext = new LotsOfThings(5);
有效,当 Page
首次显示时,将显示 5 个 Thing
对象。
明确地说,什么不起作用 是 AddSomeThing_click()
按钮处理程序的后一次调用将添加另一个 Thing
到 LotsOfThings
但 仅显示 原来的 5 Thing
个对象;仅此而已,即使根据调试器 LotsOfThings
上存在更多 Thing
个对象。
我在使用调试器时注意到,每当调用 OnPropertyChanged(...)
时,handler
就是 null
。这显然很重要,但我不知道为什么会发生这种情况,此时遵循了我在网上搜索可以找到的所有补救帮助。
为什么?
Thing.cs
的内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestItemsControlInWinPhone8App
{
public class Thing : INotifyPropertyChanged
{
private string _Id = Guid.NewGuid().ToString();
public string Id
{
get
{
return _Id;
}
set { }
}
#region Constructor
public Thing()
{
this.OnPropertyChanged( "Id");
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string pPropertyName)
{
System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(pPropertyName));
}
}
#endregion
}
}
LotsOfThings.cs
的内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestItemsControlInWinPhone8App
{
class LotsOfThings : INotifyPropertyChanged, IList<Thing>
{
private List<Thing> _things = new List<Thing>();
public List<Thing> Things
{
get {
return _things;
}
set { }
}
public LotsOfThings( int pNumberOfThings)
{
for( int x = 0; x < pNumberOfThings; x++){
this.Add( new Thing());
}
OnPropertyChanged("Things");
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string pName)
{
System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(pName));
}
}
#endregion
#region IList<T> methods
public int IndexOf(Thing item)
{
return _things.IndexOf(item);
}
public void Insert(int index, Thing item)
{
_things.Insert(index, item);
}
public void RemoveAt(int index)
{
_things.RemoveAt(index);
}
public Thing this[int index]
{
get
{
return _things[index];
}
set
{
_things[index] = value;
}
}
public void Add(Thing item)
{
_things.Add(item);
OnPropertyChanged("Things");
}
public void Clear()
{
_things.Clear();
}
public bool Contains(Thing item)
{
return _things.Contains(item);
}
public void CopyTo(Thing[] array, int arrayIndex)
{
_things.CopyTo(array, arrayIndex);
}
public int Count
{
get { return _things.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(Thing item)
{
return _things.Remove(item);
}
public IEnumerator<Thing> GetEnumerator()
{
return _things.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _things.GetEnumerator();
}
#endregion
}
}
如果您只需要下载应用程序或使用更好的界面查看它,您可以在这里找到它: TestItemsControlInWinPhone8App
谢谢。
PS。我已经阅读并且我认为遵循了我在 Whosebug 和网上其他地方找到的关于传递到 OnPropertyChanged()
方法的空处理程序和我能找到的 ItemsControl
用法的所有建议。
_things
需要 ObservableCollection<Thing>
。 List<T>
没有实现 INotifyCollectionChanged
,因此当它的内容改变时不会发出通知。 ObservableCollection<Thing>
会,这将使 UI 知道何时需要将项目添加到列表中。
简单、容易、标准的处理方式是将 ObservableCollection
公开为 属性。如果你用一个新的替换整个集合,raise PropertyChanged("Things")
;当项目为 added/removed 时,ObservableCollection
将引发适当的事件,而无需您执行任何操作。有经验的 WPF 人员在阅读您的代码时会知道他们在看什么。
为了让它按照您的想法工作,您必须在改变 Things
集合的方法中调用 OnPropertyChanged("Things")
;我还没有测试过,但我认为它应该可以工作(它可能不会工作的原因是 Things
返回的实际集合对象没有改变;控件可能会看到并选择不更新;但是作为我说我还没有测试过)。然后您可以将 Things
绑定到控件上的 ItemsSource
,也许它应该可以工作。但是你可以让其他 类 改变 Things
,因为它是 public。试图追查所有未解决的问题将是一团糟。使用起来更容易 ObservableCollection
.
如果你想将 LotsOfThings
本身绑定到 ItemsSource
,你将不得不在 LotsOfThings
上实现 INotifyCollectionChanged
,这将是一个真正的重写麻烦所有这些都是手工完成的,我不确定它能给你带来什么。您可以让 LotsOfThings
成为 ObservableCollection<Thing>
的子类——这样一开始您就可以免费获得完整且可靠的 INotifyCollectionChanged
实现。
Ed Plunkett 让我走上了正确的道路,但答案有点复杂,所以我列出了我在这个答案中学到的知识。
首先,我可以使用 ObservableCollection<T>
- Ed 是对的。但是我会失去一些我想要的 IList<T>
功能。此外,我试图遵循 XAML Deep Dive ... (min 40-49) 中的做法,但他们没有使用 ObservableCollection<T>
。
原来我错误地使用了 INotifyPropertyChanged
而不是 INotifyCollectionChanged
。第二个接口有一个稍微复杂的处理程序,记录在这个关于 calling OnCollectionChanged
的 Whosebug 问题的答案中。
在问这个问题之前,我的研究发现了很多方法也可以获取空事件处理程序。一种是使用拼写错误的 属性 名称调用处理程序(例如 OnPropertyChanged("thing")
,而你应该使用 OnPropertyChanged("Thing")
因为这就是 属性 的实际调用 - 假设你正在处理属性而不是集合。另一种获取空事件处理程序的方法是不将正确的对象绑定到正确的内容或容器控件。在这里,请看一下 "stack overflow C# why is my handler null".
为了最终解决这个问题的核心问题,我按照 Ed 的提示做了一些研究,让我更加熟悉 difference between List<T>
, ObservableCollection<T>
and INotifyPropertyChanged
并找到了那个优秀的页面。
希望对您有所帮助。感谢埃德;我所有的赞成票都给你。
P.S。我已经更新了我的测试代码的存储库以进行修复,并且 git tag
-ged original version as broken and the fixed version as fixed。