仅使用C#显示图像Xamarin表单的问题



我在参考此网站上问这个问题https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#local_images

我在网站上尝试了XAML代码,但是在我创建了一个新的Xamarin PCL项目并测试了C#代码之后。它无法显示图像。

我做了什么:从abotpage.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"
             x:Class="EmbbedImages.Views.AboutPage"
             xmlns:vm="clr-namespace:EmbbedImages.ViewModels;"
             Title="{Binding Title}">
  <ContentPage.BindingContext>
    <vm:AboutViewModel />
  </ContentPage.BindingContext>
</ContentPage>

,然后是agationpage.xaml.cs,我对其进行了修改,使其看起来如下:

using Xamarin.Forms;
namespace EmbbedImages.Views
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();
            var beachImage = new Image { Aspect = Aspect.AspectFit };
            beachImage.Source = ImageSource.FromFile("butterfly.jfif");
        }
    }
}

我已经确保将蝴蝶映像添加到我的Android绘制文件夹中,但是没有显示图像。由于我是Xamarin和Android以及C#的新手,任何帮助将不胜感激。

图像可能不会显示,因为它不是页面的一部分。如果您将其添加到页面上,但仍然不会显示它在加载/打开文件/解码时必须是一个问题。

您要么将其添加到XAML中,要么将代码编辑为以下内容:

using Xamarin.Forms;
namespace EmbbedImages.Views
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();
            var beachImage = new Image { Aspect = Aspect.AspectFit };
            beachImage.Source = ImageSource.FromFile("butterfly.jfif");
            this.Content = beachImage;    
        }
    }
}

在XAML中添加它看起来像:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="EmbbedImages.Views.AboutPage"
             xmlns:vm="clr-namespace:EmbbedImages.ViewModels;"
             Title="{Binding Title}">
  <ContentPage.BindingContext>
    <vm:AboutViewModel />
  </ContentPage.BindingContext>
  <ContentPage.Content>
    <Image x:Name="beachImage" Aspect="AspectFit">
  </ContentPage.Content>
</ContentPage>

和背后的代码:

using Xamarin.Forms;
namespace EmbbedImages.Views
{
    public partial class AboutPage : ContentPage
    {
        public AboutPage()
        {
            InitializeComponent();
            beachImage.Source = ImageSource.FromFile("butterfly.jfif");
        }
    }
}

最新更新