Tôi có một Dự án Xamarin được tạo kiểu với MvvmCross. Có Các tiểu dự án:Xamarin: Cách tải hình ảnh từ dự án thư viện iOS
- Core (PCL)
- ViewModel (PCL)
- iOS (thực thi)
Nếu tôi thêm hình ảnh cho dự án iOS của tôi (Resoureces/Images/test_image. png), sau đó tôi có thể tải nó với mã này:
UIImage image = UIImage.FromBundle("Images/test_icon.png");
Bây giờ, tôi muốn sử dụng một tiểu dự án mới
- Controls (iOS Library)
Thư viện này nên tải một hình ảnh. Tôi đã thêm hình ảnh vào Điều khiển (Resoureces/Images/test_image.png)
Nhưng tôi không thể tải hình ảnh này trong phần Điều khiển.
Câu hỏi của tôi: Cách tải hình ảnh từ thư viện 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);
}
}
và lớp ViewController là:
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));
}
}
Dưới đây là toàn Proj: https://bitbucket.org/ww_wschaefer/xamarin-first-crossover-app/overview
Bạn không thể sử dụng từ Bundle trừ khi có sẵn của nó như là nguồn bundeled, tôi nghĩ bạn sẽ là tốt hơn về sử dụng 'UIImage.FromFile ("Images/test_icon.png") '. Bạn cũng nên xem [Câu hỏi] này (http://stackoverflow.com/q/18130779/1155650). –
Tôi không thể tin được, giải pháp này thật dễ dàng, cảm ơn Rohit. – iOSfleer
vui mừng vì sự cố của bạn đã được giải quyết. Tôi đã thêm phần giải thích nếu bạn quan tâm. –