【问题标题】:Image doesn't show in Xamarin.forms (PCL)图像未在 Xamarin.forms (PCL) 中显示
【发布时间】:2016-05-11 01:55:42
【问题描述】:

我有一个名称为 fb.png 的图像,它位于根项目 (prtable) 中,我将此图像添加到 Droid 项目中的 Resource>drawble

Thees 是我的MainPage.xaml 代码:

 <Image x:Name="img1"></Image>

Thees 是我的MainPage.xaml.cs 代码:

 public MainPage()
    {
        InitializeComponent();

        ImageSource img = ImageSource.FromResource("App2.fb.png");
        img1.Source = img;
        img1.Aspect = Aspect.AspectFit;
        img1.BackgroundColor = Color.Navy;

     }

需要出现什么变化?

【问题讨论】:

  • 您说您的图像被称为 fb.png,但在您的代码中您将其称为“App2.fb.png”。您需要在 ImageSource.FromResource 中使用“fb.png”

标签: xaml xamarin xamarin.forms


【解决方案1】:

如果文件保存在 Resources/Drawable 目录中,则使用 FromFile,而不是 FromResource。 FromResource 用于在您构建的库中打包为嵌入资源的图像。

您还需要指定文件在 Resources/Drawable 中显示的确切名称,因此应该这样做:

public MainPage()
{
    InitializeComponent();

    ImageSource img = ImageSource.FromFile("fb.png");
    img1.Source = img;
    img1.Aspect = Aspect.AspectFit;
    img1.BackgroundColor = Color.Navy;

 }

【讨论】:

  • 其他要尝试的事情:确保您的 Android 项目中 Resources/drawable 中的 fb.png 具有 AndroidResource 的 BuildAction。然后发布整个 MainPage.xaml 文件,因为容器视图可能会导致问题。并且更具体地说明“它不起作用”的意思。是不是图片不显示?崩溃了?
  • 我将 buildActioncontent 设置为 AndroidResource 并且图像正在显示。
【解决方案2】:

这里是 MVVM 绑定图像资源到图像控制的完整实现。您需要将视图模型设置为 XAML 所在页面的上下文。同样以“App2.fb.png”访问似乎很奇怪,它应该只是 fb.png。这可能是一个更简单的修复..只需将图像源重命名为 Droid > 资源中列出的图像的确切名称

XAML

<Image
Aspect="AspectFit"
Source="{Binding PropertyImageStatusSource}">

基础视图模型

让您的视图模型从视图模型基类继承,以便 INotifyPropertyChanged 在您的访问器上普遍实现。

    public class _ViewModel_Base : INotifyPropertyChanged
    {


        //figure out what is getting set
        public virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);



            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        //attach handler with property name args to changing property, overridable in inheriting classes if something else needs to happen on property changed
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

视图模型

Public MyViewModel : _ViewModel_Base 
{
    private string ImageStatusSource = "fb.png";
    public string PropertyImageStatusSource
    {
        set { SetProperty(ref ImageStatusSource, value); }
        get { return ImageStatusSource; }
    }

}

【讨论】:

  • 不幸的是它不起作用。我在public MainPage() 方法中放了上面的codeBehind?
  • 我认为这实际上可能是您命名资源的方式。更新了反映的答案。
  • 希望人们能说出他们投反对票的原因。属性更改基本视图模型方法是教科书 MVVM Xamarin。视图模型除了设置一个封装的属性来绑定之外什么都不做。这种方法在我自己的企业应用程序中使用,将在developer.xamarin.com/guides/xamarin-forms/… 中找到;如果有更好的方法来处理它,我总是愿意讨论而不是不加解释地投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多