简体   繁体   English

Xamarin:如何从iOS库项目加载图像

[英]Xamarin: How to load image from iOS library project

I have a Xamarin Project styled with MvvmCross. 我有一个用MvvmCross设计的Xamarin项目。 There are Subprojects: 有子项目:

  • Core (PCL) 核心(PCL)
  • ViewModel (PCL) ViewModel(PCL)
  • iOS (Executable) iOS(可执行文件)

If i add an image to my iOS project (Resoureces/Images/test_image.png), then i can load it with this code: 如果我将图像添加到我的iOS项目(Resoureces / Images / test_image.png),那么我可以使用以下代码加载它:

UIImage image = UIImage.FromBundle("Images/test_icon.png");

Now, i want to use a new Subproject 现在,我想使用一个新的子项目

  • Controls (iOS Library) 控件(iOS库)

This library should load an image. 该库应该加载图像。 I added an image to Controls (Resoureces/Images/test_image.png) 我在Controls(Resoureces / Images / test_image.png)中添加了一个图像

But i can not load this image in Controls proj. 但我无法在Controls proj中加载此图像。

My Question: How to load images from iOS libraries? 我的问题:如何从iOS库加载图像?

    public class MyButton : UIButton
    {
        public MyButton () : base()
        {
            Initialize ();
        }

        void Initialize()
        {
            // load image from bundle
            UIImage image = UIImage.FromBundle("Images/test_icon.png");
            // image is null
            this.SetImage (image, UIControlState.Normal);
        }
    }

and the ViewController class is : 而ViewController类是:

    public partial class FirstView : MvxViewController
    {
        public FirstView () : base ("FirstView", null)
        {
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            // load image from bundle
//          UIImage image = UIImage.FromBundle("Images/test_icon.png");
//          image is not null if added in iOS Proj
//          this.imageView.Image = image;

            MyButton button = new MyButton ();

            View.Add (button);

            View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 10));
            View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, 74));
            View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, 64)); 
            View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, 64)); 
        }
    }

在此输入图像描述

Here is full proj: https://bitbucket.org/ww_wschaefer/xamarin-first-crossover-app/overview 这是完整的项目: https//bitbucket.org/ww_wschaefer/xamarin-first-crossover-app/overview

A little explanation on my comment. 对我的评论有点解释。

You have to change 你必须改变

UIImage image = UIImage.FromBundle("Images/test_icon.png");

to

UIImage image = UIImage.FromFile("Images/test_icon.png");

As the image is not added as bundled resource. 由于图像未添加为捆绑资源。

The UIImage.FromFile() method loads the image asynchronously. UIImage.FromFile()方法异步加载图像。 It also allows the application to load the image from an external location . 它还允许应用程序从外部位置加载图像

Unlike the UIImage.FromFile() method, the UIImage.FromBundle() method is a blocking call and only loads images from within the application bundle . UIImage.FromFile()方法不同, UIImage.FromBundle()方法是一个阻塞调用, 只加载应用程序包中的图像 However, it caches the images after loading it. 但是,它会在加载后缓存图像。

For further understanding have a look at the book - Developing C# Apps for iPhone and iPad using MonoTouch 为了进一步了解,请阅读本书 - 使用MonoTouch为iPhone和iPad开发C#应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM