From 3a9c2dc10200760247e05cfb4d25fc552cdf359f Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 26 Nov 2024 00:42:57 +0300 Subject: [PATCH] Add VB.NET support #8 --- .../AvaloniaHotReloadExtensions.vb | 181 ++++++++++++++++++ .../HotAvalonia.Extensions.csproj | 1 + 2 files changed, 182 insertions(+) create mode 100644 src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.vb diff --git a/src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.vb b/src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.vb new file mode 100644 index 0000000..6dd72cb --- /dev/null +++ b/src/HotAvalonia.Extensions/AvaloniaHotReloadExtensions.vb @@ -0,0 +1,181 @@ +' +' This file has been automatically added to your project by the "HotAvalonia.Extensions" NuGet package +' (https://nuget.org/packages/HotAvalonia.Extensions). +' +' Please see https://github.com/Kir-Antipov/HotAvalonia for more information. +' + +#Region "License" +' MIT License +' +' Copyright (c) 2023-2024 Kir_Antipov +' +' Permission is hereby granted, free of charge, to any person obtaining a copy +' of this software and associated documentation files (the "Software"), to deal +' in the Software without restriction, including without limitation the rights +' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +' copies of the Software, and to permit persons to whom the Software is +' furnished to do so, subject to the following conditions: +' +' The above copyright notice and this permission notice shall be included in all +' copies or substantial portions of the Software. +' +' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +' SOFTWARE. +#End Region + +#Disable Warning + +Imports System +Imports System.Diagnostics +Imports System.Diagnostics.CodeAnalysis +Imports System.IO +Imports System.Reflection +Imports System.Runtime.CompilerServices +Imports Avalonia + +Namespace Global.HotAvalonia + ''' + ''' Indicates that the decorated method should be called whenever the associated Avalonia control is hot reloaded. + ''' + ''' + ''' This attribute is intended to be applied to parameterless instance methods of Avalonia controls. + ''' When the control is hot reloaded, the method marked with this attribute is executed. + ''' This can be used to refresh or update the control's state in response to hot reload events. + ''' + '''

+ ''' + ''' The method must meet the following requirements: + ''' + ''' It must be an instance method (i.e., not static). + ''' It must not have any parameters. + ''' + ''' + ''' Example usage: + ''' + ''' + ''' Private Sub Initialize() + ''' ' Code to initialize or refresh + ''' ' the control during hot reload. + ''' End Sub + ''' + '''
+ + + + Friend NotInheritable Class AvaloniaHotReloadAttribute + Inherits Attribute + End Class + + ''' + ''' Provides extension methods for enabling and disabling hot reload functionality for Avalonia applications. + ''' + + Friend Module AvaloniaHotReloadExtensions +#If ENABLE_XAML_HOT_RELOAD AndAlso Not DISABLE_XAML_HOT_RELOAD Then + ''' + ''' A mapping between Avalonia instances and their associated hot reload context. + ''' + Private ReadOnly s_apps As New ConditionalWeakTable(Of Application, IHotReloadContext) + + ''' + ''' Enables hot reload functionality for the given Avalonia application. + ''' + ''' The Avalonia application instance for which hot reload should be enabled. + ''' The file path of the application's main source file. Optional if the method called within the file of interest. + + + Public Sub EnableHotReload(ByVal app As Application, Optional ByVal appFilePath As String = Nothing) + If app Is Nothing Then + Throw New ArgumentNullException(NameOf(app)) + End If + + Dim context As IHotReloadContext = Nothing + If Not s_apps.TryGetValue(app, context) Then + If Not String.IsNullOrEmpty(appFilePath) AndAlso Not File.Exists(appFilePath) Then + Throw New FileNotFoundException("The corresponding XAML file could not be found.", appFilePath) + End If + + If Not String.IsNullOrEmpty(appFilePath) Then + AvaloniaProjectLocator.AddHint(app.GetType(), appFilePath) + End If + + Dim appDomainContext As IHotReloadContext = AvaloniaHotReloadContext.FromAppDomain() + Dim assetContext As IHotReloadContext = AvaloniaHotReloadContext.ForAssets() + context = HotReloadContext.Combine(appDomainContext, assetContext) + s_apps.Add(app, context) + End If + + context.EnableHotReload() + End Sub + + ''' + ''' Enables hot reload functionality for the given Avalonia application. + ''' + ''' The Avalonia application instance for which hot reload should be enabled. + ''' The callback function capable of resolving a project path for a given assembly. + + + Public Sub EnableHotReload(ByVal app As Application, ByVal projectPathResolver As Func(Of Assembly, String)) + AvaloniaProjectLocator.AddHint(projectPathResolver) + EnableHotReload(app, String.Empty) + End Sub + + ''' + ''' Disables hot reload functionality for the given Avalonia application. + ''' + ''' The Avalonia application instance for which hot reload should be disabled. + + + Public Sub DisableHotReload(ByVal app As Application) + If app Is Nothing Then + Throw New ArgumentNullException(NameOf(app)) + End If + + Dim context As IHotReloadContext = Nothing + If s_apps.TryGetValue(app, context) Then + context.DisableHotReload() + End If + End Sub +#Else + ''' + ''' Enables hot reload functionality for the given Avalonia application. + ''' + ''' The Avalonia application instance for which hot reload should be enabled. + ''' The file path of the application's main source file. Optional if the method called within the file of interest. + + + + Public Sub EnableHotReload(ByVal app As Application, Optional ByVal appFilePath As String = Nothing) + End Sub + + ''' + ''' Enables hot reload functionality for the given Avalonia application. + ''' + ''' The Avalonia application instance for which hot reload should be enabled. + ''' The callback function capable of resolving a project path for a given assembly. + + + + Public Sub EnableHotReload(ByVal app As Application, ByVal projectPathResolver As Func(Of Assembly, String)) + End Sub + + ''' + ''' Disables hot reload functionality for the given Avalonia application. + ''' + ''' The Avalonia application instance for which hot reload should be disabled. + + + + Public Sub DisableHotReload(ByVal app As Application) + End Sub +#End If + End Module +End Namespace + +#Enable Warning diff --git a/src/HotAvalonia.Extensions/HotAvalonia.Extensions.csproj b/src/HotAvalonia.Extensions/HotAvalonia.Extensions.csproj index 91348bb..8f66872 100644 --- a/src/HotAvalonia.Extensions/HotAvalonia.Extensions.csproj +++ b/src/HotAvalonia.Extensions/HotAvalonia.Extensions.csproj @@ -15,6 +15,7 @@ +