繁体   English   中英

c# VS 2019 Xamarin:从嵌入图像的代码背后设置图像源时出现问题

[英]c# VS 2019 Xamarin: Problem setting images source from code behind for embedded images

我已经在 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;
   }
}

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

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
};

文档确切地显示了如何做到这一点

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM