Last active
April 17, 2024 00:55
-
-
Save Fxplorer/4807fc0aeeee02ccfc2e9ad4dd3f2266 to your computer and use it in GitHub Desktop.
Avalonia from F# script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if INTERACTIVE | |
#r "nuget: Avalonia" | |
#r "nuget: Avalonia.Desktop" | |
#r "nuget: Avalonia.Themes.Simple" | |
#endif | |
open Avalonia | |
open Avalonia.Controls | |
(* | |
This version works. I realized that by binding the view1 was too early so I moved it to inside the the OnFrameworkInitializationCompleted method. | |
*) | |
type App() = | |
inherit Application() | |
override this.Initialize() = | |
this.Styles.Add ( Avalonia.Themes.Simple.SimpleTheme() ) | |
override this.OnFrameworkInitializationCompleted() = | |
printfn "OnFrameworkInitializationCompleted...Start" | |
match this.ApplicationLifetime with | |
| :? Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime as desktop -> | |
let view1 = | |
let window = Window(Title = "Hello World") | |
let textBlock = TextBlock(Text = "Hello World from Avalonia F#!") | |
window.Content <- textBlock | |
window | |
desktop.MainWindow <- view1 | |
printfn "this.ApplicationLifetime matched IClassicDesktopStyleApplicationLifetime" | |
| _ -> () | |
printfn "OnFrameworkInitializationCompleted...End" | |
let app = | |
AppBuilder.Configure<App>() | |
.UsePlatformDetect() | |
app.StartWithClassicDesktopLifetime([||]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if INTERACTIVE | |
#r "nuget: Avalonia" | |
#r "nuget: Avalonia.Desktop" | |
#r "nuget: Avalonia.Themes.Simple" | |
#endif | |
open Avalonia | |
open Avalonia.Controls | |
(* | |
This script will fail with the following error: | |
$ dotnet fsi /home/linux/CODE/XploringAvalonia/XPLORATION/f4.fsx | |
System.InvalidOperationException: Unable to locate 'Avalonia.Platform.IWindowingPlatform'. | |
at Avalonia.LocatorExtensions.GetRequiredService[T](IAvaloniaDependencyResolver resolver) | |
at Avalonia.Controls.Platform.PlatformManager.CreateWindow() | |
at Avalonia.Controls.Window..ctor() | |
at <StartupCode$FSI_0002>.$FSI_0002.main@() in /home/linux/CODE/XploringAvalonia/XPLORATION/f4.fsx:line 19 | |
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) | |
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) | |
Stopped due to error | |
After some more research and trials, I found that the issue is due to the fact that the Window object is created before the Avalonia application is initialized. | |
To fix this, I moved the creation of the view inside the App class. | |
See https://gist.github.com/Fxplorer/4807fc0aeeee02ccfc2e9ad4dd3f2266#file-avalonia_fsharpscript_first_working_try-fsx | |
*) | |
let view1 = | |
let window = Window(Title = "Hello World") | |
let textBlock = TextBlock(Text = "Hello World from Avalonia F#!") | |
window.Content <- textBlock | |
window | |
type App() = | |
inherit Application() | |
override this.Initialize() = | |
this.Styles.Add ( Avalonia.Themes.Simple.SimpleTheme() ) | |
override this.OnFrameworkInitializationCompleted() = | |
printfn "OnFrameworkInitializationCompleted...Start" | |
match this.ApplicationLifetime with | |
| :? Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime as desktop -> | |
desktop.MainWindow <- view1 | |
printfn "this.ApplicationLifetime matched IClassicDesktopStyleApplicationLifetime" | |
| _ -> () | |
printfn "OnFrameworkInitializationCompleted...End" | |
let app = | |
AppBuilder.Configure<App>() | |
.UsePlatformDetect() | |
app.StartWithClassicDesktopLifetime(Array.empty<string>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if INTERACTIVE | |
#r "nuget: Avalonia" | |
#r "nuget: Avalonia.Desktop" | |
#r "nuget: Avalonia.Themes.Simple" | |
#endif | |
open Avalonia | |
open Avalonia.Controls | |
(* | |
Changing view1 to a function solves the error. It allows creating the expected view early but does not accually run the function until called. | |
*) | |
let view1 () = | |
let window = Window(Title = "Hello World") | |
let textBlock = TextBlock(Text = "Hello World from Avalonia F#!") | |
window.Content <- textBlock | |
window | |
type App() = | |
inherit Application() | |
override this.Initialize() = | |
this.Styles.Add ( Avalonia.Themes.Simple.SimpleTheme() ) | |
override this.OnFrameworkInitializationCompleted() = | |
match this.ApplicationLifetime with | |
| :? Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime as desktop -> | |
desktop.MainWindow <- view1 () | |
printfn "Avalonia app running..." | |
| _ -> () | |
let app = | |
AppBuilder.Configure<App>() | |
.UsePlatformDetect() | |
app.StartWithClassicDesktopLifetime([||]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment