2016-06-11 27 views
2

Es ist möglich, Ansicht von einem benutzerdefinierten XIB in Xamarin zu erstellen und zu laden? In Objective-C ist wie:XIB in ViewController Xamarin laden?

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

Antwort

1

1 - Erstellen Sie Ihre XIB-Datei (Beispiel MyView).

2 - in der .cs in die XIB-Datei hinzufügen, um diese statische Konzept Methode verwendet:

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

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

      return v; 
     } 
    } 

3 - Hinzufügen MyView zum 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); 
    } 
} 

Sie können here mehr lesen.

+0

statische Methode Create ist kein statischer Konstruktor. – Grigory

+0

Die Idee hier ist das Erstellen der Xib über eine statische Methode, nicht von einem statischen Konstruktor. – jzeferino

+0

Ich weiß. Sie haben geschrieben, dass die Create-Methode ein statischer Konstruktor ist. Es ist nicht. Es ist eine normale statische Methode aus C# Perspektive. – Grigory