3

I'm working through a project where I'm going to have multiple square size instances of the same set of form components.

I can either create 8 instances manually in my form UI or what I'd rather do is create a view (or Item Renderer) and then dynamically add instances of that view to my main view.

How do I add a create and add a custom view dynamically to the main view in my Xamarin form?

Note: Including Swift tag because you might know the answer if you know Swift or Objective C since the API wraps Apple API.

If IIUC:

  • Create a view in XCode Interface Builder
  • In ViewDidLoad create an instance of the custom instance views
  • Add each instance to the main view

I'd read a guide if there was one but I can't find anything specifically on this.

Some what related. I can create a new View in Xcode interface Builder pretty easily. Is there a way to export that as a class to my application?

enter image description here

Update:
I've found a textfield in Interface Builder where I can enter the name of a class. Back in Visual Studio my main View Controller can see the HelloWorld class. I've found a method named AddChildViewController. I try testing it. Nothing happens. Do I need to set the position and size? I can't find any API to do this.

Tomorrow I will scour the ancient texts again for example code. Maybe there is something I missed?

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

        var view = new HelloView(this.Handle);
        var handle = view.Handle;
        base.AddChildViewController(view);
        var view2 = new HelloView(this.Handle);
        handle = view.Handle;
        base.AddChildViewController(view2);
    }

I noticed a note in the console log:

<ViewController: 0x600000a94180> already have child of: <ViewController: 0x600000a94180>

Update II:
This adds a new NSView and then a NSButton in that view to the main window:

var frame = new CoreGraphics.CGRect(0, 0, 100, 100);
var button = new NSButton(frame) {
    Title = "My Button",
};

var view = new NSView(frame) {};
view.AddSubview(button);
View.AddSubview(view);

It doesn't add my custom class yet though.

Update III:
I'm able to add the custom HelloWorldView class but the controls are not visible. If I add a button to the form I see it but it is anchored to the bottom of the screen. I don't see the controls created from Interface Builder.

        //var frame = this.View.Frame;
        var frame = new CoreGraphics.CGRect(0, 0, 100, 20);
        var button = new NSButton(frame) {
            Title = "My Button"
        };
        var frame2 = new CoreGraphics.CGRect(0, 0, 100, 100);
        var helloView = new HelloView() {
        };

        helloView.Frame = frame2;
        helloView.AddSubview(button);
        mainFrame.AddSubview(helloView);

HelloView.cs:

public partial class HelloView : NSView
{
    public HelloView () : base ()
    {
    }
}

Note about the code above: I removed the handle parameter because it was causing a compiler error. Setup: Visual Studio for Mac using Xamarin C# and XCode Interface Builder

-- Notes for the bounty.

To receive the bounty you must show how to do either step 1 or step 2 mentioned above in the Bounty Requirements section. I prefer step 1. If you are unsure ask.

enter image description here

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • I am confused, Are you building a mac app or mobile app? – BoredToDeath May 27 '21 at 13:58
  • @BoredToDeath mac app – 1.21 gigawatts May 27 '21 at 23:21
  • Have you taken a look at how they add the "MainViewController" here: https://learn.microsoft.com/en-us/xamarin/ios/user-interface/storyboards/indepth-storyboard?tabs=macos#creating-a-storyboard-with-xcode The IDE should create a class here automatically if you follow the instructions (I know you are creating a MacOS app vs an iOS app, but it should work similarly) Also, try to simplify your question to one ask and update your question so you get appropriate attention – Saamer May 30 '21 at 23:27
  • @Saamer hi i read through that. it looks interesting but i don't think that will help since it initializes the view as a storyboard. my code above sort of works except for the positioning andor stack view not doing vertical or horizontal layouts. – 1.21 gigawatts May 31 '21 at 04:51
  • Could you please share screenshots of what it looks like right now and what you want it to look like? – Saamer Jun 01 '21 at 10:39
  • I can do item 2 already. I can already programmatically add a view and buttons to the view. What I'd like is to add a Storyboard multiple times to a view in a grid pattern of 4 across and 2 rows of the image above. If this expires I'll add another question simply asking for item 1. It should be much easier to read and answer. – 1.21 gigawatts Jun 02 '21 at 02:03
  • So there are no answers and bounties are non-refundable? – 1.21 gigawatts Jun 02 '21 at 14:58

0 Answers0