agent_bean
agent_bean

Reputation: 1575

Tap Gesture not responding

I'm trying to create a clickable label. The about page displays properly, but I am not able to interact with the span.

What am I missing?

AboutPage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:local="clr-namespace:App1"
             mc:Ignorable="d"
             x:Class="App1.AboutPage">

    <!-- Required to map viewmodel -->
    <ContentPage.BindingContext>
        <local:AboutViewModel />
    </ContentPage.BindingContext>
    
    <ContentPage.Content>
        <StackLayout
            VerticalOptions = "Center"
            HorizontalOptions = "Center">
            <Label Text="Test"
                   FontSize="30"
                   FontAttributes ="Bold"></Label>
            <Label Text="Version 2.0"></Label>
            <Label Text="Copyright © 2020"></Label>
            <Label Text="All rights reserved"></Label>
            
            <Label>
                <Label.FormattedText>
                    <FormattedString>
                        <Span Text="License Agreement"
                  TextColor="Blue"
                  TextDecorations="Underline">
                            <Span.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding TapCommand}" />
                            </Span.GestureRecognizers>
                        </Span>
                    </FormattedString>
                </Label.FormattedText>
            </Label>
        </StackLayout>

    </ContentPage.Content>
</ContentPage>

AboutViewModel.cs

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace App1
{

    class AboutViewModel
    {
        public System.Windows.Input.ICommand TapCommand => new Command(About);

        public AboutViewModel()
        {

        }

        public void About()
        {
            try
            {
                Uri uri = new Uri("google.com");
                Device.OpenUri(uri);
            }
            catch (Exception ex)
            {
                //handle the exception
            }
        }
    }
}

Upvotes: 0

Views: 205

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10958

Device.OpenUri(Uri) Method is now obsolete. https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.device.openuri?view=xamarin-forms

You could use Xamarin.Essentials: Launcher instead.

 async void About()
    {
       
            //Uri uri = new Uri("google.com");
            //Device.OpenUri(uri);
            //https://www.google.com/
            
                await Launcher.OpenAsync("https://www.google.com/");

      
    }

Screenshot:

enter image description here

Upvotes: 1

Related Questions