FS2024 Static linking error when PCL project use by TypeProvider

一个人想着一个人 提交于 2020-01-14 11:45:26

问题


It's trying to make a TypeProvider for Xamarin.Forms, but has been plagued by FS2024 error.

  • Parse own library from the XAML of Xamarin.Forms
  • Assign x:Name to Propertis

`F#

type MainPage = Moonmile.XamarinFormsTypeProvider.XAML<"MainPage.xaml">
// made btn1 and text1 propertis

type MainPageEx(target:MainPage) =
    let mutable count = 0
    do
        // When set event to btn.Clicked, happen FS2024 error.
        // If this event is comment out, it success build.
        target.btn1.Clicked.Add( fun e ->
            count <- count + 1
            target.btn1.Text <- "Clicked " + count.ToString())
    // Property is success
    member this.CurrentPage
        with get() = target.CurrentPage

When you are referring to a property, build & operation you can normally. But the internal class of Xamarin.Forms like Button.Clicked, If you try to access to, it is the build error.

Sample code for error https://github.com/moonmile/SimpleEventTypeProvider

Making code for XamarinFormsTypeProvider github.com/moonmile/XamarinFormsTypeProvider

Maybe, I suspect inconsistencies and is happening in the part of the generation of a Native TypeProvider and Xamrin.Forms.Core a PCL.

F# Compiler for F# 3.1 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License my error!!!
isMscorlib: true
name: "System.Runtime"
PrimaryAssembly.DotNetCore.Name: "System.Runtime"
PrimaryAssembly.Mscorlib.Name: "mscorlib"

parameter error FS2024: Static linking may not use assembly that targets different profile.

It's to operate the property they work properly, and to MVVM perhaps. Butt I am trying to implement a way to be assigned to Button.Clicked events as shown in the codebehide-like buildings if possible.

Would there workaround or what?

In the case of XAML in WPF, How can such seems to work well. github.com/fsprojects/FsXaml


回答1:


This answer isn't guaranteed to be correct, but it should help at least point you in the right direction.

The first thing to do is to make sure that you have installed the latest Visual F# Tools Build, as this adds the FSharp.Core that is compatible with the PCL profiles (You can find it here: (https://visualfsharp.codeplex.com/). Once that is installed, you will want to reference either the Profile78, or Profile259 FSharp.Core.dll (On my machine, these are found at: "C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp.NETPortable\2.3.5.0", and "C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp.NETPortable\2.3.5.1" respectively).

Once you have that installed, the next thing to do is make sure that your PCL projects have the following in their project files (This tells MSBuild / xBuild that the projects are PCL libraries, and that they are F# Projects):

<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{F2A71F9B-5D33-465A-A702-920D77279786}</ProjectTypeGuids>

Once that is done, you will need to select either Profile78, or Profile259 (I would recommend 78, as the current Xamarin.Forms nuget package doesn't support 259).

Once that is done, then you should be able to build and run and it should get rid of the error.




回答2:


Thank you for my question.

Meybe, When it build TypeProvider, F# compiler use classes in mscorlib. When it resolve the type of btn1.Clicked event, the F# comiler use type in System.Runtime. I think for that, and it can not be resolved at build time

Try, if you attach the Clicked Event using reflection, it has moved successfully on Android build through. I seem, if it I use only shard classes in mscorlib and System.Rutime, I can build no FS2024 error.

type MainPage = Moonmile.XamarinFormsTypeProvider.XAML<"MainPage.xaml">

type MainPageEx() as this =
    inherit BindObject<MainPage>(new MainPage())

    // Add handlder by reflection
    let AddHandler(target:obj, eventName:string, eventMethod: obj*obj -> unit ) =
        let hdr = Action<obj,obj>( fun s e  -> eventMethod(s,e))

        let ei = target.GetType().GetRuntimeEvent(eventName)
        let dt = ei.AddMethod.GetParameters().[0].ParameterType
        let handler = new Action<obj,obj>(fun s e -> hdr.Invoke( s, new EventArgs() ))
        let handlerInvoke = handler.GetType().GetRuntimeMethod("Invoke", [|typeof<obj>; typeof<Type[]>|])
        let dele = handlerInvoke.CreateDelegate( dt, handler )
        let add = new Func<Delegate, EventRegistrationToken> ( fun t -> 
            let para = ei.AddMethod.GetParameters()
            let ret  = ei.AddMethod.Invoke( target, [|t|])
            if ret <> null then
                ret :?> EventRegistrationToken
            else
                new EventRegistrationToken()
            )
        let remove = new Action<EventRegistrationToken>( fun t -> ei.RemoveMethod.Invoke(target, [|t|]) |> ignore )
        // WindowsRuntimeMarshal.AddEventHandler<Delegate>(add, remove, dele)
        add.Invoke( dele ) |> ignore
        ()

        let mutable count = 0
        do
        (*  // build error
            target.btn1.Clicked.Add( fun e ->
                count <- count + 1
                target.btn1.Text <- "Clicked " + count.ToString())
        *)
            // add handler by reflection
            AddHandler( base.Target.btn1, "Clicked", this.ButtonClick ) 
            ()

        member this.CurrentPage
            with get() = this.Target.CurrentPage

        member this.ButtonClick(s,e) =
            count <- count + 1
            base.Target.text1.Text <- "clicked " + count.ToString()


来源:https://stackoverflow.com/questions/25175031/fs2024-static-linking-error-when-pcl-project-use-by-typeprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!