-
I'm creating bindings for a component called NavigationView.onSelectedItemChanged (
fun x -> (match x with
| :? NavigationViewItem as y -> y.Content :?> string
| null -> ""
| _ -> "") |> item.Set
) and the function gets called with an instance of My code is below. Here's my file App.fs: namespace AvaloniaApplication2
open Avalonia
open Avalonia.Controls.ApplicationLifetimes
open Avalonia.FuncUI.Hosts
open Avalonia.Controls
open Avalonia.FuncUI
open Avalonia.FuncUI.DSL
open FluentAvalonia.Styling
open FluentAvalonia.UI.Controls
open Panels.NavigationView
module Main =
let view =
Component (fun ctx ->
let item = ctx.useState ""
StackPanel.create [
StackPanel.children [
NavigationView.create [
NavigationView.menuItems [
NavigationViewItem.create "Test 1"
NavigationViewItem.create "Kalle Anka"
NavigationViewItem.create "Hej"
]
NavigationView.onSelectedItemChanged (
fun x -> (match x with
| :? NavigationViewItem as y -> y.Content :?> string
| null -> ""
| _ -> "") |> item.Set
)
]
Grid.create [
Grid.children [
TextBlock.create [
TextBlock.text (string item.Current)
]
]
]
]
]
)
type MainWindow() =
inherit HostWindow()
do
base.Title <- "Counter Example"
base.Content <- Main.view
type App() =
inherit Application()
override this.Initialize() =
this.Styles.Add (FluentAvaloniaTheme())
override this.OnFrameworkInitializationCompleted() =
match this.ApplicationLifetime with
| :? IClassicDesktopStyleApplicationLifetime as desktopLifetime ->
desktopLifetime.MainWindow <- MainWindow()
| _ -> ()
base.OnFrameworkInitializationCompleted() Here's my file NavigationView.fs: namespace Panels.NavigationView
open System.Collections.Generic
open Avalonia.FuncUI.Builder
open Avalonia.FuncUI.DSL
open Avalonia.FuncUI.Types
[<AutoOpen>]
module NavigationViewItem =
open FluentAvalonia.UI.Controls
type NavigationViewItem with
static member create(content: string) =
let a = NavigationViewItem()
a.Content <- content
a
[<AutoOpen>]
module NavigationView =
open FluentAvalonia.UI.Controls
let create (attrs: IAttr<NavigationView> list): IView<NavigationView> =
ViewBuilder.Create<NavigationView>(attrs)
type NavigationView with
static member menuItems (value: obj list) : IAttr<NavigationView> =
AttrBuilder<NavigationView>.CreateProperty(NavigationView.MenuItemsSourceProperty, value |> List, ValueNone)
static member selectedItem<'t when 't :> NavigationView>(item: obj) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<obj>(NavigationView.SelectedItemProperty, item, ValueNone)
static member onSelectedItemChanged<'t when 't :> NavigationView>(func: obj -> unit, ?subPatchOptions) =
AttrBuilder<'t>.CreateSubscription<obj>(NavigationView.SelectedItemProperty, func, ?subPatchOptions = subPatchOptions) Can you tell if I'm doing something wrong? I don't expect |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hey @matachi The problem is that:
There are multiple ways of fixing / avoiding that issue.
|
Beta Was this translation helpful? Give feedback.
First, you should prefer correct bindings over resorting to the init function.
I highly recommend using the existing bindings as a reference.
Some controls (mostly 3rd party ones) require certain methods to be called on initialisation. That does not work well with bare components and is where the init function is really useful.
The init function gives you access to the actual control instance when the underlaying control is initially created.