Skip to content

Commit

Permalink
Merge pull request #26 from wieslawsoltes/TopLevelAttachedToVisualLog…
Browse files Browse the repository at this point in the history
…icalTree

Top level attached to visual logical tree
  • Loading branch information
wieslawsoltes authored Jan 3, 2025
2 parents 0addf1c + be2b19f commit ce8cd3c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
10 changes: 8 additions & 2 deletions samples/BehaviorsTestApplication/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
xmlns:vm="using:BehaviorsTestApplication.ViewModels"
xmlns:views="using:BehaviorsTestApplication.Views"
Title="XamlBehaviors Test Application" Width="1000" Height="700"
x:DataType="vm:MainWindowViewModel"
x:CompileBindings="False">
x:DataType="vm:MainWindowViewModel">
<Interaction.Behaviors>
<!-- TopLevel Logical tree special case -->
<!--
<EventTriggerBehavior EventName="Opened">
<InvokeCommandAction Command="{Binding InitializeCommand, Mode=OneTime}" />
</EventTriggerBehavior>
-->
<!-- TopLevel attached to visual tree special case -->
<RoutedEventTriggerBehavior RoutedEvent="{x:Static Control.LoadedEvent}">
<InvokeCommandAction Command="{Binding InitializeCommand, Mode=OneTime}" />
</RoutedEventTriggerBehavior>
</Interaction.Behaviors>
<views:MainView />
</Window>
39 changes: 39 additions & 0 deletions src/Avalonia.Xaml.Interactivity/Interaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ private static void SetVisualTreeEventHandlersFromGetter(AvaloniaObject obj)
control.Loaded += Control_LoadedFromGetter;
control.Unloaded -= Control_UnloadedFromGetter;
control.Unloaded += Control_UnloadedFromGetter;

if (obj is TopLevel topLevel)
{
topLevel.Opened -= TopLevel_OpenedFromChangedEvent;

Check warning on line 155 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 155 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 155 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 155 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 155 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 155 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
topLevel.Opened -= TopLevel_OpenedFromGetter;

Check warning on line 156 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 156 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 156 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 156 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 156 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 156 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
topLevel.Opened += TopLevel_OpenedFromGetter;

Check warning on line 157 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 157 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 157 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 157 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 157 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 157 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
}
}

private static void SetVisualTreeEventHandlersFromChangedEvent(AvaloniaObject obj)
Expand Down Expand Up @@ -184,6 +191,13 @@ private static void SetVisualTreeEventHandlersFromChangedEvent(AvaloniaObject ob
control.Loaded += Control_LoadedFromChangedEvent;
control.Unloaded -= Control_UnloadedFromChangedEvent;
control.Unloaded += Control_UnloadedFromChangedEvent;

if (obj is TopLevel topLevel)
{
topLevel.Opened -= TopLevel_OpenedFromGetter;

Check warning on line 197 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 197 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 197 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 197 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 197 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 197 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromGetter(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
topLevel.Opened -= TopLevel_OpenedFromChangedEvent;

Check warning on line 198 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 198 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 198 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
topLevel.Opened += TopLevel_OpenedFromChangedEvent;

Check warning on line 199 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 199 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).

Check warning on line 199 in src/Avalonia.Xaml.Interactivity/Interaction.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

Nullability of reference types in type of parameter 'sender' of 'void Interaction.TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
}
}

// AttachedToVisualTree / DetachedFromVisualTree
Expand Down Expand Up @@ -313,4 +327,29 @@ private static void Control_UnloadedFromChangedEvent(object? sender, RoutedEvent

GetBehaviors(d).Unloaded();
}

// TopLevel Opened

private static void TopLevel_OpenedFromGetter(object sender, EventArgs e)
{
if (sender is not AvaloniaObject d)
{
return;
}

GetBehaviors(d).Attach(d);
GetBehaviors(d).AttachedToVisualTree();
GetBehaviors(d).AttachedToLogicalTree();
}

private static void TopLevel_OpenedFromChangedEvent(object sender, EventArgs e)
{
if (sender is not AvaloniaObject d)
{
return;
}

GetBehaviors(d).AttachedToVisualTree();
GetBehaviors(d).AttachedToLogicalTree();
}
}

0 comments on commit ce8cd3c

Please sign in to comment.