Мне нужно вставить блок в пользовательский интерфейс, когда пользователь нажимает кнопку «Добавить», и сделать снимок экрана « новейший" пользовательский интерфейс.
Код пользовательского интерфейса
VM
using Caliburn.Micro;
using ShadowBot.UIAutomation.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp1
{
public class MainWindowContext : Screen
{
public BindableCollection Blocks { get; set; } = new BindableCollection();
public MainWindowContext()
{
Blocks.Add(new Block() { Text = DateTime.Now.ToString() });
}
public void Add()
{
Blocks.Add(new Block() { Text = DateTime.Now.ToString() });
NotifyOfPropertyChange(nameof(Blocks));
Application.Current.Dispatcher.Invoke(() =>
{
using (var bitmap = SnippingTool.Screenshots())
{
bitmap.Save("D:\\Desktop\\temp\\test.png", System.Drawing.Imaging.ImageFormat.Png);
}
});
}
public void Remove()
{
if (Blocks.Count > 0)
{
Blocks.RemoveAt(Blocks.Count - 1);
NotifyOfPropertyChange(nameof(Blocks));
}
}
}
public class Block : PropertyChangedBase
{
private string _text;
public string Text
{
get => _text;
set
{
_text = value;
NotifyOfPropertyChange(nameof(Text));
}
}
}
}
Проблема: новый блок вставлен в BindableCollection, но его рендеринг еще не завершен, когда я делаю снимок экрана
Что я пробовал
- Измените метод Ivoke на BeginInvoke, но это не работает
- Измените DispatcherPriority на ContextIdle, работает.
{
using (var bitmap = SnippingTool.Screenshots())
{
bitmap.Save("D:\\Desktop\\temp\\test.png", System.Drawing.Imaging.ImageFormat.Png);
}
},System.Windows.Threading.DispatcherPriority.ContextIdle);
But the **ContextIdle priority is too low**,i worry about it will cause more hard bugs into my program,bucause i did many important things besides of take snapShot.
**So,Is there any event or a callback in WPF to notify me after a Element finished rendering?**
Подробнее здесь: https://stackoverflow.com/questions/793 ... hed-in-wpf