Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for .NET Hot Reload #232

Merged
merged 23 commits into from
Oct 1, 2023
Merged

Conversation

TheCodeTraveler
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler commented Jun 28, 2023

Description

This PR implements MetadataUpdateHandler which is required for libraries wishing to support C# Hot Reload.

While C# Hot Reload does update the running/active C# code, it will not automatically refresh the visible .NET MAUI UI. To refresh the displayed UI, .NET MAUI devs are required to write additional code specific to their app that invalidates the current view.

For this initial implementation, developers will need to do two things:

  1. Implement ICommunityToolkitHotReloadHandler
  • In ICommunityToolkitHotReloadHandler.OnHotReload(), specify how to update the visible page
  • Shell example below
    class HotReloadHandler : ICommunityToolkitHotReloadHandler
    {
        public async void OnHotReload(IReadOnlyList<Type> types)
        {
            foreach (var type in types)
            {
              if (type.IsSubclassOf(typeof(Page)))
              {
                if (Application.Current?.MainPage is AppShell shell)
    	    {
                      await Application.Current.MainPage.Dispatcher.DispatchAsync(async () =>
                      {
                        await Shell.Current.GoToAsync(type.Name, false);
                        Shell.Current.Navigation.RemovePage(shell.CurrentPage); 
                      });
                }
    
                break;
              }
            }
        }
    }
  1. In MauiProgram.CreateMauiApp(), add your implementation of ICommunityToolkitHotReloadHandler:

    builder.Services.AddSingleton<ICommunityToolkitHotReloadHandler, HotReloadHandler>();

ScreenFlow

Linked Issues

PR Checklist

Additional information

While C# Hot Reload does update the running/active C# code, it will not refresh the .NET MAUI UI. To refresh the displayed UI, .NET MAUI devs are required to write additional code specific to their app that invalidates the

For this initial implementation of C# Hot Reload support, I recommend that we do not provide a default implementation of ICommunityToolkitHotReloadHandler because it is near impossible to account for every application architecture.

I have included in this PR (and I recommend including in the docs) a sample implementation of ICommunityToolkitHotReloadHandler.

In future releases, once we are comfortable with a stable implementation of ICommunityToolkitHotReloadHandler, we can wire it up automatically for the users in UseMauiCommunityToolkitMarkup().

Copy link
Member

@pictos pictos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the interface idea and let devs opt-in to use the feature. I didn't have my PC with me to test everything, so the comment about the debug flag is a guess

@VincentH-Net
Copy link

I like that the ICommunityToolkitHotReloadHandler interface allows devs to inspect the updated types via the IReadOnlyList<Type> types parameter. This allows devs to control the rebuild (+rebuild order) of instances that a page depends upon but that are not created by the page, e.g. a style that is created from C#.

To illustrate, here is a reload handler for WPF in C# Markup 2 - the same logic applies.

I also applaud that no standard handler implementation is shipped; I expect that for non-trivial Apps this logic will need to be customized by the app developer.

Imo it would be fine to keep the handler at a simple implementation in docs, as the PR description proposes, or perhaps as part of a (dotnet new?) template

@VincentH-Net
Copy link

await Shell.Current.GoToAsync(type.Name, false);

In the PR description, the Shell example, where is the type variable defined that is referenced in above line?

@TheCodeTraveler
Copy link
Collaborator Author

TheCodeTraveler commented Jun 28, 2023

Thanks Vincent!

where is the type variable defined that is referenced in above line

Whoops - that was just a lazy copy/paste edit from the source code into the PR description. Fixed ✅

@TheCodeTraveler TheCodeTraveler added the needs discussion The team will aim to discuss this at the next monthly standup label Jul 6, 2023
@TheCodeTraveler TheCodeTraveler marked this pull request as ready for review July 6, 2023 20:23
@TheCodeTraveler TheCodeTraveler removed the needs discussion The team will aim to discuss this at the next monthly standup label Jul 6, 2023
@TheCodeTraveler TheCodeTraveler added do not merge Do not merge this PR pending documentation This feature requires documentation labels Jul 6, 2023
@ghost ghost added stale The author has not responded in over 30 days help wanted This proposal has been approved and is ready to be implemented labels Aug 7, 2023
@TheCodeTraveler TheCodeTraveler removed help wanted This proposal has been approved and is ready to be implemented stale The author has not responded in over 30 days labels Sep 26, 2023
@TheCodeTraveler TheCodeTraveler removed do not merge Do not merge this PR pending documentation This feature requires documentation labels Oct 1, 2023
@TheCodeTraveler TheCodeTraveler enabled auto-merge (squash) October 1, 2023 21:34
@TheCodeTraveler TheCodeTraveler changed the title Add Support for C# Hot Reload Add Support for .NET Hot Reload Oct 1, 2023
@TheCodeTraveler TheCodeTraveler merged commit d1825b6 into main Oct 1, 2023
@TheCodeTraveler TheCodeTraveler deleted the Add-C#-HotReload-Support branch October 1, 2023 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants