0

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 を取得する理由がわかりません。

4

1 に答える 1

0

ItemsSource がバインドするリソースを宣言している XAML を投稿できますか?

これを投稿していないため、これは推測ですが、名前とキーを指定すると、リソースが初期化されず、null 参照例外が発生します...

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

キーのみを宣言すると、自動的に初期化されます...

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

ただし、ユーザー コントロールのプロパティとしてではなく、ユーザー コントロールの Resources ディクショナリを介して参照する必要があることに注意してください。

于 2013-01-22T06:01:27.750 に答える