上一篇博客写的主要是自定义标注,但是在实际项目开发过程中,所用到的标注类型各式各样。直接在Xaml页面的Resources中定义标注,既不易于编写,也不易于管理。所以最好是创建ControlTemplate的Xaml模板文件来管理自定义的内容。
例如|:
<ControlTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> <Canvas> <Canvas.Resources> <Style TargetType="Grid" x:Key="kvTipStyle"> <Setter Property="Background" Value="Transparent"/> <Setter Property="Height" Value="32"/> <Setter Property="Width" Value="32"/> <Setter Property="Canvas.Top" Value="-16"/> <Setter Property="Canvas.Left" Value="-16"/> </Style> <Style TargetType="Ellipse" x:Key="kvEllipseStyle"> <Setter Property="Height" Value="7"/> <Setter Property="Width" Value="7"/> <Setter Property="Fill" Value="#P0000P"/> <Setter Property="Stroke" Value="Black"/> <Setter Property="StrokeThickness" Value="0.5"/> </Style> </Canvas.Resources> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal"/> <VisualState x:Name="MouseOver"> <Storyboard> <DoubleAnimation BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" From="1" To="1.5" Duration="00:00:0.5" /> <DoubleAnimation BeginTime="0:0:0" Storyboard.TargetName="image" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" From="1" To="1.5" Duration="00:00:0.5" /> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid Style="{StaticResource kvTipStyle}"> <Image x:Name="image" RenderTransformOrigin="0.5,0.5" Width="25" Height="25" Source="{Binding Attributes[PicPath]}"> <Image.RenderTransform> <ScaleTransform /> </Image.RenderTransform> </Image> </Grid> </Canvas> </ControlTemplate >
解决方案结构:
下面是加载模板页的方法:
/// <summary> /// 获取标注 /// </summary> /// <param name="_color">设置颜色</param> /// <returns></returns> public static Symbol GetSymbol(SymbolMode mode,string _markColor,string _fontColor) { MarkerSymbol sy = new MarkerSymbol(); try { string contentStr = new StreamReader( Application.GetResourceStream( new Uri(string.Format("/Demo;component/Template/{0}Tp.xaml", mode.ToString()), UriKind.RelativeOrAbsolute) ).Stream).ReadToEnd(); contentStr = contentStr.Replace("#P0000P", _markColor); contentStr = contentStr.Replace("#F0000F", _fontColor); sy.ControlTemplate = (ControlTemplate)XamlReader.Load(contentStr); } catch { } return sy; }