Maui: Toast throws exception on Windows


Issue moved from dotnet/maui#12976


From @cdavidyoung on Saturday, January 28, 2023 1:53:52 PM

Description

With the most recent updates Toast is now throwing an exception on Windows but works fine on the Mac. This is a simple mod to the standard Maui app that illustrates the problem. If you run this and set a breakpoint on the exception it stops when you press the button.

using CommunityToolkit.Maui.Alerts;
namespace TooltipsSample;

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
	}

	private void OnCounterClicked(object sender, EventArgs e)
	{
		count++;
        CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        var toast = Toast.Make("Toast");
        try
        {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            toast.Show(cancellationTokenSource.Token);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
        catch (Exception ex)
        {
            // This gives an exception on Windows
        }
 
        if (count == 1)
			CounterBtn.Text = $"Clicked {count} time";
		else
			CounterBtn.Text = $"Clicked {count} times";

		SemanticScreenReader.Announce(CounterBtn.Text);
	}
}

Steps to Reproduce

See the description for sample code.

Link to public reproduction project repository

https://github.com/dotnet/maui

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

I was not able test on other platforms

Affected platform versions

windows10.0.19041

Did you find any workaround?

use try/catch to not crash but Toast not working

Relevant log output

{"Could not load file or assembly 'Microsoft.Windows.AppNotifications.Builder.Projection, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.":"Microsoft.Windows.AppNotifications.Builder.Projection, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null"}

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 30 (8 by maintainers)

Most upvoted comments

Sorry. I had just read your last message and not the previous one. I build within VS so I can’t comment on those build commands.

@cdavidyoung add this line to yor csproj file:

<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.2.230118.102" />