2013-01-05 98 views
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說實話,我想不通。

+0

你能從異常中發佈堆棧跟蹤嗎? –

回答

0

你可以發佈XAML你聲明資源哪些ItemsSource綁定到?

這是一個猜測,因爲你還沒有發佈,但如果你提供一個名稱以及一個鍵,然後資源將不會被初始化,這將導致你的空引用異常......

<UserControl.Resources> 
    <viewModel:FeedsViewModel x:Key="FeedsViewModel" x:Name="FeedsViewModel"/> 
</UserControl.Resources> 

如果只聲明瞭一個鍵,然後它會自動初始化...

<UserControl.Resources> 
    <viewModel:FeedsViewModel x:Key="FeedsViewModel"/> 
</UserControl.Resources> 

但是記住你將不得不通過用戶控制的資源字典引用它,而不是在用戶控件的屬性。

相關問題