Ich muss einen Block in die Benutzeroberfläche einfügen, wenn der Benutzer auf „Hinzufügen“ klickt Klicken Sie auf die Schaltfläche und machen Sie einen Screenshot der „neuesten“ Benutzeroberfläche.
UI-Markup:
Code: Select all
Code: Select all
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));
}
}
}
}
Was ich versucht habe
- Ändern Sie die Invoke-Methode in BeginInvoke, aber das funktioniert funktioniert nicht
- Ändern Sie die DispatcherPriority in ContextIdle, es funktioniert.
Aber die ContextIdle-Priorität ist zu niedrig. Ich mache mir Sorgen, dass dadurch weitere schwerwiegende Fehler in meinem Programm verursacht werden, da ich neben dem Erstellen eines Snapshots viele wichtige Dinge getan habe.
Code: Select all
Application.Current.Dispatcher.BeginInvoke(() => { using (var bitmap = SnippingTool.Screenshots()) { bitmap.Save("D:\\Desktop\\temp\\test.png", System.Drawing.Imaging.ImageFormat.Png); } }, System.Windows.Threading.DispatcherPriority.ContextIdle);