【问题标题】:Getting an embedded resource from a shared project从共享项目中获取嵌入式资源
【发布时间】:2018-11-15 19:37:15
【问题描述】:

我正在开发一个 Xamarin Forms 应用程序,它有两个项目,一个 Android 应用程序和一个 iOS 应用程序。所有其他代码都存储在共享项目中。

解决方案如下:

  • MyApp.Android(Android 项目),引用 MyApp.Base
  • MyApp.iOS(iOS 项目),引用 MyApp.Base
  • MyApp.Base(共享项目)

我正在使用以下代码从共享项目中读取 SVG 图像:

using (var stream = GetType().Assembly.GetManifestResourceStream("MyApp.Base.image.svg"))
{
    // do work here...
}

当图像在 Android 或 iOS 项目中时,这非常有效,但我希望共享图像,所以我将它放在共享项目中。

GetType().Assembly 返回 MyApp.Android,因此找不到图像。我想我忽略了一些东西,但我无法找到解决方案。

谁能指出我正确的方向?谢谢!

【问题讨论】:

标签: c# svg xamarin.forms


【解决方案1】:

您使用的是共享项目而不是 PCL 或 .Net 标准库。因此,当您编译它们时,您的所有共享项目内容都将合并到特定于平台的项目中。这意味着 - 即使您将资源嵌入到将合并到 .iOS/.Droid 项目中的 MyApp.Base 中。 我建议您了解有关共享项目与 PCL 或 .Net 标准库的更多信息。 下面的代码;我没有测试,但这是你应该遵循的方向:

#if __IOS__
    var resourcePrefix = "MyApp.iOS.";
#endif
#if __ANDROID__
    var resourcePrefix = "MyApp.Android.";
#endif

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(SharedPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream
(resourcePrefix + "image.svg");

欲了解更多信息,请查看此页面:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=macos#embedding-in-shared-projects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多