//------------------------------------------------------------------------------- // // Private Methods // //------------------------------------------------------------------------------- #region Private Methods /// <summary> /// Copy the current Selection in XAML format. /// Called by : /// CopySelectedData /// </summary> /// <param name="dataObject"></param> /// <param name="strokes"></param> /// <param name="elements"></param> /// <param name="transform"></param> /// <param name="size"></param> /// <returns>True if the copy is succeeded</returns> private bool CopySelectionInXAML(IDataObject dataObject, StrokeCollection strokes, List <UIElement> elements, Matrix transform, Size size) { InkCanvas inkCanvas = new InkCanvas(); // We already transform the Strokes in CopySelectedData. if (strokes.Count != 0) { inkCanvas.Strokes = strokes; } int elementCount = elements.Count; if (elementCount != 0) { InkCanvasSelection inkCanvasSelection = InkCanvas.InkCanvasSelection; for (int i = 0; i < elementCount; i++) { // NOTICE-2005/05/05-WAYNEZEN, // An element can't be added to two visual trees. // So, we cannot add the elements to the new container since they have been added to the current InkCanvas. // Here we have to do is according to the suggestion from Avalon team - // 1. Presist the elements to Xaml // 2. Load the xaml to create the new instances of the elements. // 3. Add the new instances to the new container. string xml = XamlWriter.Save(elements[i]); UIElement newElement = XamlReader.Load(new XmlTextReader(new StringReader(xml))) as UIElement; ((IAddChild)inkCanvas).AddChild(newElement); // Now we tranform the element. inkCanvasSelection.UpdateElementBounds(elements[i], newElement, transform); } } if (inkCanvas != null) { inkCanvas.Width = size.Width; inkCanvas.Height = size.Height; ClipboardData data = new XamlClipboardData(new UIElement[] { inkCanvas }); try { data.CopyToDataObject(dataObject); } catch (SecurityException) { // If we hit a SecurityException under the PartialTrust, we should just fail the copy // operation. inkCanvas = null; } } return(inkCanvas != null); }
private bool CopySelectionInXAML(IDataObject dataObject, StrokeCollection strokes, List <UIElement> elements, Matrix transform, Size size) { if (!SecurityHelper.CheckUnmanagedCodePermission()) { return(false); } InkCanvas inkCanvas = new InkCanvas(); if (strokes.Count != 0) { inkCanvas.Strokes = strokes; } int count = elements.Count; if (count != 0) { InkCanvasSelection inkCanvasSelection = this.InkCanvas.InkCanvasSelection; for (int i = 0; i < count; i++) { try { string s = XamlWriter.Save(elements[i]); UIElement uielement = XamlReader.Load(new XmlTextReader(new StringReader(s))) as UIElement; ((IAddChild)inkCanvas).AddChild(uielement); inkCanvasSelection.UpdateElementBounds(elements[i], uielement, transform); } catch (SecurityException) { inkCanvas = null; break; } } } if (inkCanvas != null) { inkCanvas.Width = size.Width; inkCanvas.Height = size.Height; ClipboardData clipboardData = new XamlClipboardData(new UIElement[] { inkCanvas }); try { clipboardData.CopyToDataObject(dataObject); } catch (SecurityException) { inkCanvas = null; } } return(inkCanvas != null); }
private bool CopySelectionInXAML(IDataObject dataObject, StrokeCollection strokes, List<UIElement> elements, Matrix transform, Size size) { //NOTE: after meeting with the partial trust team, we have //collectively decided to only allow copy / cut of XAML if the caller //has unmanagedcode permission, else we silently ignore the XAML if (!SecurityHelper.CheckUnmanagedCodePermission()) { return false; } else { InkCanvas inkCanvas = new InkCanvas(); // NOTICE-2005/12/06-WAYNEZEN, // We already transform the Strokes in CopySelectedData. if (strokes.Count != 0) { inkCanvas.Strokes = strokes; } int elementCount = elements.Count; if (elementCount != 0) { InkCanvasSelection inkCanvasSelection = InkCanvas.InkCanvasSelection; for (int i = 0; i < elementCount; i++) { // NOTICE-2005/05/05-WAYNEZEN, // An element can't be added to two visual trees. // So, we cannot add the elements to the new container since they have been added to the current InkCanvas. // Here we have to do is according to the suggestion from Avalon team - // 1. Presist the elements to Xaml // 2. Load the xaml to create the new instances of the elements. // 3. Add the new instances to the new container. string xml; try { xml = XamlWriter.Save(elements[i]); UIElement newElement = XamlReader.Load(new XmlTextReader(new StringReader(xml))) as UIElement; ((IAddChild)inkCanvas).AddChild(newElement); // Now we tranform the element. inkCanvasSelection.UpdateElementBounds(elements[i], newElement, transform); } catch (SecurityException) { // If we hit a SecurityException under the PartialTrust, we should just stop generating // the containing InkCanvas. inkCanvas = null; break; } } } if (inkCanvas != null) { inkCanvas.Width = size.Width; inkCanvas.Height = size.Height; ClipboardData data = new XamlClipboardData(new UIElement[] { inkCanvas }); try { data.CopyToDataObject(dataObject); } catch (SecurityException) { // If we hit a SecurityException under the PartialTrust, we should just fail the copy // operation. inkCanvas = null; } } return inkCanvas != null; } }