0

我对 Visual Studio/Xamarin 还是很陌生。我正在尝试将图像添加到我的 Android 示例中。

我在 macOS Mojava 10.14.6 和以下软件包上使用 Visual Studio for Mac community 8.4.5(内部版本 19):

  • BruTile 2.1.2
  • Mapsui 1.4.8
  • OsmSharp 6.2.0
  • Xamarin.Android.Support.Core.Utils 28.0.0.3
  • Xamarin.Android.Support.CustomTabs 28.0.0.3
  • Xamarin.Android.Support.Design 28.0.0.3
  • Xamarin.Essentials 1.3.1

在我的解决方案中,我将图像添加到以下位置:

  • 资产 > 图片 > sample.png
  • 资源 > 绘图 > sample.png

然后我尝试使用以下代码找到它:

    String target = "sample.png"
    Assembly resourceAssembly = null;
    String resourceName = null;
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
    foreach (Assembly assembly in assemblies)
    {
        Console.WriteLine("Assembly: '" + assembly.GetName());
        try
        {
            String[] fileNames = assembly.GetManifestResourceNames();
            foreach (var fileName in fileNames)
            {
                Console.WriteLine("File Name: '" + fileName + "'");
                if (fileName.ToUpper().EndsWith(target.ToUpper()))
                {
                    resourceAssembly = assembly;
                    resourceName = fileName;
                }
            }
        }
        catch (NotSupportedException e)
        {
            Console.WriteLine(
                "Not Supported for Assembly: '" + assembly.GetName() + "'  (" + e.GetType() + ")"
            );
        }
    }
    if (resourceName != null) {
        Console.WriteLine("Resource file name [{0}] found as [{1}]", fileName, resourceName);
    }

控制台日志显示了相当多的程序集和文件名,但从未打印出最后一行,因此找不到 sample.png 文件。

为什么这段代码找不到文件?我是否将文件添加到解决方案中的错误位置?还是我需要额外做一些事情?

(我目前的偏好是继续使用组装方法来查找文件。)

4

1 回答 1

0

我不是 100% 确定你想做什么,但如果你需要图像来显示你可以使用 xaml。

<Image Source="sample.jpg" />

只要文件名都是小写的,没有特殊字符/数字 & 在 - Resources > drawables 它应该显示。

希望这可以帮助。

于 2020-02-14T10:40:44.077 回答