2

I would like to use F# to develop windows 8.1 universal app. So I have created F# portable library. But unfortunately I can not open Windows.Storage namespace on it.

Then I have created C# Windows Apps Class Library where I have implemented the function I was needed:

 public static async Task<string> ReadFileToString(string filename)
 {
        StorageFile file = await StorageFile.GetFileFromPathAsync(filename);
        string fileContent =  "";
        fileContent = await FileIO.ReadTextAsync(file);

        return fileContent;
 }

Then I want to Add Reference in my F# Portable Library to this C# Library but get the error message, that

"it is impossible because the project is designed for platform (.NETCore) which different from current (.NETPortable)"

It is possible to use F# to develop windows 8 universal app ?

ceth
  • 44,198
  • 62
  • 180
  • 289
  • If you make your C# project a portable library, then it should work. Doesn't have anything to do with F#. A portable library can't reference a library that targets a specific platform. – CoderDennis Feb 26 '15 at 18:22
  • Yes, but I cannot use Windows.Storage namespace (and StorageFile) in this library. So I am looking to possibility read files in universal app written in F# – ceth Feb 26 '15 at 18:26
  • Check the profile you used to create the F# library. There are different types of portable libraries. – N_A Feb 26 '15 at 18:42
  • I have only one option '.Net Portable Subset (Net Framework 4.5, Windows 8)' if I understood you correctly. But look like it is possible. For example I have found this code (https://stackoverflow.com/questions/23312965/awaiting-an-iasyncoperation-in-f) where Andy B uses StorageFile in F# – ceth Feb 26 '15 at 18:47
  • I have checked both profiles (7 and 47). None of them cannot open Windows.StorageFile – ceth Feb 26 '15 at 19:02
  • I have found PCLStorage (https://github.com/dsplaisted/PCLStorage), which solves my problem – ceth Feb 26 '15 at 19:20

1 Answers1

1

Here is the solution I have found.

  • Install PCLStorage using NuGet to F# portable library and Windows 8.1 Store App (!)
  • Set "Copy to Local" = False in F# library (!)

Here is code of f# library:

namespace PortableLibrary1

open PCLStorage

type MyClass() =
    member this.Do (filename : string) (text : string) = 
        let folder = FileSystem.Current.LocalStorage       
        async {
            let! result = Async.AwaitTask(folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting))
            result.WriteAllTextAsync(text) |> ignore        
        } |> Async.RunSynchronously

And here is a C# Store App code:

 private void Button_Click(object sender, RoutedEventArgs e)
 {
    MyClass cls = new MyClass();             
    cls.Do("uuu.ii", "message of me");
 }
ildjarn
  • 62,044
  • 9
  • 127
  • 211
ceth
  • 44,198
  • 62
  • 180
  • 289