Xamarin.Forms and F#?

TedParneforsTedParnefors SEUniversity

Hi!

How can I use Xamarin.Forms with F#?

I'm getting:
The type 'Object' is required here and is unavailable. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0' etc...

Is this because Xamarin.Forms is a PCL-library and it's still not possible to create a F# PCL project in Xamarin Studio?

Posts

  • CraigDunnCraigDunn USXamarin Team Xamurai
    edited August 2014

    This is the only resource I know of that talks about Xamarin.Forms with F# - Xamarin 3 F# Awesomeness - although Google tells me there are a couple of Xamarin.Forms F# samples here.

    What IDE, operating system, Xamarin, F# versions are you using; and also which template (PCL or Shared Project)?

  • nbevansnbevans USMember ✭✭✭

    I'm happily using F# for my PCLs (95% of the code).

    Since Visual Studio doesn't yet support Xamarin F# projects (WHY!?) I have, for now, elected to do the platform-specific code like MainActivity and custom renderers in C#. But that is a very small amount of code that, one day, I can port across to a proper Xamarin F# project type in probably half a days effort.

    F# and Xamarin.Forms is sheer bliss. It really helps me cut through all the boilerplate crap and implement real features of business value very quickly. If I were stuck using C# I'd be wallowing around in trying to build some perfect architecture, MVVM this, stereotypical legacy-gang-of-four pattern that.

  • TedParneforsTedParnefors SEUniversity

    Craig: Yes, I had read Dave Thomas' article before, and he's alluding to a future blog post showing how to build a PCL backed F# app. But no details as of yet. I also tried building the samples you linked to, but I was unable to build any of them using Xamarin Studio 5.3. Are those compiling successfully for you?

    Nathan: What did you do in order to get XS to treat the F# project as a PCL project? Manually edited the project file? If that's the case, what modifications are necessary? Or is there a F# PCL project template downloadable somewhere?

    Thanks!

    I'm trying to get this to work on OS X, Xamarin Studio 5.3.

  • FabioGFabioG ITBeta, University ✭✭✭

    @NeoDarque, I'm having the same problem. I'm on OS X and XS 5.3 too + Mono 3.8.
    This thread seems to bring some hope but no good news so far. I was trying to use the following as a source for my PCL but I did succeed:

    https://github.com/alexy/EffinPickle/

    I've removed all the MVVMCross stuff; I can open the project, change the PCL profile in XS but when I try to compile it I get this error:

    FSC: error FS0193: The module/namespace 'System.Net' from compilation unit 'System' did not contain the namespace, module or type 'WebClient'

    Could someone shed some light on this? I've attached the solution.

  • FabioGFabioG ITBeta, University ✭✭✭

    Sorry I meant, "I did not succeed".

  • TedParneforsTedParnefors SEUniversity
    edited August 2014

    @FabioG I tried creating an Xamarin.Forms project in VisualStudio on a PC instead, and added a PCL F# project there referencing Xamarin.Forms, this compiles just fine. So I moved this project over to XS 5.3 on OS X where it fails to compile with the same odd error message you received:

    **Error FS0193: The module/namespace 'System.Net' from compilation unit 'System' did not contain the namespace, module or type 'WebClient' (FS0193)
    **

    Looks more like a bug somewhere on the XamarinStudio/F# compiler integration.

  • FabioGFabioG ITBeta, University ✭✭✭

    @NeoDarque, I don't know if it's a bug or may be it's that Visual F# and F# Open Source Edition are not aligned so the Core assemblies are different. @7sharp9‌, could you please give us a hint about the problem? Can we expect F# PCLs working on the Mac in the future? I recall something that was fixed on CodePlex and needs to be ported to the GitHub repo. Am I right?

  • CraigDunnCraigDunn USXamarin Team Xamurai

    @LarryOBrien‌ has made some progress which I'm pretty sure he'll share when he gets a chance :)

  • LarryOBrienLarryOBrien USXamarin Team Xamurai
  • TedParneforsTedParnefors SEUniversity

    @LarryOBrien‌: Thanks for the walkthrough, much appreciated! It is very close to black magic though... :)

  • DaveThomasDaveThomas GBMember, Xamarin Team Xamurai
    edited August 2014

    Sorry Ive not had time to do that second post.

    Thankfully @LarryOBrien‌ kindly wrote up the steps I gave to get it running.

    Its not so much black magic its just a work around to an known issue with implicit PCL references not being added automatically.

    @FabioG‌ Im waiting patiently for F#3.2 to be merged from codeplex to github, I think the issue was more to do with the VS than F# PCL in general. Once the issues are fixed it will allow nice F# templates to be added to Xamarin Studio.

  • FabioGFabioG ITBeta, University ✭✭✭

    That's cool! Thanks guys!

  • AndyDentAndyDent AUMember ✭✭

    Are you having PCL-related problems? I have some working samples going but haven't had time to polish and put on GitHub, will put a VERY rough version up there this weekend. Ping me directly if I forget.

    I'm deep in Apple Watch programming at present and my head's back in Swift.

    Here's the code

    namespace FormsFS_iOS
    
    
    open System
    open UIKit
    open Foundation
    open Xamarin.Forms
    
    type App() =  
     static member GetMainPage =
      let sl = new StackLayout (
                Spacing = 20.0,
                Padding = Thickness(10.0),
                VerticalOptions = LayoutOptions.FillAndExpand
        )
      let slc : View[] = [|
        new BoxView ()
        new Entry (Placeholder = "Username") 
        new Entry (Placeholder = "Password", IsPassword = true) 
        new Button (Text = "Login", TextColor = Color.White, BackgroundColor = Color.FromHex "77D065") 
      |]
      slc |> Seq.iter (fun view -> sl.Children.Add(view))
      new ContentPage( Content = sl )
    
    
    [<Register("AppDelegate")>]
    type AppDelegate() =
     inherit UIApplicationDelegate()
    
    // member val Window = null with get, set
     override val Window = null with get, set
    
    // This method is invoked when the application is ready to run.
     override this.FinishedLaunching(app, options) =
       this.Window <- new UIWindow(UIScreen.MainScreen.Bounds)
       Forms.Init()
       this.Window.RootViewController <- App.GetMainPage.CreateViewController()
       this.Window.MakeKeyAndVisible()
       true
    
    module Main =
       [<EntryPoint>]
       let main args =
         UIApplication.Main(args, null, "AppDelegate")
         0
    
Sign In or Register to comment.