Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Xamarin's Storyboard and F#

Tags:

ios

xamarin

f#

since its not (yet) possible to use the Storyboard for generating classes and glue code. I had the idea to write the F# classes on my own and do the glueing myself.

So I written this loading of the Storyboard namespace

open System
open MonoTouch.UIKit
open MonoTouch.Foundation

[<Register("AppDelegate")>]
type AppDelegate() = 
    inherit UIApplicationDelegate()

    member val window = null with get, set

    override this.FinishedLaunching(app, options) = 
        this.window <- new UIWindow(UIScreen.MainScreen.Bounds)
        let Storyboard = UIStoryboard.FromName("MainStoryboard", null)
        this.window.RootViewController <- Storyboard.InstantiateInitialViewController() :?> UIViewController
        this.window.MakeKeyAndVisible()
        true

module Main = 
    [<EntryPoint>]
    let main args = 
        UIApplication.Main(args, null, "AppDelegate")
        0

and the following controller class

open System
open System.Drawing
open MonoTouch.UIKit
open MonoTouch.Foundation

[<Register("HomeController")>]
type HomeController() = 
    inherit UIViewController()

    override this.ViewDidLoad() = 
        base.ViewDidLoad()
        System.Console.WriteLine("FOO!")

Then I created the Storyboard (see attached pictures). Viewsproperties

AND - everything gets loaded and the storyboard works fine - One exception: ViewDidLoadnever gets called. Obviously I was not successful in attaching my hand coded controller.

Does anybody have an idea how do accomplish this?

like image 491
robkuz Avatar asked Dec 02 '14 13:12

robkuz


People also ask

How do I use storyboard in Xamarin iOS?

Create a new Storyboard file by right-clicking on the project to Add > New File > iOS > Empty Storyboard. Add your Storyboard name to the Main Interface section of the iOS Application. This does the equivalent of instantiating the Initial View Controller in the FinishedLaunching method within the App Delegate.

What are the various methods of Xamarin application that can be made?

For app development, Xamarin uses two methods: Xamarin. Forms and Xamarin Native. Forms, while native UI technology and MVC or MVVM Cross Architecture are used in Xamarin Native. MVVM and XAML are used in Xamarin.


1 Answers

I found the answer. Instead of creating a Controller without parameters

[<Register("HomeController")>]
type HomeController() = 
    inherit UIViewController()

One must create a controller with an pointer and init the base controller with that pointer too.

[<Register("HomeController")>]
type HomeController(handle:IntPtr) = 
    inherit UIViewController(handle)

To attach the controls on the view with the view controller (e.g. attach the button named "Clicker")

enter image description hereenter image description here

one has to add the following code to your view controller

[<Register("HomeController")>]
type HomeController(handle:IntPtr) = 
    inherit UIViewController(handle)

    let mutable _Clicker = new UIButton()

    [<Outlet>]
    member this.Clicker 
           with get() = _Clicker
           and set value = _Clicker <- value
like image 77
robkuz Avatar answered Sep 21 '22 08:09

robkuz