当我尝试更新LongListSelector控件的ItemsSource属性时,会发生ArgumentNullReference异常。
XAML代码:
<phone:LongListSelector x:Name="LongListSelectorMain" Margin="-12,0,0,0" ItemsSource="{Binding Items}" LayoutMode="List">
<phone:LongListSelector.DataContext>
<local:MainViewModel/>
</phone:LongListSelector.DataContext>
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,30">
<TextBlock FontSize="25" Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock FontSize="25" Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock FontSize="25" Text="{Binding LineThree}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
项目类
Public Class ItemViewModel
Implements INotifyPropertyChanged
Private _lineOne As String
''' <summary>
''' Proprietà ViewModel di esempio: questa proprietà viene utilizzata per visualizzare il relativo valore mediante un'associazione.
''' </summary>
''' <returns></returns>
Public Property LineOne() As String
Get
Return _lineOne
End Get
Set(ByVal value As String)
If Not value.Equals(_lineOne) Then
_lineOne = value
NotifyPropertyChanged("LineOne")
End If
End Set
End Property
Private _lineTwo As String
''' <summary>
''' Proprietà ViewModel di esempio: questa proprietà viene utilizzata per visualizzare il relativo valore mediante un'associazione.
''' </summary>
''' <returns></returns>
Public Property LineTwo() As String
Get
Return _lineTwo
End Get
Set(ByVal value As String)
If Not value.Equals(_lineTwo) Then
_lineTwo = value
NotifyPropertyChanged("LineTwo")
End If
End Set
End Property
Private _Skipass As Skipass
''' <summary>
''' Proprietà ViewModel di esempio: questa proprietà viene utilizzata per visualizzare il relativo valore mediante un'associazione.
''' </summary>
''' <returns></returns>
Public Property Skipass() As Skipass
Get
Return _Skipass
End Get
Set(ByVal value As Skipass)
If Not value.Equals(_Skipass) Then
_Skipass = value
NotifyPropertyChanged("Skipass")
End If
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal propertyName As String)
Dim handler As PropertyChangedEventHandler = PropertyChangedEvent
If Nothing IsNot handler Then
handler(Me, New PropertyChangedEventArgs(propertyName))
End If
End Sub
结束班
的DataContext
Public Class MainViewModel
Implements INotifyPropertyChanged
Public Sub New()
Me.Items = New ObservableCollection(Of ItemViewModel)()
End Sub
''' <summary>
''' Raccolta per oggetti ItemViewModel.
''' </summary>
Private _items As ObservableCollection(Of ItemViewModel)
Public Property Items() As ObservableCollection(Of ItemViewModel)
Get
Return _items
End Get
Private Set(ByVal value As ObservableCollection(Of ItemViewModel))
_items = value
End Set
End Property
Private _sampleProperty As String = "Sample Runtime Property Value"
''' <summary>
''' Proprietà ViewModel di esempio: questa proprietà viene utilizzata per visualizzare il relativo valore mediante un'associazione
''' </summary>
''' <returns></returns>
Public Property SampleProperty() As String
Get
Return _sampleProperty
End Get
Set(ByVal value As String)
If Not value.Equals(_sampleProperty) Then
_sampleProperty = value
NotifyPropertyChanged("SampleProperty")
End If
End Set
End Property
Private _isDataLoaded As Boolean
Public Property IsDataLoaded() As Boolean
Get
Return _isDataLoaded
End Get
Private Set(ByVal value As Boolean)
_isDataLoaded = value
End Set
End Property
''' <summary>
''' Crea e aggiunge alcuni oggetti ItemViewModel nella raccolta di elementi.
''' </summary>
Public Sub LoadData()
'Dati di esempio; sostituirli con dati reali
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime one", .LineTwo = "Maecenas praesent accumsan bibendum", .LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime two", .LineTwo = "Dictumst eleifend facilisi faucibus", .LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime three", .LineTwo = "Habitant inceptos interdum lobortis", .LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime four", .LineTwo = "Nascetur pharetra placerat pulvinar", .LineThree = "Ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime five", .LineTwo = "Maecenas praesent accumsan bibendum", .LineThree = "Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime six", .LineTwo = "Dictumst eleifend facilisi faucibus", .LineThree = "Pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime seven", .LineTwo = "Habitant inceptos interdum lobortis", .LineThree = "Accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime eight", .LineTwo = "Nascetur pharetra placerat pulvinar", .LineThree = "Pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime nine", .LineTwo = "Maecenas praesent accumsan bibendum", .LineThree = "Facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime ten", .LineTwo = "Dictumst eleifend facilisi faucibus", .LineThree = "Suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime eleven", .LineTwo = "Habitant inceptos interdum lobortis", .LineThree = "Habitant inceptos interdum lobortis nascetur pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime twelve", .LineTwo = "Nascetur pharetra placerat pulvinar", .LineThree = "Ultrices vehicula volutpat maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime thirteen", .LineTwo = "Maecenas praesent accumsan bibendum", .LineThree = "Maecenas praesent accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime fourteen", .LineTwo = "Dictumst eleifend facilisi faucibus", .LineThree = "Pharetra placerat pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime fifteen", .LineTwo = "Habitant inceptos interdum lobortis", .LineThree = "Accumsan bibendum dictumst eleifend facilisi faucibus habitant inceptos interdum lobortis nascetur pharetra placerat"})
'Me.Items.Add(New ItemViewModel() With {.LineOne = "runtime sixteen", .LineTwo = "Nascetur pharetra placerat pulvinar", .LineThree = "Pulvinar sagittis senectus sociosqu suscipit torquent ultrices vehicula volutpat maecenas praesent accumsan bibendum"})
Me.IsDataLoaded = True
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal propertyName As String)
Dim handler As PropertyChangedEventHandler = PropertyChangedEvent
If handler IsNot Nothing Then
handler(Me, New PropertyChangedEventArgs(propertyName))
End If
End Sub
结束班
正如您所看到的,我刚刚使用了Microsoft提供的默认示例......但说实话,我无法弄清楚为什么我会得到NullReference。
答案 0 :(得分:0)
您可以将XAML发布到您声明ItemsSource绑定的资源的位置吗?
这是一个猜测,因为你没有发布这个,但是如果你提供了一个Name和一个Key,那么资源没有初始化,这会导致你的null引用异常...
<UserControl.Resources>
<viewModel:FeedsViewModel x:Key="FeedsViewModel" x:Name="FeedsViewModel"/>
</UserControl.Resources>
如果您只声明一个密钥,那么它会自动初始化...
<UserControl.Resources>
<viewModel:FeedsViewModel x:Key="FeedsViewModel"/>
</UserControl.Resources>
但请记住,您必须通过用户控件的Resources字典引用它,而不是作为用户控件的属性。