简体   繁体   English

react-native for windows UWP - 如何加载本地光栅图像资源

[英]react-native for windows UWP - how to load local raster image resource

I am using react-native for windows UWP.我将 react-native 用于 windows UWP。 I don't understand how to load local raster image assets I want to package with the application.我不明白如何使用应用程序加载我想要 package 的本地光栅图像资产。 I am currently placing my image files in windows/foobar/Assets/ (where foobar is the name of the application).我目前将图像文件放在 windows/foobar/Assets/ 中(其中 foobar 是应用程序的名称)。 My initial assumption was that I could simply require them in the source like this:我最初的假设是我可以像这样在源代码中简单地要求它们:

<Image style={{ width: 50, height: 50}} uri={require('./windows/foobar/Assets/energy.png')} />

This works for svg files but errors for raster files saying it was unable to define the module.这适用于 svg 文件,但光栅文件的错误表明它无法定义模块。 That makes sense since require is for loading modules and raster files are definitely not that.这是有道理的,因为 require 用于加载模块,而光栅文件绝对不是这样。 VS Code tries to help me understand this by not auto completing any raster files under Assets/. VS Code 试图通过不自动完成 Assets/ 下的任何光栅文件来帮助我理解这一点。

However I've seen some examples online state I can do:但是我在网上看到了一些例子 state 我可以这样做:

source={require('@expo/snack-static/react-native-logo.png')}

I don't understand why this format could work but the local path would fail.我不明白为什么这种格式可以工作,但本地路径会失败。

I've seen other examples stating I should use:我已经看到其他示例说明我应该使用:

uri={require('ms-appx:///ReactAssets/energy.png')}

But this does not resolve.但这并不能解决。

Ultimately I assume I need to tell react-native for windows about the assets I want to include in my app so they can be resolved through some kind of resource bundle, but I'm not finding any documentation on how to accomplish this.最终,我假设我需要告诉 react-native 的 windows 我想包含在我的应用程序中的资产,以便可以通过某种资源包解决它们,但我没有找到任何关于如何完成此操作的文档。 What do I do?我该怎么办?

The discussion on this github issue is a little confusing to follow but eventually got me to the right answer.关于这个 github 问题的讨论有点令人困惑,但最终让我得到了正确的答案。

  1. Put your image under windows\\{yourprojectname}\Image将您的图像放在windows\\{yourprojectname}\Image
  2. Open the windows solution in Visual Studio at windows\\{yourprojectname}.slnwindows\\{yourprojectname}.sln的 Visual Studio 中打开 windows 解决方案
  3. In solution explorer, at the top, click on 'show all files'.在解决方案资源管理器的顶部,单击“显示所有文件”。
  4. Expand the project for your Universal Windows app, find the Images folder, right click your image file and select 'Include in project'.为您的通用 Windows 应用程序展开项目,找到图像文件夹,右键单击您的图像文件和 select '包含在项目中'。 Save all files.保存所有文件。
  5. Restart react-native重启react-native

You can now refer to your images with:您现在可以参考您的图像:
<Image source={{ uri: "ms-appx:///Image/yourimage.png"}}/>

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

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