Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can FsXaml be used in an F# interpreted script?

Tags:

xaml

f#

fsxaml

I just converted one of the FsXaml demo programs to an interpreted F# script so I could experiment with it and learn. It wouldn't run, and the interpreter gave me the following error message:

System.NotSupportedException: The invoked member is not supported in a dynamic assembly.
at System.Reflection.Emit.InternalAssemblyBuilder.GetManifestResourceStream(String name)
at FsXaml.InjectXaml.from(String file, Object root)
at FsXaml.App.InitializeComponent() at FsXaml.App..ctor()
at FSI_0002.main[a](a argv) in C:\Users\bobmc\OneDrive\FSharp\Learning\WPFExamples\FsXaml\demos\WpfSimpleMvvmApplication\WPFApp.fsx:line 104
at .$FSI_0002.main@() in C:\Users\bobmc\OneDrive\FSharp\Learning\WPFExamples\FsXaml\demos\WpfSimpleMvvmApplication\WPFApp.fsx:line 109

Can I use the F# interpreter with FsXaml? Thanks to all for your help.

like image 227
Bob McCrory Avatar asked Jan 30 '17 22:01

Bob McCrory


1 Answers

Unfortunately, WPF and scripts don't play well together.

The exception occurs within the WPF runtime itself - FsXaml.InjectXaml is using a XamlObjectWriter to populate the type with the contents from the XAML file. This type doesn't work if you're using a dynamic assembly (like FSI), which unfortunately means that FsXaml will likely never be able to work from FSI.

That being said, even if there was a way around this, it'd be of very limited use. WPF also has restrictions that make it not play well with a scripting scenario, such as the "only one application can ever be created within a given AppDomain" restriction. That one makes it so closing the "main" (first) window makes it so you can never open another one. As such, I haven't prioritized trying to make this work in FSI.

I'd be happy to accept contributions if somebody has an idea of how to make FsXaml play more nicely within the context of FSI, but at this point, I don't see a good solution for that usage scenario.

Edit: FsXaml 3.1.6 now includes functionality to make this a lot easier. It works well, provided you don't close the main window, or you use dialogs. There is a demo application/script illustrating this.

like image 186
Reed Copsey Avatar answered Oct 18 '22 21:10

Reed Copsey