Adding Razor cshtml view runtime re-compilation to a ASP.NET Core 5.0 app after creating it

I recently came across an interesting issue where after starting a new ASP.NET Core 5.0 .NET 5 project using the “ASP.NET Core Web App (Model-View-Controller)” template did not include the ability to update .cshtml Razor files without recompiling and restarting the whole app. There is a checkbox to “Enable Razor Runtime Compilation” during project setup but it’s easy to miss and tricky to add afterwards if you don’t know what you are looking for.

First, you need to head to the Nuget Package explorer by right clicking on the Dependencies node of your project tree selecting “Manage NuGet packages…”. Search for and install Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation. Install it and it’s dependencies into your project.

Then head to Startup.cs. Find the line within ConfigureServices() that says “services.AddControllersWithViews()” and add “.AddRazorRuntimeCompilation()”. You won’t be able to add this line without first installing the previous NuGet package.

Now run your app. You will see that changing the contents of Razor .cshtml files and refreshing the page will cause the view to be updated with your new content, without having to stop and recompile.

YouTube demonstration video

Razor Runtime Compilation - Reload Razor view in .NET Core 5.0 without recompiling
Watch this video on YouTube.

Handling and intercepting Back button Navigation in Xamarin Forms Shell

I’ve recently ended up needing to ask if the user really wants to navigate away from a page in my Xamarin app, Net Writer. Essentially whilst a post is being edited I don’t want the user to accidentally lose their progress, necessitating the need to inject a “Are you sure?” or “Confirm exit” prompt when the user presses either the Android hardware or OS level back button or the back button on the navigation bar provided by the Xamarin Forms Shell.

I found a blog post by Bohdan Benetskyi from March 2020, but it was geared for MvvmCross and not Xamarin Forms Shell. Here is how to adapt it.

Introducing IBackButtonHandler to your Page

If you have existing pages you can add an interface to them and implement it, for example here is a page from my app with IBackButtonHandler added to the class:

  [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class PostListSummaryPage : ReactiveContentPage<PostSummaryListPageViewModel>, IBackButtonHandler
    {

This can then be implemented however you like, returning true if the back navigation should be aborted, for example:

public async Task<bool> HandleBackButton()
{
    if (await ShouldNotGoBack())
    {
        return true;
    }

    return false;
}

public async Task<bool> ShouldNotGoBack()
{
    var response = await this.DisplayActionSheet("Would you like to discard any unsaved changes to this post?", "No", "Yes, go back");

    return response == "No";
}

Once that is done next up is wiring up the back buttons.

Handling the Android OS back button

To handle the OS level back button you’ll need to add the following to MainActivity.cs:

public async override void OnBackPressed()
{
    var backButtonHandler = Shell.Current.CurrentPage as IBackButtonHandler;

    if (backButtonHandler == null)
    {
        base.OnBackPressed();
        return;
    }

    var backButtonHandled = await backButtonHandler.HandleBackButton();
    if (!backButtonHandled)
    {
        base.OnBackPressed();
    }
}

Lets step through this. First up, it attempts to cast the current Shell page as IBackButtonHandler. This means that only Pages that implement this interface will have any change of behaviour here. If the interface is not implemented, the base implementation of OnBackPressed() is called. If it is implemented, the HandleBackButton method is evaluated. If it returns true, the back navigation is cancelled. If false, the base implementation of OnBackPressed() is called, continuing the back navigation.

Handling the Shell back button

This is the button on the top left that shows when you are more than one level deep in the navigation stack. Handling this is less clean as it requires adding a call to Shell.SetBackButtonBehaviour in the constructor of each page that requires this. For example, add the following to the constructor of your page, after InitializeComponent():

Shell.SetBackButtonBehavior(this, new BackButtonBehavior()
{
    Command = new Command(async () => {

        var backButtonHandled = await this.HandleBackButton();
        if (!backButtonHandled)
        {
            await Navigation.PopAsync();
        }

    })
});

After that, you’l get a nice prompt pop up whenever you hit the OS back button or the shell navigation back button, but only on pages that implement IBackButtonHandler.

Walkthough video

If you would like a video walkthough of the above, check out this video:

How to confirm Xamarin Forms Shell Navigation Back Button
Watch this video on YouTube.

Improve Remote Desktop frame rate to 60fps by enabling AVC 4:4:4 encoding

I am a great fan of Remote Desktop and have been using it for over a decade. It’s built in and just works. One gripe of mine has always been the poor framerate which makes animations and transitions super janky by default.

In RDP 10 it turns out this can be massively improved by enabling a screen encoding based on AVC/H.264 video. By enabling the group policy “Prioritize H.264/AVC 444 graphics mode for Remote Desktop Connections” under Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Remote Session Environment, I was able to get a glorious 60fps, almost double what I was getting before.

There is also another group policy to use your GPU to do the encoding, “Configure H.264/AVC hardware encoding for Remote Desktop connections”. Turn this on and you can see your GPU in task manager doing a little bit of work to encode the video, potentially saving the CPU from some effort. However in my testing I actually saw slightly worse performance in this mode, as you can see from this video:

Improve Remote Desktop frame rate up to 60fps!
Watch this video on YouTube.

I do wonder why this option is buried in Group Policy and not more user accessible – it appears to be enabled by default in cloud hosted environments with virtual GPUs (such as the Azure GPU instances). I also wonder if you can actually game using it?