加载XIB到ViewController Xamarin中? [英] Load XIB into ViewController Xamarin?

查看:87
本文介绍了加载XIB到ViewController Xamarin中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从xamarin中的自定义XIB创建和加载视图? 在Objective-C中就像:

It is possible to create and load view from a custom XIB in xamarin? In Objective-C is like:

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;

推荐答案

1-创建您的XIB文件(例如MyView).

1 - Create your XIB file (example MyView).

2-在与XIB文件相关的.cs文件中,添加以下静态创建者方法:

2 - In the .cs related to the XIB file add this static creator method:

partial class MyView : UIView
    {
        public MyView (IntPtr handle) : base (handle)
        {    
        }

        public static MyView Create()
        {    
            var arr = NSBundle.MainBundle.LoadNib ("MyView", null, null);
            var v = arr.GetItem<MyView>(0);

            return v;
        }
    }

3-将MyView添加到ViewController:

public partial class ViewController : UIViewController
{
    MyView v;
    public ViewController (IntPtr handle) : base (handle)
    {
    }

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

        v = MyView.Create();
        v.Frame = View.Frame;
        View.AddSubview (v);
    }
}

您可以在此处阅读更多内容.

You can read more here.

这篇关于加载XIB到ViewController Xamarin中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆