Can Xamarin.Essentials: Browser be a part of the page?

Yongqing Huang 241 Reputation points
2022-01-19T07:34:46.347+00:00

Hi,

I would like to have Xamarin.Essentials: Browser to be a part of the page:
at the top is a entry to type in URL, plus a couple buttons.
The rest of page is for Xamarin.Essentials: Browser.
If "Search" button is clicked, Xamarin.Essentials: Browser will be activated.
Normally Xamarin.Essentials: Browser navigate to a new page.

Is there a way to keep my UI elements (entry, buttons) en Browser in ONE page?
so that user can type in URL in my entry, browser in Chrome/Firefox.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,302 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,616 Reputation points Microsoft Vendor
    2022-01-20T06:57:46.46+00:00

    Hello Yongqing,​

    Welcome to our Microsoft Q&A platform!

    Can Xamarin.Essentials: Browser be a part of the page?

    No, you can see source code of the Xamarin.Essentials: Browser in Android, Browser use Platform.AppContext.StartActivity(intent); way to open a new page. If you start Activity in Android, it will open a new page.

    If you want to keep your UI elements (entry, buttons) en Browser in ONE page? You can use SearchBar and Webview to achieve it like following layout. Use SearchBar to input url, use webview to show webpage.

       <ContentPage.Content>  
               <StackLayout>  
                   <SearchBar x:Name="webviewSearch" SearchButtonPressed="webviewSearch_SearchButtonPressed" HorizontalOptions="FillAndExpand"></SearchBar>  
                   <WebView x:Name="MyWebview"  HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"></WebView>  
               </StackLayout>  
           </ContentPage.Content>  
    

    Here is background code. When you click the search button in the keyboard, this webview will show web page. And if you want to keep the user's credentials, please create a custom renderer for webview, enable the Control.Settings.JavaScriptEnabled = true ; and Control.Settings.DomStorageEnabled = true ; like previous question.

       public partial class SchedulePage : ContentPage  
           {  
               public SchedulePage()  
               {  
                   InitializeComponent();  
               }  
         
               private void webviewSearch_SearchButtonPressed(object sender, EventArgs e)  
               {  
                     
                   if (webviewSearch.Text.StartsWith("https://"))  
                   {  
                       //if url do  start with https://, Search it  
                       MyWebview.Source = webviewSearch.Text;  
                   }  
                   else  
                   {  
                       //if url do not start with https://, add this pre-fix  
                       MyWebview.Source = "https://" + webviewSearch.Text;  
                   }  
                   
                
               }  
           }  
    

    Best Regards,

    Leon Lu


    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Yongqing Huang 241 Reputation points
    2022-01-20T09:10:34.15+00:00

    Thanks, Leon.

    0 comments No comments