首页 文章

Xamarin Forms将资源图像作为XAML中ContentPage的背景嵌入

提问于
浏览
1

我是第一次尝试Xamarin表格所以这可能是一个写得很糟糕的问题,请原谅我 .

我有一个图像作为Embeded资源:screenshot我可以将该图像用作XAML中的BackgroundImage,用于ContentPage吗?怎么样

谢谢大家 .

更新,如此处所建议:https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images

  • 我将自定义xaml标记扩展添加到主页cs:

[Xamarin.Forms.ContentProperty(“Source”)]
public class ImageResourceExtension:Xamarin.Forms.Xaml.IMarkupExtension
{
public string Source {get;组; }

公共对象ProvideValue(IServiceProvider serviceProvider)
{
if(Source == null)
{
return null;
}
//使用您需要的任何方法在此处进行翻译查找
var imageSource = Xamarin.Forms.ImageSource.FromResource(Source);

return imageSource;
}
}

  • 我现在可以使用这样的图像:

<StackLayout VerticalOptions =“Center”HorizontalOptions =“Center”>
<! - 使用自定义标记扩展 - >
<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:本地= “CLR-名称空间:App2的;装配= App2的”
X:类= “App2.MainPage”
的BackgroundImage = “”>

2 回答

  • 0

    将图像放到文件夹:

    for Android into Resources/drawable

    for iOS into Resources

    然后试试

    <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="back01.png">
    
  • -2

    似乎只在便携式中生成图像嵌入式资源,适用于除BackgroundImage之外的所有图像用法 .
    要用于BackgroundImage,图像必须也嵌入在每个平台资源中(必须选择每个平台构建操作的正确) .
    然后,对于BackgroundImage,它可以像这样被引用:"folder_name/file_name.png"因此,它必须多次执行一次(与其他图像使用相反) .

    以上对我来说是一个解决方案 .
    如果有人知道怎么做而没有多余的行动,请发帖 .

相关问题