XAML Xamarin图像不显示在手机

yhived7q  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(122)

我使用xamarin.forms和我试图使图像。我尝试了这么多的东西,但图标不显示在手机上。

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

namespace ArduinoNotes
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        protected override void OnAppearing()
        {
            base.OnAppearing();
            NavigationPage.SetHasNavigationBar(this, true);
        }
    }
}
<?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="ArduinoNotes.MainPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem
            Text="a"/>
    </ContentPage.ToolbarItems>
    <StackLayout>
        <Image x:Name="mainImage"
               Source="icons/logo.png"
               WidthRequest="50"
               HeightRequest="50">
        </Image>
    </StackLayout>
</ContentPage>

solution explorer
我试着把源代码写成logo.png、icons.logo.png和ArduinoNotes.icons.logo.png(ArduinoNotes是我的项目名)。

6ju8rftf

6ju8rftf1#

看来你想显示一个本Map像。我想在这里给予一些建议。
iOS的一个简单方法是将您的图像放在ProjectName.iOS/Resources文件夹中。您也可以在Resources中创建一个新文件夹,并将图像放在该文件夹中。然后将图像的Build Action设置为BundleResource。然后我们可以使用它,例如:

<Image x:Name="mainImage" BackgroundColor="Pink"
     Source="icons/monkeyface.png"
     WidthRequest="50"
     HeightRequest="50">

在iOS中,我们也应该考虑Safe Area Layout Guide on iOS。(因为当我调试时,我发现我的图像在安全区,所以我看不到它)我们可以设置

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
     xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
     ios:Page.UseSafeArea="true"
     ....
     >

对于Android,将图像放在ProjectName.Android/Resources/drawable文件夹中,并将Build Action设置为AndroidResource。(但由于现在我们无法在drawable中创建子文件夹)然后使用它:

<Image x:Name="mainImage" BackgroundColor="Pink"
     Source="monkeyface.png"
     WidthRequest="50"
     HeightRequest="50">

希望对你有用。

相关问题