Xaml 如何在Xamarin.Forms UWP中使用图像?
我目前正在Xamarin.Forms UWP中创建一个示例系统。我想知道为什么我调用图像的代码在UWP部分似乎不能正常工作,而它在Android中工作。我还想将图像设置为背景,将图像设置为按钮 如何对其进行编码,使其在两种平台上都能正常工作 以下是我使用的代码:Xaml 如何在Xamarin.Forms UWP中使用图像?,xaml,xamarin,xamarin.forms,uwp,uwp-xaml,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" x
<?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>资源>可绘制的
对于UWP中的Xamarin表单,图像必须位于根项目目录(不在资产中)中,并且构建操作必须设置为内容 然后,图像将以Xamarin格式在Android和iOS中工作。在UWP中,您可以使用
img.Source=新位图图像(新Uri(“ms”)-appx:///Assets/Logo.png"));代码>
根据下面的屏幕截图,图像位于资产文件夹中
对于UWP Xamarin表单:
在根目录中创建名为“scale-100”的文件夹,然后将所有图像放入其中
使用扩展名ex指定图像名称:
您还可以根据此创建“scale xxx”在UWP项目中创建一个名为Resources的文件夹,将包含内容的图像放在那里作为构建操作
然后使用这个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);
}
}
}
添加标记扩展foler作为xaml名称空间,并使用如下方式
<Button ImageSource="{local:PlatformResourceImage clock.png}"/>
<Image Source="{local:PlatformResourceImage clock.png}"/>
现在,您可以在所有平台上对相同的映像路径使用相同的语法
您可以创建一个文件夹并使用更改路径,如下所示:
[程序集:ExportRenderer(typeof(图像)、typeof(CustomImageRenderer))]
公共类CustomImageRenderer:ImageRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
if(例如NewElement!=null)
{
var source=e.NewElement.source;
如果(源为FileImageSource FileImageSource)
{
fileImageSource.File=$“资产/{fileImageSource.File}”;
}
}
基础。一个要素发生变化(e);
}
}
显然,您需要在uwp和android中使用image,因此我的解决方案如下:
在xaml中声明图像
在pcl项目中添加“资源”文件夹
在“资源”中添加“图像”文件夹
粘贴图像并转到图像属性(右键单击>属性)
将“构建操作”更改为“嵌入式资源()
在页面代码中添加以下内容:
kitty.Source=ImageSource.FromResource(“stackoverflow.Resources.Images.kitty.png”,typeof(MainPage.Assembly);
优点:
- 您可以多次使用同一图像,而不需要在每个项目中使用副本
缺点:
- 不知道是否可以在MVVM项目中使用它
如果您只想使用uwp项目而不是其他项目,只需在“资产”中添加一个文件夹,并使您的xaml源代码如下
哦,我明白了。但是我需要使用哪些代码?我可以使用这些代码吗?是的,如果图像是字符串形式的,例如,如果您将图像放在drawable(在Android上)、root project(在Windows上)、Resources(在iOS上)中然后,它将在所有3个平台上以Xamarin格式正确显示。我已经尝试过了,先生,您的代码中有2个引号吗?我注意到,每次您提到filename.png时,最后只有一个引号。”是的,这真是一团糟:)如果你想使用文件夹,唯一的办法就是在每个图像源上放置一个ValueConverter,并更改UWP/WinRT上任何图像的URL