Exemplo n.º 1
0
        /// <summary>
        /// Imports a drawing from xml.
        /// </summary>
        /// <param name="xElement">Xml element too start searching</param>
        /// <param name="clearDrawing">remove all shapes before import</param>
        /// <returns>true if all xml element could be loaded, false if unknown (skiped) elements have been found</returns>
        /// <remarks>
        /// Searches for the 1st DrawingImage, if none is found look for Canvas
        /// </remarks>
        public bool Import(XElement xElement, bool clearDrawing)
        {
            if (clearDrawing)
            {
                ClearDrawing();
            }

            // look for DrawingImage
            var xRoot = (from e in xElement.DescendantsAndSelf("DrawingImage")
                         select e).FirstOrDefault();

            if (xRoot != null)
            {
                // we need the 1st DrawingGroup
                xRoot = (from e in xElement.Descendants("DrawingGroup")
                         select e).FirstOrDefault();

                if (xRoot != null)
                {
                    foreach (var xGD in xRoot.Elements("GeometryDrawing"))
                    {
                        XDrawingShape shape = XDrawingShape.LoadFromGeometryDrawing(this, xGD);
                        if (shape != null)
                        {
                            m_Shapes.Add(shape);
                            Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), shape.Path);
                        }
                    }
                }
            }
            else
            {
                // DrawingImage not found, look for Canvas
                xRoot = (from e in xElement.DescendantsAndSelf("Canvas")
                         select e).FirstOrDefault();

                if (xRoot != null)
                {
                    foreach (var xPath in xRoot.Elements("Path"))
                    {
                        XDrawingShape shape = XDrawingShape.LoadFromPath(this, xPath);
                        if (shape != null)
                        {
                            m_Shapes.Add(shape);
                            Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), shape.Path);
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (Clipboard.ContainsText() && Clipboard.GetText().StartsWith("<GeometryDrawing"))
            {
                try
                {
                    var xaml = Clipboard.GetText();
                    var xDoc = XDocument.Parse(xaml);

                    XDrawingShape shape = XDrawingShape.LoadFromGeometryDrawing(this, xDoc.Root);
                    if (shape != null)
                    {
                        m_Shapes.Add(shape);
                        Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), shape.Path);
                        SelectedShape = shape;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception while pasting");
                }
            }
        }