文本框不会在主窗口WPF上更新
我有一个从SMS调制解调器截取SMS的程序,然后它将从另一个类更新主窗口上的文本框不会在主窗口WPF上更新,wpf,vb.net,Wpf,Vb.net,我有一个从SMS调制解调器截取SMS的程序,然后它将从另一个类更新主窗口上的文本框。我的代码是: 主窗口: Class MainWindow Public WithEvents SMScon As SMSComms Private Delegate Sub GetDisplay(ByVal SMS As SMSMessage) Public Sub GetFromSMSComms(SMS As SMSMessage) Handles SMScon.SMSListChanged If t
文本框
。我的代码是:
主窗口:
Class MainWindow
Public WithEvents SMScon As SMSComms
Private Delegate Sub GetDisplay(ByVal SMS As SMSMessage)
Public Sub GetFromSMSComms(SMS As SMSMessage) Handles SMScon.SMSListChanged
If tb_recievedmessages.Dispatcher.CheckAccess() Then
DisplaySMS(SMS)
Else
tb_recievedmessages.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New GetDisplay(AddressOf DisplaySMS), SMS)
End If
End Sub
Private Sub DisplaySMS(ByVal SMS As SMSMessage)
Debug.WriteLine("Writing To TextBox")
tb_recievedmessages.Text() = SMS.MessageText
End Sub
End Class
在不同类别中捕获的SMS:
SMSComms.vb:
Public Class SMSComms
Public Event SMSListChanged(ByVal SMS As SMSMessage)
Private Delegate Sub GetDisplaySMSCallback(ByVal SMS As List(Of String))
Public Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
Dim rxString As String
Dim index As Integer
Dim newMsg As New SMSMessage
RemoveHandler SMSPort.DataReceived, AddressOf DataReceivedHandler
Thread.Sleep(100)
rxString = SMSPort.ReadExisting()
rxString = TrimCRLF(rxString)
index = NewSMSIndex(rxString)
If index > -1 Then
newMsg = ReadSMS(index)
If newMsg.Index > -1 Then
AddHandler SMSListChanged, AddressOf Main.GetFromSMSComms
RaiseEvent SMSListChanged(newMsg)
End If
End If
RemoveHandler SMSListChanged, AddressOf Main.GetFromSMSComms
AddHandler SMSPort.DataReceived, AddressOf DataReceivedHandler
End Sub
End Class
现在SMS对象从SMSComms类传递到主窗口,我使用Dispatcher.BeginInvoke()调用主线程,但当它到达写入textbox的子线程时,textbox不会在主UI上更新
谁能告诉我为什么??或者如何使其更新???您是否设置了一些断点并逐步通过?你的留言正确吗?消息是否为空,因此文本框是否正确?textbox.text不是属性吗
tb_receivedMessages.Text()=blah
对我来说似乎不对。我的朋友,你的代码太多了。请阅读此页以了解如何减少它,然后请编辑您的问题以实现此目的。顺便说一句,在您使用WPF编写一行代码之前,请先学习MVVM。发送到tb_ReceivedMessages.Text()的消息是正确的SMS。Messagetext是字符串,所以我不明白它为什么不写信给textbox@JohnGardnerVisual Basic允许在属性设置器上使用参数