c# - 如何旋转放置在 map 控件上的图像

标签 c# uwp win-universal-app uwp-xaml uwp-maps

问题

我有一个在 map 上跟踪车辆的应用程序。但是我无法让小 bug 按照它们的运动方向旋转。基本上都是横着走!啊!

Little icons of cars on a map

代码

Image vehicleImage = new Image
{
  //Set image size and source
};
RenderTransform rotation= new RotateTransform{Angle = X};
vehicleImage.RenderTransfrom = rotation; 
_mainMap.Children.Add(vehicleImage);
MapControl.SetLocation(vehicleImage, _position);

放置在 map 上的图像似乎完全忽略了我尝试应用的任何角度。

最佳答案

要理解旋转不生效的原因,我们先来看看下图,它是从 Visual Studio 中的 Live Visual Tree 中截取的 -

enter image description here

我使用的是 Rectangle,但在您的情况下,您会在那里看到 Image 控件。当您将它插入 MapControl.Children 集合时,它将被一个名为 MapOverlayPresenter 的特殊元素包裹(如图所示)。

这个 MapOverlayPresenterMapControl 中的一个内部元素,令人惊讶的是,Internet 上没有关于它究竟做什么的官方文档。我的猜测是,当您缩放或旋转 map 时,此叠加层只是通过沿相反方向缩放或旋转来响应,以保持子元素的原始大小和旋转,这会导致内部 Image 以某种方式迷路。

(P.S. RotationAngleRotationAngleInDegrees 来自 Composition 在这里也没有效果。)


解决方案

解决这个问题的方法很简单——不是直接在 Image 上公开旋转变换,而是创建一个名为 ImageControlUserControl,它封装此 Image 及其转换,具有诸如 UriPathAngle 之类的依赖属性,它们负责将信息向下传递到内部 Image 及其 CompositeTransform 属性。

ImageControl XAML

<UserControl x:Class="App1.ImageControl" ...>
    <Image RenderTransformOrigin="0.5,0.5"
           Source="{x:Bind ConvertToBitmapImage(UriPath), Mode=OneWay}"
           Stretch="UniformToFill">
        <Image.RenderTransform>
            <CompositeTransform Rotation="{x:Bind Angle, Mode=OneWay}" />
        </Image.RenderTransform>
    </Image>
</UserControl>

ImageControl 代码隐藏

public string UriPath
{
    get => (string)GetValue(UriPathProperty);
    set => SetValue(UriPathProperty, value);
}
public static readonly DependencyProperty UriPathProperty = DependencyProperty.Register(
    "UriPath", typeof(string), typeof(ImageControl), new PropertyMetadata(default(string)));     

public double Angle
{
    get => (double)GetValue(AngleProperty);
    set => SetValue(AngleProperty, value);
}
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register(
    "Angle", typeof(double), typeof(ImageControl), new PropertyMetadata(default(double)));

public BitmapImage ConvertToBitmapImage(string path) => new BitmapImage(new Uri(BaseUri, path));

如何使用这个ImageControl

var vehicleImage = new ImageControl
{
    Width = 80,
    UriPath = "/Assets/car.png",
    Angle = 45
};

_mainMap.Children.Add(vehicleImage);

结果

enter image description here

希望这对您有所帮助!

关于c# - 如何旋转放置在 map 控件上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45248112/

相关文章:

c# - 如何在Windows 10 Universal App中制作磨砂玻璃效果?

c# - UWP - 项目中包含的文件出现 UnauthorizedAccessException

c# - 更改 CommandBar (AppBar) 颜色

c# - 如何将控件添加到集合的开头?

c# - 如何为 Lua(对于 Windows)创建可加载的自定义 .NET dll?

c# - UWP 设置 NavigationView TogglePaneButton 颜色

c# - SQLite 查询后 Reader 丢失值

admob - Google Admob 和 WINdows 10 通用应用

c# - ASP.NET 无法引用类

c# - session 变量不会在不同 Controller 之间保持值