【问题标题】:Is it possible to add an SVG to a MapIcon.Image for display in a UWP Map Control?是否可以将 SVG 添加到 MapIcon.Image 以在 UWP 地图控件中显示?
【发布时间】:2018-06-26 09:02:22
【问题描述】:

我可以毫无问题地添加 PNG 图像来更改默认的 MapIcon 图像,但是一旦我尝试使用 SVG,应用程序就会崩溃,并出现“application.exe 已退出,代码 -529697949 (0xe06d7363) 'Microsoft C++ Exception'”。

我知道 SvgImageSource 类,我可以在应用程序的其他区域使用它来渲染 SVG 图像,但是当尝试将 SVG 添加到 MapIcon.Image 时;这是 IRandomAccessStreamReference 类型,它不起作用。我只是想知道这是否是 API 的限制,在 MapIcon 上显示自定义图像时我是否应该只使用位图而不是 SVG?

我在下面添加了一个快速失败的示例。

MainPage.xaml:

<Page xmlns:my="using:Windows.UI.Xaml.Controls.Maps" 
    x:Class="TestMapWithSVGForMapIcon.MainPage"
    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"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <my:MapControl Loaded="{x:Bind _viewModel.AddMapIcon}"/>
    </Grid>
</Page>

MainPage.xaml.cs:

public sealed partial class MainPage : Page
{
    private MainViewModel _viewModel;

    public MainPage()
    {
        InitializeComponent();

        _viewModel = new MainViewModel();
        DataContext = _viewModel;
    }
}

MainViewModel.cs:

public class MainViewModel
{
    protected MapControl Map { get; set; }

    public void AddMapIcon(object sender, RoutedEventArgs e)
    {
        if (sender is MapControl map)
        {
            Map = map;
        }

        var mapIcon = new MapIcon
        {
            Location = new Geopoint(new BasicGeoposition()
            {
                Latitude = 53.795452,
                Longitude = -4.304733
            }),
            //works Ok with .png files
            Image = RandomAccessStreamReference.CreateFromUri(new Uri($"ms-appx:///Assets/Hazard_H1.svg")),              
        };

        Map.MapElements.Add(mapIcon);    
    }
}

【问题讨论】:

    标签: c# xaml svg windows-10-universal uwp-maps


    【解决方案1】:

    不,SVG 不是 MapIcon.Image 支持的流类型 您可以使用包含 PNG 或 JPG 格式的编码光栅图像或 RGB 格式的原始字节数组的流。

    【讨论】:

    • 你好 Duncan,你有关于如何将原始字节数组转换为 RandomAccessStreamReference 的代码示例吗?
    • 从未尝试过该方法,但我认为它会是这样的: using (var memStream = new MemoryStream(array)) { var streamReference = RandomAccessStreamReference.CreateFromStream(memStream.AsRandomAccessStream()); }
    • 感谢您的回答。我已经尝试过了,但是没有设置图标,我找不到确切知道原因的方法。这是唯一工作的同步(非异步)代码:使用 (var l_rasMemoryStream = new InMemoryRandomAccessStream()) 使用 (var l_osOutput = l_rasMemoryStream.GetOutputStreamAt(0)) 使用 (var l_dwWriter = new DataWriter(l_osOutput)) { l_dwWriter.WriteBytes( p_arbImage); l_dwWriter.StoreAsync().GetResults(); l_dwWriter.FlushAsync().GetResults(); p_miToSet.Image = RandomAccessStreamReference.CreateFromStream(l_rasMemoryStream); }
    • 这段代码确实简单多了,但也不起作用:p_miToSet.Image = RandomAccessStreamReference.CreateFromStream(p_arbImage.AsBuffer().AsStream().AsRandomAccessStream());
    • 你是对的,这是我的最终代码:var l_rasStream = new InMemoryRandomAccessStream(); await l_rasStream.WriteAsync(p_arbImage.AsBuffer()); await l_rasStream.FlushAsync(); p_miToSet.Image = RandomAccessStreamReference.CreateFromStream(l_rasStream);
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2010-09-16
    • 2021-11-13
    • 2014-10-20
    • 2021-08-07
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多