嗨我想构建一个简单的xamarin.forms应用程序,所以我在这里有xaml代码所以在这段代码中我想添加网站url所以任何人都可以请帮助我需要添加xaml代码添加网站的选项网址?当我尝试使用标签和文本选项添加网址时,我想添加标签杂货下面的链接,网址仍然就像标签一样,但我希望它能以这样一种方式,如果我点击网址,它应该带我到浏览器中的网站
<?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>
答案 0 :(得分:2)
<Button Text="Google" Clicked="GoGoogle" />
protected void GoGoogle(object sender, EventArgs e) {
Device.OpenUri(new 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"));
}
答案 1 :(得分:0)
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Go to SO" ForegroundColor="Blue"/>
</FormattedString>
</Label.FormattedText>
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="GoToSO" />
</Label.GestureRecognizers>
</Label>
protected void GoToSO(object sender, EventArgs e)
{
Device.OpenUri(new Uri("http://www.stackoverflow.com"));
}
答案 2 :(得分:0)
如果将ICommands与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>