扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

时间:2022-05-19 10:11:12

原文 http://blog.csdn.net/esricd/article/details/7587136

在ArcGIS API for Silverlight/WPF中原版的TextSymbol只能支持文字正向显示。在很多实际项目中,往往需要文字标注有一些角度甚至是沿线标注,下面 我们来看一下原装的TextSymbol和扩展后的TextSymbol的比较和实现思路。

扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

要实现右图的效果只需要从TextSymbol继承一个Symbol并增加Rotation属性,并加载相应的控件模板就行了。

以下是控件模板的代码:

  1. <ControlTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
  4. xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
  5. <TextBlock Text="{Binding Symbol.Text}"
  6. FontFamily="{Binding Symbol.FontFamily}"
  7. FontSize="{Binding Symbol.FontSize}"
  8. Foreground="{Binding Symbol.Foreground}">
  9. <TextBlock.RenderTransform>
  10. <CompositeTransform Rotation="{Binding Symbol.TextRotation}"/>
  11. </TextBlock.RenderTransform>
  12. </TextBlock>
  13. </ControlTemplate>

控件模板中需要绑定对象中的文本、字体、字号、颜色、角度五个属性。

对象类的加载XAML代码如下:

  1. base.ControlTemplate = XamlReader.Load(LoadXaml("LabelSymbol.xaml")) as ControlTemplate;
  2. public static string LoadXaml(string FileName)
  3. {
  4. string xamlstring;
  5. var assemblyName = new AssemblyName(Assembly.GetExecutingAssembly().FullName);
  6. string CurrentAssemblyName = assemblyName.Name;
  7. string resourceName = string.Format("{0};component{1}{2}", CurrentAssemblyName, "/", FileName);
  8. Uri uri = new Uri(resourceName, UriKind.Relative);
  9. StreamResourceInfo streamResourceInfo = Application.GetResourceStream(uri);
  10. using (Stream resourceStream = streamResourceInfo.Stream)
  11. {
  12. using (StreamReader streamReader = new StreamReader(resourceStream))
  13. {
  14. xamlstring = streamReader.ReadToEnd();
  15. }
  16. }
  17. return xamlstring;
  18. }

对象类中再定义对应的五个属性就能实现有倾斜角度的标注了。最终实现效果如图:

扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

后话:

这个扩展的Symbol仅仅是对文字符号增加旋转角度,其中还有不完善的地方,在线路转角的地方标注的时候往往会与线交叉,如:

扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

如果再深入完善一下,稍做修改可以将标注做成真正的沿线标注,如:

扩展ArcGIS API for Silverlight/WPF 中的TextSymbol支持角度标注

沿线文本在网上有大量的资料,在这里就不再啰嗦了,希望本文对各位ArcGIS API for Silverlight开发人员有帮助。