[英]how to add jpg image to IOs project for Visual studio with xamarin
[英]Unable to add image to xcassets in Xamarin.iOS project with Visual Studio 2022
我最近更新到 Visual Studio for Mac 2022,从那时起我一直遇到以下问题:我无法将新图像添加到 Media.xcassets 文件夹
在 VS for Mac 2019 上,我能够在 Visual Studio 中实现这一点,现在当我在左侧面板浏览器中双击 xcassets 文件夹时,该文件将在 Xcode 中打开。 我可以在 Xcode 中添加一个新图像集,但是当我从 Visual Studio 清理、构建和部署应用程序时,更改不会反映出来。 我在 xib 文件上使用或以编程方式加载的新图像的任何实例,例如 UIImage.FromBundle("new_image") 都不会显示在应用程序中。 之前使用 VS for Mac 2019 添加到项目中的图像正确显示。
这似乎是 VS for Mac 2022 的一个错误,所以我真的在问是否有人知道解决方法?
这似乎是由 VS 2022 中的错误引起的。我通过以下步骤解决了这个问题:
确保您的 Media.xcassets 文件夹位于 Xamarin iOS 项目的根目录中,与 .csproj 文件位于同一位置,例如。 MyApp/MyApp.iOS/Media.xcassets - 在我的情况下,它在 MyApp/MyApp.iOS/Resources/Media.xcassets 所以我不得不移动它
在 Visual Studio 中双击 Media.xcassets 文件以在 Xcode 中打开它
在 Xcode 中添加新图像并保存
返回 Visual Studio 时,您应该在 VS 窗口的左下方看到“错误同步 Xcode 的更改”(VS 错误)
更新您的 iOS 项目文件 (MyApp/MyApp.iOS/MyApp.iOS.csproj) 以包含对新图像的 Contents.json 和 pdf/png 文件的引用。 例如,我必须添加这两行:
<ImageAsset Include="Media.xcassets\new_image.imageset\Contents.json" /> <ImageAsset Include="Media.xcassets\new_image.imageset\new_image.pdf" />
下次构建和部署时,新图像应该出现在应用程序中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.