Xamarin / Android: F # scoping - how can I see the namespace in another file?

Xamarin / Android: F # scoping - how can I see the namespace in another file?

I know this sounds very thorough, but I can't get it to work. I will illustrate an example:

I am launching a new solution, I select a new Android application F # and call it FSScopeTest1, giving me MainActivity.fs

namespace FSScopeTest1 open System open Android.Content open Android.OS open Android.Runtime open Android.Views open Adroid.Widget [<Activity (Label = "FSScopeTest1", MainLauncher = true)>] type MainActivity () = inherit Activity () let mutable count:int = 1 override this.OnCreate (bundle) = base.OnCreate (bundle) // Set our view from the "main" layout resource this.SetContentView (Resource_Layout.Main) // Get our button from the layout resource, and attach an event to it let button = this.FindViewById<Button>(Resource_Id.myButton) button.Click.Add (fun args -> button.Text <- sprintf "%d clicks" count count <- count + 1 ) 

Then I add the new F # source file, ScopeTestNS.fs

 namespace ScopeTestNS module ScopeTestMod = let astr = "some text" 

Then I add to MainActivity.fs, in the second line:

 open ScopeTestNS 

and change the lamba expression for button.Click.Add to read

  button.Click.Add (fun args -> // button.Text <- sprintf "%d clicks!" count // count <- count + 1 button.Text <- ScopeTestMod.astr ) 

now when i create the solution i get the error:

 The namespace or module "ScopeTestMod" is not defined. 

How to make my ScopeTestNS namespace visible in MainActivity.fs so that I can see any modules I define there?

Thanks a lot, Ross

+4
source share
1 answer

The f # compiler reads the source files in a specific order (as opposed to the C # compiler). You need to make sure that your project included the files in the correct order so that you can access the modules defined in other files.

EDIT based on comment from Onorio Catenacci:

Apparently, the F # binding used in Xamarin Studio does not currently support file reordering in the F # project (see this github issue https://github.com/fsharp/fsharpbinding/issues/135 ), however the files. fsproj are simple xml files that can be edited with a text editor to change the order in which the files are compiled.

EDIT EDIT

Apparently F # addin in xamarin studio now allows you to drag and drop files to reorder (thanks @ 7sharp9 for the update)

+5
source

Source: https://habr.com/ru/post/1486602/


All Articles