【问题标题】:How to get path to item which is in <Image/> in UWP? Closed如何获取 UWP 中 <Image/> 中项目的路径?关闭
【发布时间】:2020-04-10 11:41:50
【问题描述】:

我想获取 Image(Xaml) 中的项目的路径。我做弹出菜单并想将图像复制到剪贴板,但我希望我的程序知道我点击了哪个图像。现在我有了这个:

private async void CopyImage_Click(object sender, RoutedEventArgs e )
        {
            StorageFile file = await StorageFile.GetFileFromPathAsync(here i must have the path);
            List<StorageFile> storageFiles = new List<StorageFile>(1);
            storageFiles.Add(file);

            var dataPackage = new DataPackage();
            dataPackage.SetStorageItems(storageFiles);
            dataPackage.RequestedOperation = DataPackageOperation.Copy;

            try
            {
                Clipboard.SetContent(dataPackage);
            }
            catch (Exception ex)
            {

            }
        }

【问题讨论】:

  • 这个Image的内容怎么设置?如果您使用BitmapImageUriSource,您可以从那里获取文件路径。

标签: c# .net xaml uwp


【解决方案1】:

正如@Xerillio 所说,您可以从Image Source 属性中获取uri 方案,并使用GetFileFromApplicationUriAsync 获取存储文件。详细步骤请参考以下代码。

private async void MyImage_Tapped(object sender, TappedRoutedEventArgs e)
{
    var souce = MyImage.Source as BitmapImage;
    var uri = souce.UriSource;
    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
    List<StorageFile> storageFiles = new List<StorageFile>(1);
    storageFiles.Add(file);

    var dataPackage = new DataPackage();
    dataPackage.SetStorageItems(storageFiles);
    dataPackage.RequestedOperation = DataPackageOperation.Copy;

    try
    {
        Clipboard.SetContent(dataPackage);
    }
    catch (Exception ex)
    {

    }
}

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 2014-11-20
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多