copging UIElement

essamce 621 Reputation points
2020-05-11T08:50:19.76+00:00

hi,
i'm trying to copy UIElement, and it always gives me the same error.
here method i'm trying:

 public static UIElement cloneElement(UIElement orig)  
        {  
            if (orig == null)  
            {  
                return (null);  
            }  
  
            var s = System.Windows.Markup.XamlWriter.Save(orig);  
            var stringReader = new System.IO.StringReader(s);  
            var xmlReader = System.Xml.XmlTextReader.Create(stringReader, new System.Xml.XmlReaderSettings());  
  
            return (UIElement)System.Windows.Markup.XamlReader.Load(xmlReader);  
  
        }  

   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);  
            }  
            if (clone is T)  
                return (T)clone;  
            else  
                return null;  
        }  

here is the error8092-error.png

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-05-11T10:16:14.627+00:00

    Hi, loading a XAML with resources you add resource and you get conflict with existing resources. You must copy XAML without resources and include resources only once.

    1 person found this answer helpful.