如何在Xamarin.Forms UWP中使用图像?

Jay*_*sta 2 xaml xamarin xamarin.forms uwp uwp-xaml

我目前正在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>
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

Ada*_*ley 11

对于UWP中的Xamarin Forms,图像必须位于根项目目录中(不在Assets中),并且构建操作设置为Content.

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


Esa*_*rif 5

对于 UWP Xamarin 表单:

  1. 在根目录中创建名为“scale-100”的文件夹,然后将所有图像放入其中。
  2. 使用扩展名指定图像名称 ex: <Image Source="myimage.png"/>

您还可以从此链接中指定创建“scale-xxx”