如何在Xamarin.Forms中使用外部浏览器创建可单击标签以打开外部url?
Xamarin中没有类似链接按钮的东西吗 我想创建一个带有url链接外观的标签,一旦点击该链接,就会打开外部浏览器 我还需要知道如何在便携项目中打开设备的外部浏览器(以打开指定的url)如何在Xamarin.Forms中使用外部浏览器创建可单击标签以打开外部url?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,Xamarin中没有类似链接按钮的东西吗 我想创建一个带有url链接外观的标签,一旦点击该链接,就会打开外部浏览器 我还需要知道如何在便携项目中打开设备的外部浏览器(以打开指定的url) 感谢使用Xamarin.Forms在默认移动浏览器中打开URL字符串的方法: Device.OpenUri(new Uri("http://example.com")) 虽然表单的标签没有单击事件,但您可以添加TapGestureRecognitor,以便在点击标签时执行Device.O
感谢使用
Xamarin.Forms
在默认移动浏览器中打开URL字符串的方法:
Device.OpenUri(new Uri("http://example.com"))
虽然表单的
标签
没有单击事件,但您可以添加TapGestureRecognitor
,以便在点击标签时执行Device.OpenUri
例子:
Xamarin Forms Labs’ExtendedLabel允许您将标签文本的样式设置为下划线
参考:创建一个可以显示下划线的标签(或使用Xamarin Forms Labs ExtendedLabel): 视图模型:
public class YourPageViewModel : ViewModelBase
{
public const string NavigateToUrlMessage = "NavigateToUrl";
private string _url;
public string Url
{
get { return _url; }
set { SetProperty(ref _url, value); }
}
//...
private Command _navigateToUrlCommand;
public ICommand NavigateToUrlCommand
{
get { return _navigateToUrlCommand ?? (_navigateToUrlCommand = new Command(param => NavigateToUrl(), CanNavigateToUrl)); }
}
public bool CanNavigateToUrl(object parameter) => true;
private void NavigateToUrl()
{
MessagingCenter.Send(this, NavigateToUrlMessage, Url);
}
//...
}
订阅并处理YourPage.xaml.cs代码中的NavigateToUrlMessage消息:
protected override void OnAppearing()
{
MessagingCenter.Subscribe<YourPageViewModel, string>(this, YourPageViewModel.NavigateToUrlMessage, (sender, args) =>
{
var context = (BindingContext as YourPageViewModel);
if (context == null) return;
if (string.IsNullOrEmpty(args)) return;
Device.BeginInvokeOnMainThread(() =>
{
Device.OpenUri(new Uri(args));
});
});
//...
base.OnAppearing();
}
protected override void OnDisappearing()
{
MessagingCenter.Unsubscribe<YourPageViewModel, string>(this, YourPageViewModel.NavigateToUrlMessage);
//...
base.OnDisappearing();
}
protected override void OnAppearing()
{
MessagingCenter.Subscribe(此,YourPageViewModel.NavigateToUrlMessage,(发件人,参数)=>
{
var context=(BindingContext作为您的PageViewModel);
if(context==null)返回;
if(string.IsNullOrEmpty(args))返回;
Device.beginInvokeMainThread(()=>
{
OpenUri(新的Uri(args));
});
});
//...
base.OnAppearing();
}
受保护的覆盖无效OnDisappearing()
{
取消订阅(这是YourPageViewModel.NavigateToUrlMessage);
//...
base.OnDisappearing();
}
适用于Xamarin.表格3.2及以上版本
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Url, Mode=OneWay}" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://www.xamarin.com"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Xamarin" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://www.xamarin.com"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
private ICommand _tapCommand;
public ICommand TapCommand =>
_tapCommand ?? (_tapCommand = new Command<string>(OpenUrl));
void OpenUrl(string url)
{
Device.OpenUri(new Uri(url));
}
您可以使用带参数或不带参数的命令,也可以仅使用点击事件
示例:
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Url, Mode=OneWay}" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://www.xamarin.com"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Xamarin" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://www.xamarin.com"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
private ICommand _tapCommand;
public ICommand TapCommand =>
_tapCommand ?? (_tapCommand = new Command<string>(OpenUrl));
void OpenUrl(string url)
{
Device.OpenUri(new Uri(url));
}
在视图模型中:
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Url, Mode=OneWay}" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://www.xamarin.com"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
<Label>
<Label.FormattedText>
<FormattedString>
<Span Text="Xamarin" TextColor="Blue">
<Span.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding TapCommand, Mode=OneWay}"
CommandParameter="https://www.xamarin.com"/>
</Span.GestureRecognizers>
</Span>
</FormattedString>
</Label.FormattedText>
</Label>
private ICommand _tapCommand;
public ICommand TapCommand =>
_tapCommand ?? (_tapCommand = new Command<string>(OpenUrl));
void OpenUrl(string url)
{
Device.OpenUri(new Uri(url));
}
private-ICommand\u-tap命令;
公共ICommand TapCommand=>
_tapCommand??(_tapCommand=new命令(OpenUrl));
void OpenUrl(字符串url)
{
OpenUri(新的Uri(url));
}
Wow!谢谢但如果我只需要一个链接按钮就需要这些,我会学习Objective C。我可以在Android和iOS项目中比在Xamarin中更快地完成同样的事情。我开始觉得这个Xamarin是在浪费时间。缺少这么简单的东西并不是什么新鲜事。谢谢。很遗憾,我需要一个外部库。Xamarin看起来很失望。