Essentials: [Bug] Calling Launcher. TryOpenAsync(string uri) causes exception

Created on 9 Mar 2020  路  7Comments  路  Source: xamarin/Essentials

Description

Steps to Reproduce

  1. Invoke this code
string email = "[email protected]";
await Device.InvokeOnMainThreadAsync(() => Launcher.TryOpenAsync($"mailto:{email}"));

Expected Behavior

No exception is beign thrown

Actual Behavior

Exception System.InvalidOperationException with message _'A method was called at an unexpected time.'_ is thrown

Basic Information

  • Version with issue: 1.3.1 & 1.5.1
  • IDE: VS 2019
  • Platform Target Frameworks:

    • UWP: Windows 10, version 1809 (10.0; Build 17763)

Screenshots

obrazek

bug unable-to-reproduce uwp

All 7 comments

I am unable to reproduce this with your code. can you please provide a sample.

Go to TrackerViewModel.MailTo(object obj) to see the command executed (or place breakpoint). Execute command by launching the application and pressing the button _"Open email uri"_

Executed code on button press:

private async void MailTo(object obj)
{
    string email = "[email protected]";
    await Device.InvokeOnMainThreadAsync(() => Xamarin.Essentials.Launcher.TryOpenAsync($"mailto:{email}"));
}

Sample.zip

.csproj

<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
.
.
.
<PackageReference Include="Xamarin.Forms" Version="4.5.0.356" />
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.9" />
<PackageReference Include="Xamarin.Essentials" Version="1.5.1" />

I was able to reproduce your issue.

Calling "OpenAsync" works fine, however "TryOpenAsync" does not.

Workaround:'

private async void MailTo(object obj)
        {
            string email = "[email protected]";
            if (await Xamarin.Essentials.Launcher.CanOpenAsync("mailto:"))
                await Xamarin.Essentials.Launcher.OpenAsync($"mailto:{email}");
        }

You do not need to invoke on the main thread as it is coming from a command in which it already is on the main thread :)

Hmmm strange. it only seems to be an issue when targeting newer versions of UWP which is why we didn't catch it.

You do not need to invoke on the main thread as it is coming from a command in which it already is on the main thread :)

Thank you, I'm aware of that, but in order to try fixing this error, I tried various approaches.
Nevertheless, thanks for suggesting the workaround.

Workaround with subject and body (unfortionatly only plaintext on android possible). This opens less non-email related apps to send an email (if using e.g. Email.ComposeAsync(...) )
if (await Launcher.CanOpenAsync(new Uri("mailto:"))) { await Launcher.OpenAsync($"mailto:{emailTo}?subject={subject}&body={body}"); } else { await Email.ComposeAsync(subject, body, new []{ emailTo }); }

Fixed in #1171

Was this page helpful?
0 / 5 - 0 ratings