【问题标题】:c# VS 2019 Xamarin: Problem setting images source from code behind for embedded imagesc# VS 2019 Xamarin:从嵌入图像的代码中设置图像源的问题
【发布时间】:2020-06-25 19:07:08
【问题描述】:

我已经成功地在 XAML 中创建了一个图像,在一个网格中,在 AbsoulteLayout 中

<Image HeightRequest="15" WidthRequest="15" 
   Source="{local:ImageResource ScKWander.Images.UI.arrow_pointer_R.png}"                
   TranslationY="330" TranslationX="15"
/>

我正在尝试在后面的代码中创建图像,以便更改代码中的位置。虽然我尝试了几种可能的替代方案,但没有任何效果。下面的代码编译并运行没有错误。 Catch 永远不会被击中,屏幕上唯一的东西是在 XAML 中创建的一个图像。我只打算将网格用作容器,但如果需要,我可以使用网格行/列。在 UWP 中,我使用的是画布。

我尝试了几种不同的源代码编码方法,但都失败了。除了那个,我把所有的都拿出来了。

我应该补充一下,我现在遇到了几个 Xamarin 错误。我认为它是新的建筑。我不认为这些影响了这一点。我构建、运行并看到一个由 XAML 定义的箭头。

private void FillGrid()
{
 int iYoffset   = 15;
 int iXoffset   = 15;
 int iYGridsize = 25;
 int iXGridsize = 25;
 try 
   {
   for (int iCount0 = 0; iCount0 < 5; iCount0++)
     {
     for (int iCount1 = 0; iCount1 < 5; iCount1++)
       {
         var thisImage = new Image { Source = ImageSource.FromResource
            ("local:ImageResource ScKWander.Images.UI.arrow_pointer_R.png") };

       //thisImage.TranslationY  = iYoffset + (iCount0 * iYGridsize);
       //thisImage.TranslationX  = iXoffset + (iCount1 * iXGridsize);

         thisImage.HeightRequest = 25;
         thisImage.WidthRequest  = 25;
         gridBoard.Children.Add(thisImage, iCount0 * iYGridsize, iCount1 * iXGridsize);
       }
     }
   }
   catch (Exception e)
   {
   string ex = e.Message;
   }
}

【问题讨论】:

    标签: c# image xamarin


    【解决方案1】:

    documentation 显示了如何做到这一点

    var embeddedImage = new Image { 
      Source = ImageSource.FromResource(
        "WorkingWithImages.beach.jpg", 
        typeof(EmbeddedImages).GetTypeInfo().Assembly
      ) };
    

    【讨论】:

      【解决方案2】:

      首先,我真正的问题是没有:

      using System.Reflection;
      

      没有IntelliType,但我终于发现需要添加这个。 一旦完成,下面的代码就可以工作了。

      var thisImage = new Image
      {
        Source = ImageSource.FromResource(
          "ScKWander.Images.UI.arrow_pointer_r.png",
          typeof(ImageResourceExtension).GetTypeInfo().Assembly
        ),
        HorizontalOptions = LayoutOptions.Start
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-28
        • 1970-01-01
        • 2017-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多