简体   繁体   中英

How can I use Xamarin's Storyboard and 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). 查看性能

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?

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 (eg attach the button named "Clicker")

在此输入图像描述在此输入图像描述

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

There's also an iOS type provider that's still considered experimental. You can try the template here https://github.com/nosami/ios-type-provider-template but this only works with the built in storyboard designer.

The myButton identifier here is provided by the type provider based on the controls in the storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM