ScreenToGif/Util/UiElementsExtension.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace ScreenToGif.Util
{
    public static class UiElementExtensions
    {
        public static T XamlClone<T>(this T original) where T : class
        {
            if (original == null)
                return null;

            object clone;
            using (var stream = new MemoryStream())
            {
                XamlWriter.Save(original, stream);
                stream.Seek(0, SeekOrigin.Begin);
                clone = XamlReader.Load(stream);
            }

            return clone as T;
        }

        //public static T Clone<T>(this T source)
        //{
        //    var sb = new StringBuilder();
        //    var writer = XmlWriter.Create(sb, new XmlWriterSettings
        //    {
        //        Indent = true,
        //        ConformanceLevel = ConformanceLevel.Fragment,
        //        OmitXmlDeclaration = true,
        //        NamespaceHandling = NamespaceHandling.OmitDuplicates,
        //    });

        //    var mgr = new XamlDesignerSerializationManager(writer);
        //    mgr.XamlWriterMode = XamlWriterMode.Expression;

        //    XamlWriter.Save(source, mgr);

        //    return null; // sb.ToString();
        //}

        public static T DeepClone<T>(this T source) where T : UIElement
        {
            var type = source.GetType();

            var result = Activator.CreateInstance(type) as T;

            CopyProperties(source, result, type);
            DeepCopyChildren(source, result);

            return result;
        }

        private static void DeepCopyChildren<T>(T source, T result) where T : UIElement
        {
后面还有 50 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。