MVVM。文本框永远不会随着 System.Threading.Thread.Sleep 而改变
Mvvm. Textbox never change with System.Threading.Thread.Sleep
我想更改文本框的值,但这段代码不起作用。我只看到最后一个值。
如果你想帮我复制粘贴代码。
非常感谢
这是XAML
<TextBox HorizontalAlignment="Left" Height="46"
Margin="4,4,4,4" VerticalAlignment="Top" Width="162"
Text="{Binding Path=Msg,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
这是 VB 代码。
Imports System.Threading
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System
Class MainWindow
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim x As New Abc
Me.DataContext = x
End Sub
End Class
Public Class Abc
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub OnPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Protected Sub OnNotifyPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Property _Msg As String
Public Property Msg As String
Get
Return _Msg
End Get
Set(value As String)
_Msg = value
OnPropertyChanged("Msg")
End Set
End Property
Private m_ButtonCommand As ICommand
Public Property ButtonCommand() As ICommand
Get
Return m_ButtonCommand
End Get
Set(value As ICommand)
m_ButtonCommand = value
End Set
End Property
Public Sub displayMessage(ByVal param As Object)
Msg = "How"
System.Threading.Thread.Sleep(1000)
Msg = "Are"
System.Threading.Thread.Sleep(1000)
Msg = "you"
System.Threading.Thread.Sleep(1000)
Msg = "?"
System.Threading.Thread.Sleep(1000)
End Sub
Private Function CandisplayMessage(ByVal param As Object) As Boolean
Return True
End Function
Public Sub New()
m_ButtonCommand = New DelegateCommand(AddressOf displayMessage, AddressOf CandisplayMessage)
End Sub
End Class
Public Class DelegateCommand
Implements ICommand
Private m_canExecute As Func(Of Object, Boolean)
Private m_executeAction As Action(Of Object)
Private m_canExecuteCache As Boolean
Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements ICommand.CanExecuteChanged
Public Sub New(ByVal executeAction As Action(Of Object), ByVal canExecute As Func(Of Object, Boolean))
Me.m_executeAction = executeAction
Me.m_canExecute = canExecute
End Sub
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Dim temp As Boolean = m_canExecute(parameter)
If m_canExecuteCache <> temp Then
m_canExecuteCache = temp
RaiseEvent CanExecuteChanged(Me, New EventArgs())
End If
Return m_canExecuteCache
End Function
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
m_executeAction(parameter)
End Sub
End Class
................................................
当您在 UI 线程上使用 Thread.Sleep 时,您会阻塞 UI 线程,因此 UI 中不会发生任何事情。如果您想在每条消息后显示 1 秒的等待时间,您有两种选择。
1 - 在异步方法中使用延迟命令。 https://msdn.microsoft.com/en-us/library/hh194873(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4
2 - 使用调度程序框架,它可以让调度程序在您的方法等待时工作。 http://www.codeproject.com/Articles/152137/DispatcherFrame-Look-in-Depth
不幸的是我不是VB程序员,但是c#中的代码应该是这样的。
异步示例:
public Task displayMessage(object param){
Msg = "How";
await Task.Delay(1000);
Msg = "Are";
await Task.Delay(1000);
Msg = "you";
await Task.Delay(1000);
Msg = "?";
await Task.Delay(1000);
}
DispatcherFrame 示例:
public void displayMessage(object param){
Msg = "How";
Wait(1000);
Msg = "Are";
Wait(1000);
Msg = "you";
Wait(1000);
Msg = "?";
Wait(1000);
}
public void Wait(int sleep)
{
var dFrame = new DispatcherFrame();
ThreadPool.QueueUserWorkItem(state => {
Thread.Sleep(sleep);
dFrame.Continue = false;
});
Dispatcher.PushFrame(dFrame);
}
我想更改文本框的值,但这段代码不起作用。我只看到最后一个值。 如果你想帮我复制粘贴代码。 非常感谢
这是XAML
<TextBox HorizontalAlignment="Left" Height="46"
Margin="4,4,4,4" VerticalAlignment="Top" Width="162"
Text="{Binding Path=Msg,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
这是 VB 代码。
Imports System.Threading
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System
Class MainWindow
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim x As New Abc
Me.DataContext = x
End Sub
End Class
Public Class Abc
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub OnPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Protected Sub OnNotifyPropertyChanged(propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Property _Msg As String
Public Property Msg As String
Get
Return _Msg
End Get
Set(value As String)
_Msg = value
OnPropertyChanged("Msg")
End Set
End Property
Private m_ButtonCommand As ICommand
Public Property ButtonCommand() As ICommand
Get
Return m_ButtonCommand
End Get
Set(value As ICommand)
m_ButtonCommand = value
End Set
End Property
Public Sub displayMessage(ByVal param As Object)
Msg = "How"
System.Threading.Thread.Sleep(1000)
Msg = "Are"
System.Threading.Thread.Sleep(1000)
Msg = "you"
System.Threading.Thread.Sleep(1000)
Msg = "?"
System.Threading.Thread.Sleep(1000)
End Sub
Private Function CandisplayMessage(ByVal param As Object) As Boolean
Return True
End Function
Public Sub New()
m_ButtonCommand = New DelegateCommand(AddressOf displayMessage, AddressOf CandisplayMessage)
End Sub
End Class
Public Class DelegateCommand
Implements ICommand
Private m_canExecute As Func(Of Object, Boolean)
Private m_executeAction As Action(Of Object)
Private m_canExecuteCache As Boolean
Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements ICommand.CanExecuteChanged
Public Sub New(ByVal executeAction As Action(Of Object), ByVal canExecute As Func(Of Object, Boolean))
Me.m_executeAction = executeAction
Me.m_canExecute = canExecute
End Sub
Public Function CanExecute(ByVal parameter As Object) As Boolean Implements ICommand.CanExecute
Dim temp As Boolean = m_canExecute(parameter)
If m_canExecuteCache <> temp Then
m_canExecuteCache = temp
RaiseEvent CanExecuteChanged(Me, New EventArgs())
End If
Return m_canExecuteCache
End Function
Public Sub Execute(ByVal parameter As Object) Implements ICommand.Execute
m_executeAction(parameter)
End Sub
End Class
................................................
当您在 UI 线程上使用 Thread.Sleep 时,您会阻塞 UI 线程,因此 UI 中不会发生任何事情。如果您想在每条消息后显示 1 秒的等待时间,您有两种选择。
1 - 在异步方法中使用延迟命令。 https://msdn.microsoft.com/en-us/library/hh194873(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4
2 - 使用调度程序框架,它可以让调度程序在您的方法等待时工作。 http://www.codeproject.com/Articles/152137/DispatcherFrame-Look-in-Depth
不幸的是我不是VB程序员,但是c#中的代码应该是这样的。
异步示例:
public Task displayMessage(object param){
Msg = "How";
await Task.Delay(1000);
Msg = "Are";
await Task.Delay(1000);
Msg = "you";
await Task.Delay(1000);
Msg = "?";
await Task.Delay(1000);
}
DispatcherFrame 示例:
public void displayMessage(object param){
Msg = "How";
Wait(1000);
Msg = "Are";
Wait(1000);
Msg = "you";
Wait(1000);
Msg = "?";
Wait(1000);
}
public void Wait(int sleep)
{
var dFrame = new DispatcherFrame();
ThreadPool.QueueUserWorkItem(state => {
Thread.Sleep(sleep);
dFrame.Continue = false;
});
Dispatcher.PushFrame(dFrame);
}