繁体   English   中英

Xamarin.form使用嵌入式图像

[英]Xamarin.form using embedded images

我想在我的可移植代码中使用嵌入式图像。 我正在使用标记扩展程序,如许多其他视频和指南所示。 我的项目编译并运行,但是没有图像显示。 这些指南说将图像放在“引用”下,但是我无法将图像拖到那里或右键单击并添加图像,因此它们位于名为“图像”的文件夹中。 如果问题在于它们不在参考文献中,那么我该如何将它们保存在参考文献中? 否则,如果有人可以发现该错误,将不胜感激。

https://www.c-sharpcorner.com/article/add-images-icons-and-splash-screen-in-xamarin-forms/

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=vswin

About.xaml.cs:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SVCAPB
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class About : ContentPage
    {
        public About()
        {
            InitializeComponent();
            Image embeddedImage = new Image { Source = ImageSource.FromResource("SVCAPB.Images.apbLeaders.jpg") };
        }
    }
}

About.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SVCAPB.MarkupExtensions"
             x:Class="SVCAPB.About"
             Title="About">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout
            HorizontalOptions="Fill"
            VerticalOptions="Fill"
                BackgroundColor="#FFC107">
                <Image Source="{local:EmbeddedImage ResourceId=SVCAPB.Images.apbLeaders.jpg }"></Image>
            </StackLayout>
            </ScrollView>
    </ContentPage.Content>
</ContentPage>

EmbeddedImage.cs(在MarkupExtensions文件夹下):

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SVCAPB.MarkupExtensions
{
    public class EmbeddedImage : IMarkupExtension
    {
        public string ResourceId
        {
            get;
            set;
        }
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (String.IsNullOrWhiteSpace(ResourceId)) return null;
            return ImageSource.FromResource(ResourceId);
        }
    }
}

您的代码是正确的,问题出在添加图像或类的引用的方式上

1.-右键单击“图像”文件夹->“添加”->“添加文件”->选择图像2.-此步骤是必需的,因为它已在文档中进行了标记-如果您使用的是Xamarin Studio,请右键单击图像->“构建”操作-> EmbeddedResource-如果您使用的是Visual Studio,请右键单击->属性->构建操作-> EmbeddedResource

要么

如果使用Xamarin Studio,则只需要xmlns:local =“ clr-namespace:SVCAPB”;如果使用Visual Studio,则需要指定目录xmlns:local =“ clr-namespace:SVCAPB.MarkupExtensions”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM