【问题标题】:Get url from image source and launch it (UWP)从图像源获取 url 并启动它 (UWP)
【发布时间】:2017-11-22 05:24:00
【问题描述】:

我有 UWP 应用程序,其中有带有 url 源的图像。

这里是 xaml 代码:

 <Image x:Name="Image" HorizontalAlignment="Left" Height="200"  Width="200" Tapped="Image_Tapped">
                        <Image.Source>
                            <BitmapImage UriSource="{Binding data.thumbnail}" />
                        </Image.Source>
                    </Image>

我创建了 Tapped 事件处理程序

这里是代码

  private void Image_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var source = Image.SourceProperty.ToString();
            Debug.WriteLine(source);
        }

但是好像不对。

如何获取 ImageSource 并在浏览器中启动此 url(图像源为 url)?

【问题讨论】:

  • “但似乎不对”是什么意思。 ?绑定不起作用还是您需要在某些查看器中打开缩略图?

标签: c# .net xaml uwp


【解决方案1】:

您需要 Launcher 类。

private async void Image_Tapped(object sender, TappedRoutedEventArgs e)
{
    if (((Image)sender).Source is BitmapImage bitmapImage)
    {
        var uri = bitmapImage.UriSource;

        // Launch the URI
        var success = await Windows.System.Launcher.LaunchUriAsync(uri);

        if (success)
        {
            // URI launched
        }
        else
        {
            // URI launch failed
        }
    }
}

另外请注意,由于您已经指定了图像大小(即 200x200)以节省一点内存,因此您可能希望将图像解码为渲染大小。如果您直接使用Image.Source,则不必这样做。

<BitmapImage DecodePixelWidth="200" DecodePixelHeight="200" ... />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多