我第一次尝试使用Xamarin Forms,所以这可能是一个写得不好的问题,请原谅。
我有一张图片作为嵌入式资源: screenshot 我能在XAML中将该图片用作ContentPage的BackgroundImage吗?如何实现?
提前感谢大家。
更新,如此处所建议: https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images:
- 我在主页面cs中添加了自定义的xaml标记扩展:
[Xamarin.Forms.ContentProperty("Source")]
public class ImageResourceExtension : Xamarin.Forms.Xaml.IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = Xamarin.Forms.ImageSource.FromResource(Source);
return imageSource;
}
}
我现在可以像这样使用图片:
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<!-- use a custom Markup Extension -->
<Image Source="{local:ImageResource App2.images.back_01.png}" />
</StackLayout>
仍然无法回答原始问题,如何将其设置为后台。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App2;assembly=App2"
x:Class="App2.MainPage"
BackgroundImage="">
local:ImageResourceExtension
而不是local:ImageResource
。 - Fabrice T