FromResource不从项目文件夹返回图像

时间:2017-05-11 17:37:41

标签: xaml xamarin

我正在学习Xamarin,我弄清楚为什么ResourceId没有在我的项目中返回图像。我只是得到一个空白页面。 App.xaml.cs将MainPage设置为下面的ImagePage.XAML,这不是问题。我将资源ID设置为文件夹的路径,但没有。

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:HelloWorld.MarkupExtensions;assembly=HelloWorld.Android"
             x:Class="HelloWorld.ImagePage">

<!--
        <ActivityIndicator IsRunning ="{Binding Source={x:Reference Image}, Path=IsLoading}" AbsoluteLayout.LayoutBounds="0.5, 0.5, 100, 100" AbsoluteLayout.LayoutFlags="PositionProportional"  Color="#428bca" />
-->
        <Image x:Name="Image" Aspect="AspectFill"/>


</ContentPage>

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 HelloWorld
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ImagePage : ContentPage
    {
        public ImagePage ()
        {
            InitializeComponent ();

          Image.Source = ImageSource.FromResource("HelloWorld.Images.background.jpg");

        }


    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

  

本地图片

     

图像文件可以添加到每个应用程序项目中,并从Xamarin.Forms共享代码中引用。要在所有应用中使用单个图像,必须在每个平台上使用相同的文件名,它应该是有效的Android资源名称(即只有小写字母,数字,下划线和句点)是允许的。)

Here您有官方文档,说明如何在Xamarin表格中使用本地图像。

<强> 修改

你忘记了图像和背景之间的点。 HelloWorld.Images.background.jpg而不是HelloWorld.Imagesbackground.jpg?

相关问题