如何在UWP C#中单击按钮打开特定URL(YouTube视频)?
编辑: 我只是检查整个标记是否为空,而不是标记中的URL,因此它导致始终为空并总是引发异常 我希望能够在用户的默认浏览器中单击按钮打开特定的URL,如YouTube视频、谷歌搜索等 使用如何在UWP C#中单击按钮打开特定URL(YouTube视频)?,c#,url,uwp,uri,C#,Url,Uwp,Uri,编辑: 我只是检查整个标记是否为空,而不是标记中的URL,因此它导致始终为空并总是引发异常 我希望能够在用户的默认浏览器中单击按钮打开特定的URL,如YouTube视频、谷歌搜索等 使用时,等待Windows.System.Launcher.launchurisync(新Uri(url))URL无效,此方法仅打开“正常”网站,如 有没有办法通过点击按钮打开特定的URL? 守则: private async void imageTapEvent(object sender, RoutedEvent
时,等待Windows.System.Launcher.launchurisync(新Uri(url))代码>URL无效,此方法仅打开“正常”网站,如
有没有办法通过点击按钮打开特定的URL?
守则:
private async void imageTapEvent(object sender, RoutedEventArgs e)
{
try
{
if (((String)(((Image)sender).Tag)) != "")
await Windows.System.Launcher.LaunchUriAsync(new Uri(((ImageTag)(((Image)sender).Tag)).url));
}
catch (Exception)
{
MessageDialog msg = new MessageDialog("The chosen URL is invalid", "Invalid URL");
await msg.ShowAsync();
}
标记是保存为字符串的URL
有没有办法在UWP应用程序中打开特定的URL
当然,UWP具有用于呈现html页面的控件,您可以为其提供uri源,然后WebView将在应用程序中呈现网站。有关更多详细信息,请参阅Web视图
但正如我所说,我想在一个按钮点击事件中在默认浏览器中打开URL。这可能吗?当然,您可以通过调用launchurisync
方法使用浏览器启动uri。但它只能使用uri打开,就像使用浏览器访问uri一样。我在这里遇到了一个问题,因为在尝试启动YouTube视频时,会引发异常。它只会打开像microsoft.com这样的网站,而不是像我在问题中说的那样打开整个YouTube视频。是否有任何解决方案,或者我只是做错了什么?请在案例描述中分享您的代码。
<WebView x:Name="webView1" Source="http://www.contoso.com"/>
private async void Button_Click(object sender, RoutedEventArgs e)
{
string uriToLaunch = @"https://www.test.com";
var uri = new Uri(uriToLaunch);
// Set the option to show a warning
var options = new Windows.System.LauncherOptions();
options.TreatAsUntrusted = true;
// Launch the URI with a warning prompt
var success = await Windows.System.Launcher.LaunchUriAsync(uri, options);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}