简体   繁体   English

如何在C ++ / Winrt UWP应用程序中使用SVG图像?

[英]How to use SVG images in c++/winrt UWP application?

I have been able to programmatically display bitmap images using C++/WinRT in my UWP applications, however I can't get SVG images to work. 我已经可以在UWP应用程序中使用C ++ / WinRT以编程方式显示位图图像,但是我无法使SVG图像正常工作。

Below is the most simple example I could come up with. 下面是我能想到的最简单的例子。 I create an application, I load an SVG image, I add it to the application window and activate the window. 创建一个应用程序,加载一个SVG图像,将其添加到应用程序窗口中并激活该窗口。 I have provided the source code and the SVG file below. 我在下面提供了源代码和SVG文件。

A few notes: 一些注意事项:

  • The application runs, it's just that no image is displayed 该应用程序运行,只是没有图像显示
  • I made sure the SVG file is compliant with supported features in Windows 我确保SVG文件与Windows中支持的功能兼容
  • The file is in my project root folder 该文件在我的项目根文件夹中
  • Loading bitmaps in a similar way works without problems 以类似方式加载位图可以正常工作
  • I have tried to troubleshoot using the OpenFailed method, but the info it gives is useless 我试图使用OpenFailed方法进行故障排除,但是它提供的信息没有用

source 资源

#include "pch.h"


using namespace winrt;
using namespace winrt::Windows::ApplicationModel::Activation;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Xaml::Media::Imaging;


struct App : ApplicationT<App>
{
    Image mImage;

    void OnLaunched(LaunchActivatedEventArgs const &)
    {
        //Load PNG image - WORKS
        //mImage.Source(BitmapImage(Uri(L"ms-appx:///sample.png")));

        //Load SVG image - FAILS
        mImage.Source(SvgImageSource(Uri(L"ms-appx:///sample.svg")));

        //Show image on screen
        Window window = Window::Current();
        window.Content(mImage);
        window.Activate();
    }

    static void Init(ApplicationInitializationCallbackParams const &)
    {
        make<App>();
    }
};

int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
    Application::Start(App::Init);
}

sample.svg sample.svg

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="50"/>
</svg>

The problem is not in your code, but in the fact that the file is not copied with the app content. 问题不在于您的代码中,而在于文件没有与应用程序内容一起复制。

Click on the sample.svg file in the Solution Explorer and look into the Properties toolbar. 在解决方案资源管理器中单击sample.svg文件,然后查看“ 属性”工具栏。 You will see Content set as False . 您将看到Content设置为False You need to set it to True so that the file is copied into the output folder along with other content files like images (those are Content by default, so that is why you don't have to do this). 您需要将其设置为True以便将文件与其他内容文件(如图像)一起复制到输出文件夹中(默认情况下为Content ,因此不必这样做)。

内容设定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM