在 WPF 中,当我加载大量元素的包装面板时,窗口在显示内容之前会暂停一段时间。我想添加等待提示,但是我找不到一种方法来检测包装面板何时完成呈现。
我已经尝试过 "loaded"、"sizechanged"、"initialized" 等方法,但都没有成功。有人对此问题有什么想法吗?
非常感谢!
我已经尝试过 "loaded"、"sizechanged"、"initialized" 等方法,但都没有成功。有人对此问题有什么想法吗?
非常感谢!
ContextIdle
的Dispatcher.BeginInvoke
调用。 private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
Dispatcher.BeginInvoke(new Action(() => DoSomething()), DispatcherPriority.ContextIdle, null);
}
private void DoSomething()
{
//This will get called after the UI is complete rendering
}
public static class DispatcherExtensions
{
public static void DoEvents(this Dispatcher dispatcher, DispatcherPriority priority = DispatcherPriority.Background)
{
DispatcherFrame frame = new DispatcherFrame();
DispatcherOperation dispatcherOperation = dispatcher.BeginInvoke(priority, (Action<DispatcherFrame>)ExitFrame, frame);
Dispatcher.PushFrame(frame);
if (dispatcherOperation.Status != DispatcherOperationStatus.Completed)
dispatcherOperation.Abort();
}
private static void ExitFrame(DispatcherFrame frame)
{
frame.Continue = false;
}
public static void Flush(this Dispatcher dispatcher, DispatcherPriority priority)
{
dispatcher.Invoke(()=> { }, priority);
}
}
回顾过去,我认为使用这个是一个糟糕的想法,因为它可能会导致难以解决的错误。
// this shows how bad it is to call Flush or DoEvents
int clicker = 0;
private void OnClick(object sender, RoutedEventArgs e)
{
if (clicker != 0)
{
// This is reachable... // Could be skipped for DispatcherPriority.Render but then again for render it seems to freeze sometimes.
}
clicker++;
Thread.Sleep(100);
this.Dispatcher.Flush(DispatcherPriority.Input);
//this.Dispatcher.DoEvents(DispatcherPriority.Render);
//this.Dispatcher.DoEvents(DispatcherPriority.Loaded);
//this.Dispatcher.DoEvents(DispatcherPriority.Input);
//this.Dispatcher.DoEvents(DispatcherPriority.Background);
clicker--;
}