簡體   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