我试着从我的datatemplate中调用我的模型类中的方法,但似乎没有编译。给出错误“对象引用未设置为对象的实例。”
在pointerPressed上我想在我的lvitem调用中使用X:bind调用方法“Poke”。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{x:Bind m_ViewModel.ListItems}" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:lvitem">
<TextBlock PointerPressed="{x:Bind Poke , Mode=OneWay }"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock Grid.Column="1" Text="{x:Bind m_ViewModel._Name, Mode=TwoWay}"/>
</Grid>
MyViewModel
Imports Windows.UI
Namespace MyViewModel
Public Class MyViewModel
Implements INotifyPropertyChanged
Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Name As String
Private List_Items As New ObservableCollection(Of lvitem)
Public Property _Name As String
Get
Return Name
End Get
Set(value As String)
Name = value
NotifyPropertyChanged()
End Set
End Property
Public Sub New()
Name = "Cater"
Dim lvi As New lvitem
Dim lvi2 As New lvitem
List_Items.Add(lvi)
List_Items.Add(lvi2)
End Sub
Public Property ListItems As ObservableCollection(Of lvitem)
Get
Return List_Items
End Get
Set(value As ObservableCollection(Of lvitem))
List_Items = value
End Set
End Property
Public Sub ClickMe()
_Name = "Clicked"
End Sub
End Class
End Namespace
CodeBehind
Public NotInheritable Class MainPage
Inherits Page
Public Property ViewModel() As MyViewModel.MyViewModel
Get
Return m_ViewModel
End Get
Set
m_ViewModel = Value
End Set
End Property
Private m_ViewModel As MyViewModel.MyViewModel
Public Sub New()
Me.InitializeComponent()
ViewModel = New MyViewModel.MyViewModel
End Sub
Private Sub Rectangle_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
End Sub
Private Sub TextBlock_PointerPressed(sender As Object, e As PointerRoutedEventArgs)
End Sub
End Class
Lvitem Class - Model
Public Class lvitem
Public Sub New()
End Sub
Public Sub Poke(sender As Object, e As Object)
Dim meint As Integer
meint = 3
End Sub
End Class
答案 0 :(得分:1)
给出错误&#34;对象引用未设置为对象的实例。&#34;
是的,我们在DataTemplate中找到了x:bind with x:DataType的一些问题。
以下是我们需要注意的几点:
确保Panel
包含DataTemplate
对于TextBlock
控件,如果我们需要使事件x:bind绑定起作用,则Text
属性必须设置为x:bind binding。
我简化了您的repro演示并标准化了一些命名问题:
<强> lvitem.vb:强>
Public Class lvitem
Public Sub Poke(sender As Object, e As PointerRoutedEventArgs)
Dim meint As Integer
meint = 3
End Sub
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
<强> MyViewModel.vb:强>
Imports Windows.UI
Namespace MyViewModel
Public Class MyViewModel
Implements INotifyPropertyChanged
Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _Name As String
Public List_Items As New ObservableCollection(Of lvitem)
Public Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
NotifyPropertyChanged()
End Set
End Property
Public Sub New()
Name = "Cater"
Dim lvi As New lvitem With {.Name = "Item1"}
Dim lvi2 As New lvitem With {.Name = "Item2"}
List_Items.Add(lvi)
List_Items.Add(lvi2)
End Sub
End Class
End Namespace
查看:强>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Margin="0,100,0,0" Grid.Column="0" ItemsSource="{x:Bind m_ViewModel.List_Items}" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:lvitem">
<Grid>
<TextBlock PointerPressed="{x:Bind Poke, Mode=OneWay}" Text="{x:Bind Name}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock Margin="0,100,0,0" Grid.Column="1" Text="{x:Bind m_ViewModel.Name, Mode=TwoWay}"/>
</Grid>
请注意 PointerPressed事件不会被触发,原因在于:Can't fire PointerPressed event from a Listbox in WinRT