2015-11-19 37 views
0

我想更改文本框的值,但此代碼不起作用。我只看到最後一個值。 如果你想幫我複製和粘貼代碼。 非常感謝Mvvm。文本框永遠不會改變與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 

............................................ ..

+0

你睡覺的UI線程。如果UI線程應該更新用戶界面***,當你讓它進入睡眠狀態時?*** – Will

回答

1

當您在UI線程上使用Thread.Sleep時,會阻塞UI線程,因此UI中不會發生任何事情。如果你想在每封郵件後顯示1秒鐘的等待時間,你有兩個選擇。

1 - 在Async方法中使用Delay命令。 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); 
} 
+0

嗨Hossein,非常感謝你的幫助 – Alan392

相關問題