在等待事件时,WPF GUI不会在加载时更新

时间:2015-10-27 18:55:11

标签: c# wpf events .net-4.6

在我的应用程序中,我有rcvid=MsgReceive(chid, (void*)&message, sizeof(message),NULL);以下列方式打开:

view

这是ManagerView view = new ManagerView(); view.ShowDialog();

View

和我的<Window x:Class="WpfUpdateGui.ManagerView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:WpfUpdateGui"> <Window.DataContext> <local:ManagerViewModel /> </Window.DataContext> <i:Interaction.Triggers> <i:EventTrigger EventName="ContentRendered"> <i:InvokeCommandAction Command="{Binding LoadedCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> <TextBox Text="{Binding Messages}" />

ViewModel

我的问题是,我想显示的第一条消息(“正在开始”)仅在设置public class ManagerViewModel : INotifyPropertyChanged { /*INPC Members...*/ private string _messages; private static EventWaitHandle _timerWaiter; /*Constructor*/ public ManagerViewModel() { _timerWaiter = new EventWaitHandle(false, EventResetMode.AutoReset); LoadedCommand = new RelayCommand(StartProcess); } private void StartProcess() { Application.Current.Dispatcher.Invoke( DispatcherPriority.ApplicationIdle, new Action(() => { AddMessage("Starting"); Worker worker = new Worker(); worker.DidSomethingEvent += Worker_DidSomethingEvent; worker.DoSomeThing(); _timerWaiter.WaitOne(); AddMessage("Finished"); })); } private void AddMessage(string message) { Application.Current.Dispatcher.Invoke(() => Messages += $"\r\n{message}"); } private void Worker_DidSomethingEvent() { _timerWaiter.Set(); } public RelayCommand LoadedCommand { get; set; } public string Messages { get { return _messages; } set { if (value == Messages) return; _messages = value; OnPropertyChanged("Messages"); } } } public class Worker { public event Action DidSomethingEvent; public void DoSomeThing() { Thread.Sleep(2500); DidSomethingEvent(); } } 后显示,即使在EventWaitHandle电话之前添加也很难。

1 个答案:

答案 0 :(得分:0)

只需将ContentRendered替换为Loaded事件即可。 (它确实与我合作)。