[UWP] [VB] x:绑定到textBox.TextChanged工作,而textBox.TextChanging不 [英] [UWP][VB] x:Bind to textBox.TextChanged Works While textBox.TextChanging Does Not

查看:88
本文介绍了[UWP] [VB] x:绑定到textBox.TextChanged工作,而textBox.TextChanging不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我将网格视图中的文本框绑定(x:绑定)到ObservableCollection中的某些数据。 文本框包含我必须验证的各种数字数据(正整数,sbyte,double等) 当我x:将每个文本框绑定到一个用于TextChanging事件的事件处理程序
并验证它时,它会编译但我得到一个JIT未处理的win32异常。 但是,如果我x:绑定到TextChanged事件,它工作正常(虽然textchanged事件给出了不理想的视觉性能 - 不正确的字符
暂时可见)。 有谁知道为什么TextChanging事件会导致异常?


这是每个场景的相关XAML和代码;我的代码中唯一的差异如下所示:


TextChanged(工作):

< TextBox Text =" {x:Bind Value,Mode = TwoWay}" TextChanged =" {x:Bind textBox_TextChanged}" Name =" {x:Bind Name,Mode = OneTime}"宽度="95" Margin =" 0,0,0,0" /> 

和事件处理程序签名:

 Public Sub textBox_TextChanged(sender As Object,e As TextChangedEventArgs)
'验证码
End Sub


TextChanging(不起作用) ):

< TextBox Text =" {x:Bind Value,Mode = TwoWay}" TextChanging =" {x:Bind textBox_TextChanging}" Name =" {x:Bind Name,Mode = OneTime}"宽度="95" Margin =" 0,0,0,0" /> 

eventhandler签名:

 Public Sub textBox_TextChanging(sender as Object,e As TextBoxTextChangingEventArgs)
'验证码
End Sub



添加代码:


xaml:

< Grid Background =" {ThemeResource ApplicationPageBackgroundThemeBrush}"> 
< GridView ItemsSource =" {Binding}"余量= QUOT; 0,0,200,150" >

< GridView.ItemTemplate>
< DataTemplate x:DataType =" local:MyData">

< StackPanel Orientation =" Horizo​​ntal" >
< TextBlock Text =" {x:Bind DataName,Mode = OneWay}"宽度="500" TextAlignment = QUOT;右" />
< TextBox Text =" {x:Bind Value,Mode = TwoWay}" TextChanging =" {Binding textBox_TextChanging}"宽度="95" />

< / StackPanel>

< / DataTemplate>
< /GridView.ItemTemplate>

< / GridView>

< / Grid>

VB:


 Public NotInheritable Class MainPage 
Inherits Page
Public Sub New()
InitializeComponent()
DataContext = MyData.LoadFromDictionaryToObservableCollection
End Sub

结束类

公共类MyData
实现INotifyPropertyChanged
公共事件PropertyChanged As PropertyChangedEventHandler
私有事件INotifyPropertyChanged_PropertyChanged As PropertyChangedEventHandler实现INotifyPropertyChanged.PropertyChanged

Dim Local_DataContainer As Windows.Storage.ApplicationDataContainer = Windows.Storage.ApplicationData.Current.LocalSettings

Private _Value As String
Private _Type As DataType
Private _DataName As String
Public Sub New(DataName As String,Value As String,Type As DataType)
_DataName = DataName
_Value = Value
_Type = Type
End Sub
公共共享函数LoadFromDictionaryToObservableCollection()As ObservableCollection(Of MyData)
Dim CurrentCollection As New ObservableCollection(Of MyData)
CurrentCollection .Add(New MyData(" Item1"," 65.2",DataType._Double))
CurrentCollection.Add(New MyData(" Item2"," 15",DataType._Byte))
CurrentCollection.Add(New MyData(" Item3"," 855",DataType._Integer))
返回CurrentCollection
结束函数
Public Enum DataType
_Byte
_Double
_Integer
End Enum

Public ReadOnly Property DataName()As String
Get
Return _DataName
End Get

结束物业
公共物业价值()作为字符串
获取
返回_Value
结束获取
设置
_Value = Value
RaisePropertyChanged()
SaveData(Me.DataName,Me.Value)

结束集
结束物业
公开ReadOnly Property Type()As DataType
Get
Return _Type
End Get

End Property

Private Sub SaveData(DataName As String, DataValue As String)
Local_DataContainer.Values(DataName)= DataValue
End Sub

Private Sub RaisePropertyChanged(< CallerMemberName>可选调用者As String ="")
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(caller))
End Sub
Public Sub textBox_TextChanging(sender as Object,e As TextBoxTextChangingEventArgs)'处理textBox .TextChanging
'验证数据
结束Sub
Public Sub textBox_TextChanged(sender As Object,e As TextChangedEventArgs)'Handles textBox.TextChanging
'验证数据
End Sub
结束等级











解决方案

嗨rTolder,


在这种情况下,您需要使用绑定而不是x:bind。对于绑定,它具有UpdateSourceTrigger,这意味着例如当文本框的文本发生更改时,它可以立即更新数据源。但是对于x:bind,不支持UpdateSourceTrigger。
有关详细信息,请参阅
数据绑定深度
。在文章的底部,有一个比较绑定和x:bind。


最好的问候,


Neal Wang


Hi All,

I'm binding (x:Bind) textBoxes in a gridview to some data in an ObservableCollection.  The textboxes contain a variety of numeric data that I have to validate (positive integers, sbyte, double, etc.)  When I x:Bind each textbox to an eventhandler for the TextChanging event and to validate it, it compiles but I get a JIT unhandled win32 exception.  However, if I x:Bind to the TextChanged event, it works fine (though the textchanged event gives suboptimal visual performance -- incorrect characters are momentarily visible).  Does anyone know why the TextChanging event would result in an exception?

Here is the relevant XAML and code behind for each scenario; the only differences in my code are shown below:

TextChanged (works):

<TextBox Text = "{x:Bind Value, Mode = TwoWay}" TextChanged="{x:Bind textBox_TextChanged}" Name="{x:Bind Name , Mode = OneTime  }" Width = "95" Margin = "0,0,0,0"  />

and the eventhandler signature:

Public Sub textBox_TextChanged(sender As Object, e As TextChangedEventArgs) 
               '  Validation Code
End Sub

TextChanging (does not work):

<TextBox Text = "{x:Bind Value, Mode = TwoWay}" TextChanging="{x:Bind textBox_TextChanging}" Name="{x:Bind Name , Mode = OneTime  }" Width = "95" Margin = "0,0,0,0"  />

eventhandler signature:

 Public Sub textBox_TextChanging(sender As Object, e As TextBoxTextChangingEventArgs) 
        '  Validation Code
End Sub

Added Code:

xaml:

<Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <GridView ItemsSource = "{Binding}" Margin="0,0,200,150" >

            <GridView.ItemTemplate>
                <DataTemplate x:DataType = "local:MyData">

                    <StackPanel Orientation = "Horizontal" >
                        <TextBlock Text = "{x:Bind DataName , Mode = OneWay }" Width = "500" TextAlignment="Right"  />
                        <TextBox Text = "{x:Bind Value, Mode = TwoWay}" TextChanging="{Binding textBox_TextChanging}"  Width = "95"  />
           
                    </StackPanel>

                </DataTemplate>
            </GridView.ItemTemplate>

        </GridView>
  
    </Grid>

VB:

Public NotInheritable Class MainPage
    Inherits Page
    Public Sub New()
        InitializeComponent()
        DataContext = MyData.LoadFromDictionaryToObservableCollection
    End Sub

End Class

Public Class MyData
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler
    Private Event INotifyPropertyChanged_PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Dim Local_DataContainer As Windows.Storage.ApplicationDataContainer = Windows.Storage.ApplicationData.Current.LocalSettings

    Private _Value As String
    Private _Type As DataType
    Private _DataName As String
    Public Sub New(DataName As String, Value As String, Type As DataType)
        _DataName = DataName
        _Value = Value
        _Type = Type
    End Sub
    Public Shared Function LoadFromDictionaryToObservableCollection() As ObservableCollection(Of MyData)
        Dim CurrentCollection As New ObservableCollection(Of MyData)
        CurrentCollection.Add(New MyData("Item1", "65.2", DataType._Double))
        CurrentCollection.Add(New MyData("Item2", "15", DataType._Byte))
        CurrentCollection.Add(New MyData("Item3", "855", DataType._Integer))
        Return CurrentCollection
    End Function
    Public Enum DataType
        _Byte
        _Double
        _Integer
    End Enum

    Public ReadOnly Property DataName() As String
        Get
            Return _DataName
        End Get

    End Property
    Public Property Value() As String
        Get
            Return _Value
        End Get
        Set
            _Value = Value
            RaisePropertyChanged()
            SaveData(Me.DataName, Me.Value)

        End Set
    End Property
    Public ReadOnly Property Type() As DataType
        Get
            Return _Type
        End Get

    End Property

    Private Sub SaveData(DataName As String, DataValue As String)
        Local_DataContainer.Values(DataName) = DataValue
    End Sub

    Private Sub RaisePropertyChanged(<CallerMemberName> Optional caller As String = "")
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(caller))
    End Sub
    Public Sub textBox_TextChanging(sender As Object, e As TextBoxTextChangingEventArgs) 'Handles textBox.TextChanging
        'validate data
    End Sub
    Public Sub textBox_TextChanged(sender As Object, e As TextChangedEventArgs) 'Handles textBox.TextChanging
        'validate data
    End Sub
End Class


解决方案

Hi rTolder,

In this situation , you need to use binding instead of x:bind. For binding, it has UpdateSourceTrigger, which means for example when the text of Textbox changes, it can update the data source immediately. However for x:bind, UpdateSourceTrigger is not supported. For more information , you can refer to Data binding in depth. In the bottom of the article, there is a comparison between binding and x:bind.

Best Regards,

Neal Wang


这篇关于[UWP] [VB] x:绑定到textBox.TextChanged工作,而textBox.TextChanging不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆