【问题标题】:Assigning image resource during runtime for xaml .Net 2.0 multi platform mobile development在运行时为 xaml .Net 2.0 多平台移动开发分配图像资源
【发布时间】:2021-04-12 09:24:21
【问题描述】:

目标:

我想显示一个带有图像的 ImageButton,就像这样:

当前的解决方案有效,但我只能选择将单个图像硬编码到 xaml 中。

为此我已经准备了很多东西:

现状

  1. 我已添加图像并将其设置为“构建操作:嵌入式资源”

  2. 我已经添加了 ImageResourceExtension,就像在线建议的那样用于 xaml:

using System;
using System.Reflection;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Bitconomy.resources
{
    [ContentProperty(nameof(Source))]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }

            // Do your translation lookup here, using whatever method you require
            var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);

            return imageSource;
        }
    }
}
  1. 我在 xaml 中添加了对 ImageResourceExtension/resources 的引用:
xmlns:resources="clr-namespace:Bitconomy.resources"
  1. 我已将图像资源添加到 ImageButton:
<ImageButton x:Name="MiningButton" Grid.Row="0" Source="{resources:ImageResource Bitconomy.resources.images.mines.stone_mine.jpg}" ></ImageButton>

结果是目标部分下方顶部显示的结果。到目前为止还好。

问题

我想在旅途中设置地雷。我准备了一个“mining_view.xaml”。在“mining_view.xaml.cs”中,我添加了配置矿井的代码,例如。石头,铁,或任何东西。我希望每个地雷都有不同的图像。

过去,我在堆栈溢出中遇到过类似的问题,但它是供桌面使用的。我很震惊移动开发到底有多么不同。相似但不一样。

如果您能帮我弄清楚如何在代码中设置图像源,我将非常高兴,这样它就可以在安卓和 ios 中运行。

public void ConfigureMine(string ItemType)
{
    this.MiningButton.Source = new ImageSource.FromResource("resources:ImageResource Bitconomy.resources.images.mines.stone_mine.jpg");
    // or sth like that?
    this.MiningButton.Source = resources.ImageResourceExtension.ProvideValue(SomeProvider?)
}

【问题讨论】:

    标签: c# android ios xaml imagesource


    【解决方案1】:
    1. 在 xaml 中创建图像。分配一个 x:Name 属性以从代码中选择图像:
    <Image x:Name="Mine_Image" Grid.Column="0" Grid.Row="0" Aspect="AspectFill"></Image>
    
    1. 您将需要一个资源扩展:
    using System;
    using System.Reflection;
    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace Bitconomy.resources
    {
        [ContentProperty(nameof(Source))]
        public class ImageResourceExtension : IMarkupExtension
        {
            public string Source { get; set; }
    
            public object ProvideValue(IServiceProvider serviceProvider)
            {
                if (Source == null)
                {
                    return null;
                }
    
                // Do your translation lookup here, using whatever method you require
                var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
    
                return imageSource;
            }
            public ImageSource GetImageSource(string resourcepath)
            {
                return ImageSource.FromResource(resourcepath, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
            }
        }
    }
    
    1. 添加资源图片

    2. 设置图像以构建动作“嵌入式资源”

    3. 在 form.xaml.cs 中更改图片(根据需要调整资源路径):

    this.Mine_Image.Source = Resources.GetImageSource("Bitconomy.resources.images.resources.Stone_mine.jpg"); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多