xamlxamarin.forms

how to add an url in xamarin.forms app


hi I want to build a simple xamarin.forms app so I have the xaml code here so in this code I want to add website url so can anyone please help what options do i need to add in xaml code to add the website url ? I want to add link below the label groceries when I tried to add url using label and text options the url remained just like label but I want it in such a way that if I click on the url it should take me to the site in browser .

 <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyApp.AboutPage">
   <ContentPage.Content>
    <StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" 
 VerticalOptions="CenterAndExpand">
        <Label Text="My List" />
        <Label Text="groceries"/>
        <Button BackgroundColor="black" TextColor="White" WidthRequest="40" 
  HeightRequest="40" Text="OK" Clicked="OnRedirectMain" 
   BorderColor="Transparent" BorderWidth="1"/>

    </StackLayout>
</ContentPage.Content>
 </ContentPage>

Solution

  • <Button Text="Google" Clicked="GoGoogle" />
    
    protected void GoGoogle(object sender, EventArgs e) {
      Device.OpenUri(new Uri("http://www.google.com"));
    }
    

    if you don't want to use a Button, you can use a Label with a gesture

    <Label Text="Google">
      <Label.GestureRecognizers>
          <TapGestureRecognizer Tapped="GoGoogle" />
      </Label.GestureRecognizers>
    </Label>
    
    protected void GoGoogle(object sender, EventArgs e) {
          Device.OpenUri(new Uri("http://www.google.com"));
        }