Xaml 如何在xamarin.forms应用程序中添加url

Xaml 如何在xamarin.forms应用程序中添加url,xaml,xamarin.forms,Xaml,Xamarin.forms,大家好,我想构建一个简单的xamarin.forms应用程序,所以我在这里有xaml代码,所以在这段代码中我想添加网站url,所以任何人都可以帮助我在xaml代码中添加哪些选项来添加网站url?当我尝试使用标签和文本选项添加url时,我想在标签杂货下面添加链接url保持与标签相同,但我希望这样,如果我单击url,它会将我带到浏览器中的站点 <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xa

大家好,我想构建一个简单的xamarin.forms应用程序,所以我在这里有xaml代码,所以在这段代码中我想添加网站url,所以任何人都可以帮助我在xaml代码中添加哪些选项来添加网站url?当我尝试使用标签和文本选项添加url时,我想在标签杂货下面添加链接url保持与标签相同,但我希望这样,如果我单击url,它会将我带到浏览器中的站点

 <?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>


受保护的无效护目镜(对象发送器,事件参数e){
OpenUri(新的Uri(“http://www.google.com"));
}
如果不想使用按钮,可以使用带有手势的标签

<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"));
    }

受保护的无效护目镜(对象发送器,事件参数e){
OpenUri(新的Uri(“http://www.google.com"));
}

受保护的void GoToSO(对象发送方,事件参数e)
{
OpenUri(新的Uri(“http://www.stackoverflow.com"));
}

如果将iCommand与MVVM一起使用,可以执行以下操作:

使用具有ICommand属性的ViewModel:

public class MyViewModel : INotifyPropertyChanged
{
    // details redacted ...

    public MyViewModel()
    {
        //...
        OpenGoogleCommand = new Command(() => Device.OpenUri(new Uri("http://www.google.com")));
    }
    public ICommand OpenGoogleCommand { get; }
}
然后在标签中绑定命令:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="Link to Google" FontSize="Large"
                      TextColor="Blue"
                      FontAttributes="Bold"/>
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding OpenGoogleCommand}" />
    </Label.GestureRecognizers>
</Label>


什么是“想要添加网站url”?您是否只想在应用程序中显示它?或者您希望用户能够单击它吗?它应该在应用程序内部打开,还是打开外部浏览器?我希望用户能够单击它,并在外部浏览器中打开它而不是按钮我希望在屏幕上显示url,单击url它应该在外部浏览器中打开,例如,我希望在屏幕上显示为“www.google.com”当用户点击这个链接时,它应该在外部浏览器中打开,您能告诉我,我应该为xaml中的url提供什么而不是按钮吗?谢谢如果你有具体的要求,你应该把它们写在你的原始帖子里,这样人们就不会浪费时间写没有帮助的答案了。嗨,你能帮我吗?
public class MyViewModel : INotifyPropertyChanged
{
    // details redacted ...

    public MyViewModel()
    {
        //...
        OpenGoogleCommand = new Command(() => Device.OpenUri(new Uri("http://www.google.com")));
    }
    public ICommand OpenGoogleCommand { get; }
}
<Label>
    <Label.FormattedText>
        <FormattedString>
            <FormattedString.Spans>
                <Span Text="Link to Google" FontSize="Large"
                      TextColor="Blue"
                      FontAttributes="Bold"/>
            </FormattedString.Spans>
        </FormattedString>
    </Label.FormattedText>
    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding OpenGoogleCommand}" />
    </Label.GestureRecognizers>
</Label>