【问题标题】:How to use Images in Xamarin.Forms UWP?如何在 Xamarin.Forms UWP 中使用图像?
【发布时间】:2016-10-27 20:35:12
【问题描述】:

我目前正在 Xamarin.Forms UWP 中创建示例系统。我想知道为什么我的调用图像的代码在 UWP 部分中似乎无法在 Android 中正常工作。我还想将图像设置为背景,将图像设置为按钮。

我如何编写代码以使其在两个平台上都能正常运行?

这是我使用的代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XamarinFormsDemo.Views.LoginPage"
         BackgroundImage="bg3.jpg"
         Title="MainPage">
  <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />

  <StackLayout VerticalOptions="Center"
             Padding="40">
    <Image Source="ebmslogo1.png"/>

    <StackLayout Padding="0,50,0,0">

      <Entry x:Name="txtUserName"
             Placeholder="Username"
             x:Hint="Username"
             BackgroundColor="Black"
             TextColor="White"/>

  <Entry x:Name="txtPassword"
         Placeholder="Password"
         IsPassword="true"
         BackgroundColor="Black"
         TextColor="White"/>

      <Button Text="LOG IN"
          FontSize="14"
         BackgroundColor="Teal"
         Clicked="NavigateButton_OnClicked"/>

    </StackLayout>

  </StackLayout>

</ContentPage>

我的图片位于 .Droid > Resources > drawable

【问题讨论】:

    标签: xaml xamarin xamarin.forms uwp uwp-xaml


    【解决方案1】:

    对于 UWP 中的 Xamarin 表单,图像必须位于项目根目录(而不是资产)中,并且构建操作设置为内容。

    然后,图像将像在 Xamarin Forms 中的 Android 和 iOS 中一样工作。

    【讨论】:

    • 哦,我明白了。但是我需要使用什么代码?我可以使用此代码吗?
    • 是的,如果您将图像作为字符串,例如 然后,如果您将该图像放置在可绘制对象(在 Android 上)、根项目(在 Windows 上)、资源(在 iOS 上),那么它将在所有 3 个平台上的 Xamarin Forms 中正确显示。
    • 我已经尝试过这个先生, 但它没有用。我不知道为什么:(跨度>
    • 你的代码中有2个引号吗?我注意到你最后只有一个,每次你提到filename.png"
    • 是的,一团糟 :) 如果您想使用文件夹,唯一的解决方法是在每个图像源上放置一个 ValueConverter 并更改 UWP/WinRT 上任何图像的 URL
    【解决方案2】:

    在 UWP 中你可以使用

    img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Logo.png"));

    图片位于 Assets 文件夹中,如下图所示

    【讨论】:

      【解决方案3】:

      对于 UWP Xamarin 表单:

      1. 在根目录中创建名为“scale-100”的文件夹,然后将所有图像放入其中。
      2. 指定带有扩展名的图像名称例如:&lt;Image Source="myimage.png"/&gt;

      您还可以根据Link 的指定创建“scale-xxx”

      【讨论】:

        【解决方案4】:

        在 UWP 项目中创建一个名为 Resources 的文件夹,将带有 Content 作为构建操作的图像放在那里

        然后使用这个 MarkupExtension

        using System;
        using Xamarin.Forms;
        using Xamarin.Forms.Xaml;
        
        namespace XamarinFormsMoshCourse.MarkupExtensions
        {
            [ContentProperty(nameof(Source))]
            public class PlatformResourceImageExtension : IMarkupExtension
            {
                public string Source { get; set; }
        
                public object ProvideValue(IServiceProvider serviceProvider)
                {
                    if (String.IsNullOrWhiteSpace(Source)) return null;
        
                    string imageSource = String.Empty;
        
                    switch (Device.RuntimePlatform)
                    {
                        case Device.iOS:
                            {
                                imageSource = Source;
                                break;
                            }
                        case Device.Android:
                            {
                                imageSource = Source;
                                break;
                            }
                        case Device.UWP:
                            {
                                imageSource = String.Concat("Resources/", Source);
                                break;
                            }
                    }
                    return ImageSource.FromFile(imageSource);
                }
            }
        }
        

        将标记扩展文件夹添加为 xaml 命名空间并像这样使用

             <Button ImageSource="{local:PlatformResourceImage clock.png}"/>
                <Image Source="{local:PlatformResourceImage clock.png}"/>
        

        现在您可以在所有平台上使用相同的语法和相同的图像路径

        【讨论】:

          【解决方案5】:

          您可以创建一个文件夹并使用Custom Renderer 更改路径,如下所示:

          [assembly: ExportRenderer(typeof(Image), typeof(CustomImageRenderer))]
          
          public class CustomImageRenderer : ImageRenderer
          {
              protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
              {
                  if(e.NewElement != null)
                  {
                      var source = e.NewElement.Source;
                      if(source is FileImageSource fileImageSource)
                      {
                          fileImageSource.File = $"Assets/{fileImageSource.File}";
                      }
                  }      
                  base.OnElementChanged(e);
              }
          }
          

          【讨论】:

            【解决方案6】:

            显然你需要在 uwp 和 android 中使用图像,所以这里是我的解决方案:

            1. 在 xaml 中声明图像
            <Image x:Name="kitty"/>
            
            1. 在 pcl 项目中添加“Resources”文件夹
            2. 在“资源”中添加“图像”文件夹
            3. 粘贴图像并转到图像属性(右击>属性)
            4. 将“构建操作”更改为“嵌入式资源 (microsoft documentation)
            5. 在你的页面代码中添加这个:
            kitty.Source = ImageSource.FromResource("stackoverflow.Resources.Images.kitty.png", typeof(MainPage).Assembly);
            

            优势:

            • 您可以多次使用同一张图片,而无需在每个项目中复制一份

            缺点:

            • 不知道能不能用在MVVM项目中

            如果你只是想使用你的 uwp 项目而不是另一个,只需将一个文件夹添加到“资产”并让你的 xaml 源像这样

            <Image x:Name="kitty" Source="Assets/Images/kitty.png"/>
            

            【讨论】:

              猜你喜欢
              • 2017-09-14
              • 2021-01-28
              • 2019-12-21
              • 2016-10-29
              • 1970-01-01
              • 2018-08-08
              • 1970-01-01
              • 2022-07-22
              • 1970-01-01
              相关资源
              最近更新 更多