【问题标题】:Xamarin forms background image not workingXamarin 形成背景图像不起作用
【发布时间】:2017-09-27 07:00:36
【问题描述】:

我正在开发 Xamarin 表单、跨平台应用程序(Android 和 iOS)。我想要登录表单的背景图片。

我将它设置在表单的构造函数上,代码是:

this.BackgroundImage = ImageSource.FromResource("PCLProjectName.Images.Login.loginbackground.png").ToString();

并且图像在 PCL 项目中,我已将其操作属性设置为 Embedded resource

PCL项目文件夹层次结构如下

Root
 - Images
    - Login
       -loginbackground.png

图片不显示

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.forms xamarin.android


    【解决方案1】:

    如果要在 XAML 文件中为 Xamarin 项目中的整个页面添加背景图像,请使用 BackgroundImage 属性并将图像添加到资源 -> 可绘制文件夹和 iOS 资源文件夹下的 Android 项目。

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                         xmlns:local="clr-namespace:PhoneDailerDemo"
                         x:Class="PhoneDailerDemo.MainPage"
                         BackgroundImage="image3.jpg">
    
                <Label Text="Welcome to Xamarin Forms!" 
                       VerticalOptions="Center" 
                       HorizontalOptions="Center" />
                <StackLayout Padding="100">
                   //..........
                </StackLayout>
      </ContentPage>
    

    【讨论】:

      【解决方案2】:

      要使图像正常工作,它们必须放在iOSandroid 的不同文件夹中。据我所知,无法使用子文件夹或自定义文件结构。

      对于 Android 设备,图像必须位于 Android&gt;Resources&gt;Drawable 下的 Android 项目中。构建操作设置为Android Resource(该文件夹应该已经存在)

      对于 iOS 设备,图像必须在 iOS 项目文件夹 iOS&gt;Resources 中。 Build Action 设置为Bundle Resource(该文件夹也应该已经存在)

      使用各个文件夹中的图像,您可以像这样设置图像.. this.BackgroundImage = ImageSource.FromFile("loginbackground.png")

      编辑:继续您的 PCL 嵌入式资源方法..

      您可以在某处运行这段代码,以查看是否在资源中正确检测到图像。我从经验中知道存在一些奇怪的文件命名问题(尽管我在您的示例中没有看到任何我遇到的问题)

          var assembly = typeof(EmbeddedImages).GetTypeInfo().Assembly;
          foreach (var res in assembly.GetManifestResourceNames()) 
          System.Diagnostics.Debug.WriteLine("found resource: " + res);
      

      【讨论】:

      • 我知道这种方法,我有很多静态图像,所以我想把它放在各自的文件夹中,以便于管理。请检查此链接:developer.xamarin.com/guides/xamarin-forms/user-interface/… 嵌入图像是可能的
      • 当您将图像放置在 PCL 的根文件夹中时,您是否能够加载图像?为什么.ToString() 会附加到您的图像行?
      • @Mahajan344 用可能有用的代码更新了答案。我在这里看到的另一件事 forums.xamarin.com/discussion/comment/95139/#Comment_95139 - 您可以使用 FromResource 加载它并提供文件名,没有文件夹目录 - 尽管我自己没有尝试过。
      • 我的 2 美分:在某些方面使用嵌入式资源可能是“方便的”,但就移动资源(消耗的内存/cpu)和操作系统提供基于自动选择的分辨率的能力而言在设备上,嵌入式资源是一种糟糕的方式......
      【解决方案3】:

      Xamarin 不允许您使用来自外部存储或 Url 的图像作为 ContentPage 的背景图像。 因此,不要在页面( ContentPage )的根标记内设置 BackgroundImage ="image_name_in_resources" ,而是将其放在代码中与您相邻的 Image 标记中,都在 AbsoluteLayout 内 这是我的代码的 sn-p

      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
      .....
          .....>
           <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
           <Image  x:Name="imageBG" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" Aspect="AspectFill"    Source="myimage.jpg" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"/>
      
          <StackLayout AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" >
          ....
          </StackLayout>
      
          </AbsoluteLayout>
      

      要更改源 prof CS 代码,这里有一个我使用过的小 sn-p

        string serverAdress = Constants.HTTP_PRE + Constants.SERVER_IP;
                      Logo.Source = new UriImageSource
                      {
                          Uri = new Uri(serverAdress + Constants.HEADER_IMAGE),
                          CachingEnabled = true
                      };
      

      为了更好地控制服务器响应是否有效...

          try
          {
              string url = Constants.HTTP_PRE + DependencyService.Get<IStoredData>().GetData(Constants.SHARED_PREF_ADDRESS);
              url += Constants.HEADER_IMAGE;
              HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(url);
              //You should be getting only the response header
              wrq.Method = "HEAD";
              if (    ( (HttpWebResponse)wrq.GetResponse()    ).ContentLength > 0) // if filePath is correct - serverIP is set
              {
                  Uri uri = new Uri(url);
                  UriImageSource uriImageSource = new UriImageSource { Uri = uri, CachingEnabled = true };
                  Logo.Source = uriImageSource;
              }
          }catch (Exception e) { System.Console.WriteLine(e.StackTrace); }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-03
        • 2018-05-08
        • 2016-09-27
        • 1970-01-01
        • 1970-01-01
        • 2014-08-02
        相关资源
        最近更新 更多