Maui app as link shortcut

asked 2022-12-28T13:50:34.227+00:00
Salvatore Rizzo 21 Reputation points

Hi all, I would like to create an android app that is convenient for opening the browser to a specific url. The Bookmark Shortcut solution doesn't suit me because the browser icon appears while with my app I could customize the icon.
I'm having trouble closing my app automatically when the browser is opened.

To open a new browser window I use this code

public partial class AppShell : Shell  
{  
	public AppShell()  
	{  
		InitializeComponent();  
        try  
        {  
            Uri uri = new Uri("https://www.microsoft.com");  
              
            Browser.Default.OpenAsync(uri, BrowserLaunchMode.External);  
            Application.Current.Quit();  
        }  
        catch (Exception ex)  
        {  
            // An unexpected error occured. No browser may be installed on the device.  
        }  
    }  
}  

and to close my app i use this:

Application.Current.Quit();  

but it doesn't work, the app always stays open.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
874 questions
No comments
{count} votes

Accepted answer
  1. answered 2022-12-29T02:02:26.627+00:00
    Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 11,061 Reputation points Microsoft Employee

    Hello,

    It is caused by you what to quit your application on the Shell initial method.

    It is not possible to exit the shell initialization through the Quit method.

    You could add your code into OnAppearing method of your MainPage to implement this feature:

       protected override void OnAppearing()  
       {  
           base.OnAppearing();  
           try  
           {  
               Uri uri = new Uri("https://www.microsoft.com");  
              Browser.Default.OpenAsync(uri, BrowserLaunchMode.External);  
               Application.Current.Quit();  
           }  
           catch (Exception ex)  
           {  
               Console.WriteLine(ex.Message);  
           }  
       }  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments

1 additional answer

Sort by: Most helpful
  1. answered 2022-12-29T08:42:31.227+00:00
    Salvatore Rizzo 21 Reputation points

    Thank you so much,
    everything works fine now.

    No comments