3
votes

How do you create an F# PCL that can be called from a C# Xamarin Android project?

Something simple, like this in a PCL:

namespace FSharpPCL2

type Class1() = 
    member this.X (s: string option) = 
      s
      |> Option.map (fun t -> t + "stuff")

Try calling it from a C# Xamarin Android project, and you'll get a compiler error something like this:

MainActivity.cs(26,41): error CS0012: The type Microsoft.FSharp.Core.FSharpOption1' is defined in an assembly that is not referenced. Consider adding a reference to assembly `FSharp.Core, Version=3.78.3.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

But it's not clear to me how you actually manage to include the right FSharp.Core in the C# project.

1

1 Answers

2
votes

You should take a look at the FSharp.Core guidelines which explains the options you have in these scenarios.

In your case you can add a package reference to the FSharp.Core nuget package in your C#-xamarin project. FSharp.Core, Version=3.78.3.1 is F# 3.1/Profile 78. You can make sure that the version you want is referenced by viewing the version/path in References > FSharp.Core > Properties. If not you can edit the csproj and change the path to the appropriate version.

An alternative is to add a reference directly to FSharp.Core that ships with Visual/Xamarin Studio in either %PROGRAMFILES(X86)%\Reference Assemblies\Microsoft\FSharp\.NETCore\3.78.3.1\FSharp.Core.dll or /Library/Frameworks/Mono.framework/Versions/4.4.1/lib/mono/Reference Assemblies/Microsoft/FSharp/.NETCore/3.78.3.1/FSharp.Core.dll