0
美好的一天。我嘗試創建圖形編輯器(如Paint)。所以,我需要創建「撤消」和「重做」按鈕。如果我複製畫布的兒童,Canvas.Children包含很多項目時需要太多時間。如何實現撤消和重做操作?如何在WPF中克隆畫布
美好的一天。我嘗試創建圖形編輯器(如Paint)。所以,我需要創建「撤消」和「重做」按鈕。如果我複製畫布的兒童,Canvas.Children包含很多項目時需要太多時間。如何實現撤消和重做操作?如何在WPF中克隆畫布
我從來沒有使用過Canvas
但克隆它,我想這和它似乎工作:
Canvas canvas = new Canvas();
canvas.Background = Brushes.Thistle;
Canvas clonedCanvas = ElementClone<Canvas>(canvas);
Brush clonedBrush = clonedCanvas.Background;
/// <summary>
/// Clones an element.
/// </summary>
public static T ElementClone<T>(T element)
{
T clone = default(T);
MemoryStream memStream = ElementToStream(element);
clone = ElementFromStream<T>(memStream);
return clone;
}
/// <summary>
/// Saves an element as MemoryStream.
/// </summary>
public static MemoryStream ElementToStream(object element)
{
MemoryStream memStream = new MemoryStream();
XamlWriter.Save(element, memStream);
return memStream;
}
/// <summary>
/// Rebuilds an element from a MemoryStream.
/// </summary>
public static T ElementFromStream<T>(MemoryStream elementAsStream)
{
object reconstructedElement = null;
if (elementAsStream.CanRead)
{
elementAsStream.Seek(0, SeekOrigin.Begin);
reconstructedElement = XamlReader.Load(elementAsStream);
elementAsStream.Close();
}
return (T)reconstructedElement;
}
只有如果沒有一個WriteableBitmap的存在。然後這失敗了。 – AeroClassics