从 ObservableCollection 中删除项目抛出 System.Windows.Markup.XamlParseException:[行:0 位置:0]
Remove items from ObservableCollection throws System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
Xaml当使用 LongListSelector
与数据绑定到 ObservableCollection
时,使用 VS2013 创建的 WP8.1 应用程序解析异常。
在从集合中删除一项之前一切都很好。奇怪的是,只有当 Canvas
位于 Grid.Row="1" 时才会产生异常。
如果 canvas
位于 Grid.Row="0" 上,则一切正常。
我用两个数据绑定做了一个简单的例子。
异常
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.ni.dll
Additional information: Value does not fall within the expected range.
- e {System.Windows.ApplicationUnhandledExceptionEventArgs} System.Windows.ApplicationUnhandledExceptionEventArgs
+ base {System.Windows.ApplicationUnhandledExceptionEventArgs} System.EventArgs {System.Windows.ApplicationUnhandledExceptionEventArgs}
- ExceptionObject {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
--- End of inner exception stack trace ---} System.Exception {System.Windows.Markup.XamlParseException}
- [System.Windows.Markup.XamlParseException] {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
--- End of inner exception stack trace ---} System.Windows.Markup.XamlParseException
+ base {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
--- End of inner exception stack trace ---} System.SystemException {System.Windows.Markup.XamlParseException}
LineNumber 0 int
LinePosition 0 int
- Non-Public members
+ base {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
--- End of inner exception stack trace ---} System.SystemException {System.Windows.Markup.XamlParseException}
_errorCode 2148468948 uint
_lineNumber 0 int
_linePosition 0 int
ErrorCode 2148468948 uint
- Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
+ [System.Collections.ListDictionaryInternal] {System.Collections.ListDictionaryInternal} System.Collections.ListDictionaryInternal
IsFixedSize false bool
IsReadOnly false bool
+ Keys {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection}
+ Values {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection}
HelpLink null string
HResult -2146233087 int
- InnerException {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Exception {System.Windows.Markup.XamlParseException}
+ [System.Windows.Markup.XamlParseException] {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Windows.Markup.XamlParseException
- Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
+ [System.Collections.ListDictionaryInternal] {System.Collections.ListDictionaryInternal} System.Collections.ListDictionaryInternal
IsFixedSize false bool
IsReadOnly false bool
+ Keys {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection}
+ Values {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection}
HelpLink null string
HResult -2146233087 int
+ InnerException null System.Exception
Message " [Line: 0 Position: 0]" string
Source "System.Windows" string
StackTrace " at MS.Internal.XcpImports.CheckHResult(UInt32 hr)\r\n at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)\r\n at System.Windows.UIElement.Measure(Size availableSize)\r\n at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)\r\n at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)\r\n at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)\r\n at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)\r\n at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)\r\n at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)\r\n at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)" string
+ Static members
+ Non-Public members
Message " [Line: 0 Position: 0]" string
Source null string
StackTrace null string
+ Static members
- Non-Public members
+ [System.Windows.Markup.XamlParseException] {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
--- End of inner exception stack trace ---} System.Windows.Markup.XamlParseException
_className "System.Windows.Markup.XamlParseException" string
+ _data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
_dynamicMethods null object
_exceptionMethod null System.Reflection.MethodBase
_exceptionMethodString null string
_helpURL null string
_HResult -2146233087 int
+ _innerException {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0]
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Exception {System.Windows.Markup.XamlParseException}
+ _ipForWatsonBuckets 0 System.UIntPtr
_message " [Line: 0 Position: 0]" string
_remoteStackIndex 0 int
_remoteStackTraceString null string
_source null string
_stackTrace null object
_stackTraceString null string
_watsonBuckets null object
_xcode -532462766 int
+ _xptrs 0 System.IntPtr
+ IPForWatsonBuckets 0 System.UIntPtr
IsTransient false bool
RemoteStackTrace null string
WatsonBuckets null object
Handled false bool
+ Non-Public members
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)
at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)
at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)
at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)
at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
> System.Windows.ni.dll!MS.Internal.XcpImports.CheckHResult(uint hr) Unknown
System.Windows.ni.dll!MS.Internal.XcpImports.SetValue(MS.Internal.IManagedPeerBase obj, System.Windows.DependencyProperty property, System.Windows.DependencyObject doh) Unknown
System.Windows.ni.dll!MS.Internal.XcpImports.SetValue(MS.Internal.IManagedPeerBase doh, System.Windows.DependencyProperty property, object obj) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.SetObjectValueToCore(System.Windows.DependencyProperty dp, object value) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.SetEffectiveValue(System.Windows.DependencyProperty property, ref System.Windows.EffectiveValueEntry newEntry, object newValue) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.RefreshExpression(System.Windows.DependencyProperty dp) Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.SendDataToTarget() Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.SourceAcquired() Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.Data.BindingExpression.DataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.OnPropertyChanged(System.Windows.DependencyProperty dp) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp, object oldValue, object newValue) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) Unknown
System.Windows.ni.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value, bool allowReadOnlySet) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.DataContext.set(object value) Unknown
[Native to Managed Transition]
System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetPropertyOrAddItem(object target, System.Reflection.PropertyInfo propertyInfo, object value, int typeIndex, string propertyName) Unknown
System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetNonAttachedProperty(object target, string propertyName, object propertyValue, int typeIndex) Unknown
System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetValueToProperty(object target, string propertyName, object propertyValue, int typeIndex, string attachedDPOwnerNamespace, string attachedDPOwnerAssembly) Unknown
System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetProperty(System.IntPtr nativeTarget, string propertyName, ref MS.Internal.CValue propertyValue, int typeIndex, string attachedDPOwnerNamespace, string attachedDPOwnerAssembly) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.ni.dll!MS.Internal.XcpImports.UIElement_Measure(System.Windows.UIElement element, System.Windows.Size availableSize) Unknown
System.Windows.ni.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize) Unknown
Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Microsoft.Phone.Controls.Iterator iterator, Microsoft.Phone.Controls.LongListSelector.RealizeOptions options) Unknown
Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(Microsoft.Phone.Controls.LongListSelector.RealizedRange nextRange, double bottom) Unknown
Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.AppendElement(Microsoft.Phone.Controls.LongListSelector.RealizedRange range, Microsoft.Phone.Controls.LongListSelector.RealizedRange nextRange, double bottom) Unknown
Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.HealElements(System.Windows.Rect requireRect, System.Windows.Rect allowRect, out bool areBoundsUpdated) Unknown
Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.Balance(bool containerViewport) Unknown
Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.MeasureOverride(System.Windows.Size availableSize) Unknown
System.Windows.ni.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, double inWidth, double inHeight, out double outWidth, out double outHeight) Unknown
Xaml 例子
<phone:PivotItem x:Name="PivotMyPage" Header="My Page" >
<Grid x:Name="LayoutMyPage" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<phone:LongListSelector Name="LLS_MyItems" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding myItemCollection}" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate x:Name="DataTemplateMyPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Title}" HorizontalAlignment="Left" />
<Canvas Grid.Row="1" Grid.Column="1" Width="50" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center" >
<Polyline Canvas.Left="0" Canvas.Top="0" Points="{Binding PC}" Stroke="Gray" StrokeThickness="4" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Canvas>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
代码隐藏
bool mainPivotLoaded = false;
private ObservableCollection<MyItem> myItemCollection = new ObservableCollection<MyItem>();
public MainPage()
{
InitializeComponent();
DataContext = App.ViewModel;
LLS_MyItems.ItemsSource = myItemCollection;
for (int x = 0; x < 20; x++)
{
MyItem i = new MyItem();
i.Title = "Hello" + x.ToString();
myItemCollection.Add(i);
}
DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(Dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 1, 0);
dt.Start();
}
private void Dt_Tick(object sender, EventArgs e)
{
if (myItemCollection.Count > 0 && mainPivotLoaded)
myItemCollection.RemoveAt(0);
}
private void MainPivot_Loaded(object sender, RoutedEventArgs e)
{
mainPivotLoaded = true;
}
class MyItem : INotifyPropertyChanged
{
public string Title { get; set; }
public PointCollection PC { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public MyItem()
{
PC = new PointCollection();
PC.Add(new Point(1, 1));
PC.Add(new Point(10, 10));
}
}
这个绑定很周到
Points="{Binding PC}"
时会发生什么
- PC 为空?
- 绑定找不到PC?
- PC 不为空但没有项目?
- PC 有物品
我推测代码处理 #4,但不处理 1 - 3。通过对所有情况进行编码并至少使用有效列表是明智的。
Xaml当使用 LongListSelector
与数据绑定到 ObservableCollection
时,使用 VS2013 创建的 WP8.1 应用程序解析异常。
在从集合中删除一项之前一切都很好。奇怪的是,只有当 Canvas
位于 Grid.Row="1" 时才会产生异常。
如果 canvas
位于 Grid.Row="0" 上,则一切正常。
我用两个数据绑定做了一个简单的例子。
异常
A first chance exception of type 'System.ArgumentException' occurred in System.Windows.ni.dll Additional information: Value does not fall within the expected range. - e {System.Windows.ApplicationUnhandledExceptionEventArgs} System.Windows.ApplicationUnhandledExceptionEventArgs + base {System.Windows.ApplicationUnhandledExceptionEventArgs} System.EventArgs {System.Windows.ApplicationUnhandledExceptionEventArgs} - ExceptionObject {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---} System.Exception {System.Windows.Markup.XamlParseException} - [System.Windows.Markup.XamlParseException] {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---} System.Windows.Markup.XamlParseException + base {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---} System.SystemException {System.Windows.Markup.XamlParseException} LineNumber 0 int LinePosition 0 int - Non-Public members + base {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---} System.SystemException {System.Windows.Markup.XamlParseException} _errorCode 2148468948 uint _lineNumber 0 int _linePosition 0 int ErrorCode 2148468948 uint - Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} + [System.Collections.ListDictionaryInternal] {System.Collections.ListDictionaryInternal} System.Collections.ListDictionaryInternal IsFixedSize false bool IsReadOnly false bool + Keys {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} + Values {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} HelpLink null string HResult -2146233087 int - InnerException {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Exception {System.Windows.Markup.XamlParseException} + [System.Windows.Markup.XamlParseException] {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Windows.Markup.XamlParseException - Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} + [System.Collections.ListDictionaryInternal] {System.Collections.ListDictionaryInternal} System.Collections.ListDictionaryInternal IsFixedSize false bool IsReadOnly false bool + Keys {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} + Values {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} System.Collections.ICollection {System.Collections.ListDictionaryInternal.NodeKeyValueCollection} HelpLink null string HResult -2146233087 int + InnerException null System.Exception Message " [Line: 0 Position: 0]" string Source "System.Windows" string StackTrace " at MS.Internal.XcpImports.CheckHResult(UInt32 hr)\r\n at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)\r\n at System.Windows.UIElement.Measure(Size availableSize)\r\n at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options)\r\n at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom)\r\n at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom)\r\n at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated)\r\n at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport)\r\n at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize)\r\n at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)" string + Static members + Non-Public members Message " [Line: 0 Position: 0]" string Source null string StackTrace null string + Static members - Non-Public members + [System.Windows.Markup.XamlParseException] {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) --- End of inner exception stack trace ---} System.Windows.Markup.XamlParseException _className "System.Windows.Markup.XamlParseException" string + _data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} _dynamicMethods null object _exceptionMethod null System.Reflection.MethodBase _exceptionMethodString null string _helpURL null string _HResult -2146233087 int + _innerException {System.Windows.Markup.XamlParseException: [Line: 0 Position: 0] at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)} System.Exception {System.Windows.Markup.XamlParseException} + _ipForWatsonBuckets 0 System.UIntPtr _message " [Line: 0 Position: 0]" string _remoteStackIndex 0 int _remoteStackTraceString null string _source null string _stackTrace null object _stackTraceString null string _watsonBuckets null object _xcode -532462766 int + _xptrs 0 System.IntPtr + IPForWatsonBuckets 0 System.UIntPtr IsTransient false bool RemoteStackTrace null string WatsonBuckets null object Handled false bool + Non-Public members at MS.Internal.XcpImports.CheckHResult(UInt32 hr) at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize) at System.Windows.UIElement.Measure(Size availableSize) at Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Iterator`1 iterator, RealizeOptions options) at Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.AppendElement(RealizedRange range, RealizedRange nextRange, Double bottom) at Microsoft.Phone.Controls.LongListSelector.HealElements(Rect requireRect, Rect allowRect, Boolean& areBoundsUpdated) at Microsoft.Phone.Controls.LongListSelector.Balance(Boolean containerViewport) at Microsoft.Phone.Controls.LongListSelector.MeasureOverride(Size availableSize) at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight) > System.Windows.ni.dll!MS.Internal.XcpImports.CheckHResult(uint hr) Unknown System.Windows.ni.dll!MS.Internal.XcpImports.SetValue(MS.Internal.IManagedPeerBase obj, System.Windows.DependencyProperty property, System.Windows.DependencyObject doh) Unknown System.Windows.ni.dll!MS.Internal.XcpImports.SetValue(MS.Internal.IManagedPeerBase doh, System.Windows.DependencyProperty property, object obj) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.SetObjectValueToCore(System.Windows.DependencyProperty dp, object value) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.SetEffectiveValue(System.Windows.DependencyProperty property, ref System.Windows.EffectiveValueEntry newEntry, object newValue) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.RefreshExpression(System.Windows.DependencyProperty dp) Unknown System.Windows.ni.dll!System.Windows.Data.BindingExpression.SendDataToTarget() Unknown System.Windows.ni.dll!System.Windows.Data.BindingExpression.SourceAcquired() Unknown System.Windows.ni.dll!System.Windows.Data.BindingExpression.System.Windows.IDataContextChangedListener.OnDataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.Data.BindingExpression.DataContextChanged(object sender, System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.OnDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.OnAncestorDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.NotifyDataContextChanged(System.Windows.DataContextChangedEventArgs e) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.OnPropertyChanged(System.Windows.DependencyProperty dp) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp, object oldValue, object newValue) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.DependencyProperty property, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, System.Windows.DependencyObject.ValueOperation operation) Unknown System.Windows.ni.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value, bool allowReadOnlySet) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.DataContext.set(object value) Unknown [Native to Managed Transition] System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetPropertyOrAddItem(object target, System.Reflection.PropertyInfo propertyInfo, object value, int typeIndex, string propertyName) Unknown System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetNonAttachedProperty(object target, string propertyName, object propertyValue, int typeIndex) Unknown System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetValueToProperty(object target, string propertyName, object propertyValue, int typeIndex, string attachedDPOwnerNamespace, string attachedDPOwnerAssembly) Unknown System.Windows.ni.dll!MS.Internal.FrameworkCallbacks.SetProperty(System.IntPtr nativeTarget, string propertyName, ref MS.Internal.CValue propertyValue, int typeIndex, string attachedDPOwnerNamespace, string attachedDPOwnerAssembly) Unknown [Native to Managed Transition] [Managed to Native Transition] System.Windows.ni.dll!MS.Internal.XcpImports.UIElement_Measure(System.Windows.UIElement element, System.Windows.Size availableSize) Unknown System.Windows.ni.dll!System.Windows.UIElement.Measure(System.Windows.Size availableSize) Unknown Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.RealizeDisplayItem(Microsoft.Phone.Controls.Iterator iterator, Microsoft.Phone.Controls.LongListSelector.RealizeOptions options) Unknown Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.RealizedRange.Append(Microsoft.Phone.Controls.LongListSelector.RealizedRange nextRange, double bottom) Unknown Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.AppendElement(Microsoft.Phone.Controls.LongListSelector.RealizedRange range, Microsoft.Phone.Controls.LongListSelector.RealizedRange nextRange, double bottom) Unknown Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.HealElements(System.Windows.Rect requireRect, System.Windows.Rect allowRect, out bool areBoundsUpdated) Unknown Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.Balance(bool containerViewport) Unknown Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.LongListSelector.MeasureOverride(System.Windows.Size availableSize) Unknown System.Windows.ni.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, double inWidth, double inHeight, out double outWidth, out double outHeight) Unknown
Xaml 例子
<phone:PivotItem x:Name="PivotMyPage" Header="My Page" >
<Grid x:Name="LayoutMyPage" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<phone:LongListSelector Name="LLS_MyItems" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding myItemCollection}" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate x:Name="DataTemplateMyPage">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Title}" HorizontalAlignment="Left" />
<Canvas Grid.Row="1" Grid.Column="1" Width="50" Height="50" HorizontalAlignment="Center" VerticalAlignment="Center" >
<Polyline Canvas.Left="0" Canvas.Top="0" Points="{Binding PC}" Stroke="Gray" StrokeThickness="4" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Canvas>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
代码隐藏
bool mainPivotLoaded = false;
private ObservableCollection<MyItem> myItemCollection = new ObservableCollection<MyItem>();
public MainPage()
{
InitializeComponent();
DataContext = App.ViewModel;
LLS_MyItems.ItemsSource = myItemCollection;
for (int x = 0; x < 20; x++)
{
MyItem i = new MyItem();
i.Title = "Hello" + x.ToString();
myItemCollection.Add(i);
}
DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(Dt_Tick);
dt.Interval = new TimeSpan(0, 0, 0, 1, 0);
dt.Start();
}
private void Dt_Tick(object sender, EventArgs e)
{
if (myItemCollection.Count > 0 && mainPivotLoaded)
myItemCollection.RemoveAt(0);
}
private void MainPivot_Loaded(object sender, RoutedEventArgs e)
{
mainPivotLoaded = true;
}
class MyItem : INotifyPropertyChanged
{
public string Title { get; set; }
public PointCollection PC { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public MyItem()
{
PC = new PointCollection();
PC.Add(new Point(1, 1));
PC.Add(new Point(10, 10));
}
}
这个绑定很周到
Points="{Binding PC}"
时会发生什么
- PC 为空?
- 绑定找不到PC?
- PC 不为空但没有项目?
- PC 有物品
我推测代码处理 #4,但不处理 1 - 3。通过对所有情况进行编码并至少使用有效列表是明智的。