0
當我嘗試更新LongListSelector控件的ItemsSource屬性時,會發生ArgumentNullReference異常。將數據綁定到LongListSelector
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
末級
正如你可以看到我剛剛使用默認的例子,微軟給出了......但是,爲什麼我收到NullReference說實話,我想不通。
你能從異常中發佈堆棧跟蹤嗎? –