This example will show how to implement a custom object from a XAML template in LEADTOOLS Annotations. We will modify the appearance of the AnnNoteObject from a solid rectangle to a full XAML control.
Add a new class file (MyNoteObjectRenderer.cs) and add the following code:
using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Controls;
using Leadtools;
using Leadtools.Annotations.Core;
using Leadtools.Annotations.Rendering;
namespace MyNamespace
{
// Custom Note renderer
public class MyNoteObjectRenderer : AnnObjectRenderer
{
// The current object to render
private AnnNoteObject _annObject;
// Current object transform
private MatrixTransform _matrixTransform;
// Current mapper
private AnnContainerMapper _mapper;
// Current object bounds;
private LeadRectD _bounds;
// Override AnnObjectRenderer.VisualComponents, this gets called
// from Render to construct the object visuals
public override List<FrameworkElement> VisualComponents
{
get
{
// Get the rendering engine
AnnWinRTRenderingEngine engine = RenderingEngine as AnnWinRTRenderingEngine;
// Get the children canvas
Dictionary<AnnObject, AnnObjectVisual> childrenCanvas = engine.ChildrenCanvas;
// Get the visual object for this AnnObject
AnnObjectVisual annObjectVisual = childrenCanvas[_annObject];
FrameworkElement element;
// See if we are already added
if (!annObjectVisual.ContainsVisualComponents())
{
// No, add us, this is the XAML template
// This defines a Grid with three text blocks for the header/footer and content.
string xaml = "<Grid xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
" Background=\"Red\" HorizontalAlignment=\"Stretch\" VerticalAlignment=\"Stretch\">" +
"<Grid.RowDefinitions>" +
"<RowDefinition Height=\"0.25*\"/>" +
"<RowDefinition Height=\"*\"/>" +
"<RowDefinition Height=\"0.25*\"/>" +
"</Grid.RowDefinitions>" +
"<Grid Grid.Row=\"0\" Background=\"Yellow\">" +
"<TextBlock x:Name=\"Header\" Foreground=\"Black\" Text=\"Header\" TextAlignment=\"Center\" VerticalAlignment=\"Center\"/>" +
"</Grid>" +
"<TextBlock Grid.Row=\"1\" x:Name=\"Content\" Text=\"Content\" TextAlignment=\"Center\"/>" +
"<Grid Grid.Row=\"2\" Background=\"Blue\">" +
"<TextBlock x:Name=\"Footer\" Foreground=\"Yellow\" Text=\"Footer\" TextAlignment=\"Center\" VerticalAlignment=\"Center\"/>" +
"</Grid>" +
"</Grid>";
element = Windows.UI.Xaml.Markup.XamlReader.Load(xaml) as FrameworkElement;
annObjectVisual.VisualComponents.Add(element);
}
// Get the element
element = annObjectVisual.VisualComponents[0] as FrameworkElement;
// Set the text, find the Content element
TextBlock textBlock = element.FindName("Content") as TextBlock;
textBlock.Text = _annObject.Text;
// Set the size
element.Width = _bounds.Width;
element.Height = _bounds.Height;
// Set the location
if (!double.IsInfinity(_bounds.X) && !double.IsInfinity(_bounds.Y) &&
!double.IsNaN(_bounds.X) && !double.IsNaN(_bounds.Y))
{
Canvas.SetLeft(element, _bounds.X);
Canvas.SetTop(element, _bounds.Y);
}
// Set the transform
element.RenderTransform = _matrixTransform;
return annObjectVisual.VisualComponents;
}
}
public override void Render(AnnContainerMapper mapper, AnnObject annObject)
{
if (mapper == null) throw new ArgumentNullException("mapper");
if (annObject == null) throw new ArgumentNullException("annObject");
// Save the object and mapper
_annObject = annObject as AnnNoteObject;
_mapper = mapper;
if (_annObject.Bounds.Width == 0 && _annObject.Bounds.Height == 0)
return;
// Get the render points
LeadPointD[] renderPoints = GetRenderPoints(mapper, _annObject);
// Get the bounding rectangle in physical coordinates and save it
_bounds = mapper.RectFromContainerCoordinates(_annObject.Rect, _annObject.FixedStateOperations);
// Create the rotation transform
// Get the center point
LeadPointD center = LeadPointDHelper.Create(_bounds.X + _bounds.Width / 2, _bounds.Y + _bounds.Height / 2);
LeadMatrix objectRotation = LeadMatrix.Identity;
objectRotation.RotateAt(_annObject.Angle, center.X - _bounds.X, center.Y - _bounds.Y);
_matrixTransform = new MatrixTransform();
_matrixTransform.Matrix = new Matrix(objectRotation.M11, objectRotation.M12, objectRotation.M21, objectRotation.M22, objectRotation.OffsetX, objectRotation.OffsetY);
base.Render(mapper, annObject);
// Reset the parameters
_annObject = null;
_matrixTransform = null;
_mapper = null;
_bounds = LeadRectDHelper.Empty;
}
}
}
Modify AutomationAttach method in MainPage as this:
public void AutomationAttach(AnnContainer container)
{
_container = container;
_engine = new AnnWinRTRenderingEngine(_container, _viewer);
// Replace the default note renderer with our own
IAnnObjectRenderer originalRenderer = renderingEngine.Renderers[AnnObject.NoteObjectId];
MyNoteObjectRenderer myNoteObjectRenderer = new MyNoteObjectRenderer();
myNoteObjectRenderer.LocationsThumbStyle = originalRenderer.LocationsThumbStyle;
myNoteObjectRenderer.RotateCenterThumbStyle = originalRenderer.RotateCenterThumbStyle;
myNoteObjectRenderer.RotateGripperThumbStyle = originalRenderer.RotateGripperThumbStyle;
_engine.Renderers[AnnObject.NoteObjectId] = myNoteObjectRenderer;
if (_engine != null)
{
_engine.Render(LeadRectDHelper.Create(0, 0, _viewer.Width, _viewer.Height), true);
}
}
Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET