如何在 Windows 通用应用程序中显示来自网络文件夹或本地驱动器的图像 [英] How to display an image from a network folder or local drive in a Windows Universal App

查看:16
本文介绍了如何在 Windows 通用应用程序中显示来自网络文件夹或本地驱动器的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在 Windows 通用应用程序的图像控件中显示 jpg.(我在尝试创建 Windows 8 Store 应用时也遇到了同样的问题)

我有一个简单的表单,上面有一个 Image 控件.我想要做的就是能够在本地驱动器或本地网络上的网络驱动器上的文件夹中打开图像并显示它们.但我没有任何运气.我唯一得到的是 E_NETWORK_ERROR,没有附加信息.

我认为这可能与安全有关,但肯定必须有设置或权限允许我这样做.我尝试在清单的功能"选项卡中启用专用网络,但这没有帮助.我在声明中没有看到任何听起来像我需要的东西.

我知道 UWP 应用程序在某种程度上是沙盒化的,但如果您甚至无法访问本地文件,它们有什么用?

这是我尝试过的代码示例.我也进行了其他迭代,但它们的最终结果都相同.

XML:

背后的代码:

 public LoadImage(){var bitmap = new BitmapImage();bitmap.ImageFailed += Bitmap_ImageFailed;bitmap.UriSource = new Uri(@"D:UsersSteveDocumentsSomeImage.JPG", UriKind.Absolute);Image1.Source = 位图;}私有无效 Bitmap_ImageFailed(对象发送者,ExceptionRoutedEventArgs e){Debug.Write(e.ErrorMessage);}

当我运行它时,我最终遇到了 Bitmap_ImageFailed 事件并且 ErrorMessage 属性只是E_NETWORK_ERROR",并且图像中没有显示任何内容.不是很有帮助.

我错过了什么?它必须是我忽略的简单而明显的东西.

更新:

多亏了这里的所有建议,我才得以顺利进行.我未能通过我的头骨的部分是你不能只给它一个文件夹并期望它读取文件,即使作为快速和肮脏的测试".你必须通过适当的渠道"才能到达那里.我把它们拼凑在一起,想出了这个例子,它显示了所选文件夹中的第一张图像:

 private async void Button_Click(object sender, RoutedEventArgs e){FolderPicker folderPicker = new FolderPicker();folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;folderPicker.FileTypeFilter.Add(".jpg");folderPicker.FileTypeFilter.Add(".tif");folderPicker.FileTypeFilter.Add(".png");StorageFolder 文件夹 = 等待 folderPicker.PickSingleFolderAsync();如果(文件夹!= null){StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", 文件夹);var files = await folder.GetFilesAsync();var bitmap = new BitmapImage();bitmap.ImageFailed += Bitmap_ImageFailed;var stream = await files.First().OpenReadAsync();等待 bitmap.SetSourceAsync(stream);Image1.Source = 位图;}}

此外,我必须将 .jpg、.tif 和 .png 的文件类型以及文件打开选择器添加到声明中.

解决方案

您可以在 MSDN 文章

我设置了一个新的文件类型 (.txt) 并让它发挥作用.和代码示例

async void Button_Click_2(object sender, RoutedEventArgs e){var _Name = "HelloWorld.txt";var _Folder = KnownFolders.DocumentsLibrary;var _Option = Windows.Storage.CreationCollisionOption.ReplaceExisting;//创建文件var _File = await _Folder.CreateFileAsync(_Name, _Option);//写入内容var _WriteThis = "你好世界!";等待 Windows.Storage.FileIO.WriteTextAsync(_File, _WriteThis);//获取文件尝试 { _File = await _Folder.GetFileAsync(_Name);}catch (FileNotFoundException) {/* TODO */}//读取内容var _Content = await FileIO.ReadTextAsync(_File);等待新的 Windows.UI.Popups.MessageDialog(_Content).ShowAsync();}

I'm having trouble displaying an jpg in an Image control in a Windows Universal App. (I had the same problem trying to create a Windows 8 Store app as well)

I have a simple form with an Image control on it. All I want to do is be able to open images in a folder on my local drive or a network drive on my local network and display them. But I am not having any luck. The only thing I get is E_NETWORK_ERROR, with no additional information.

I assume it probably has something to do with security, but surely there must be a setting or permission to allow me to do it. I tried enabling Private Networks in the Capabilities tab of the manifest, but that didn't help. I don't see anything in Declarations that sounds like what I need.

I know UWP apps are somewhat sandboxed, but if you can't even access local files, what good are they?

Here is a sample of code I have tried. I've done other iterations as well, but they all have the same end result.

Xaml:

<Image Name="Image1"/>

Code behind:

    public LoadImage()
    {
        var bitmap = new BitmapImage();
        bitmap.ImageFailed += Bitmap_ImageFailed;
        bitmap.UriSource = new Uri(@"D:UsersSteveDocumentsSomeImage.JPG", UriKind.Absolute);
        Image1.Source = bitmap;
    }

    private void Bitmap_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
        Debug.Write(e.ErrorMessage);
    }

When I run it, I end up in the Bitmap_ImageFailed event and the ErrorMessage property is simply "E_NETWORK_ERROR", and nothing is displayed in the Image. Not very helpful.

What am I missing? It has to be something simple and obvious that I am overlooking.

Update:

Thanks to all the suggestions here I was able to get it going. The part I was failing to get through my skull was that you can't just give it a folder and expect it to read a file, even as a "quick & dirty test". You have to go through "proper channels" to get there. I pieced it all together and came up with this example which displays the first image in the selected folder:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add(".jpg");
        folderPicker.FileTypeFilter.Add(".tif");
        folderPicker.FileTypeFilter.Add(".png");

        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
        if (folder != null)
        {
            StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
            var files = await folder.GetFilesAsync();

            var bitmap = new BitmapImage();
            bitmap.ImageFailed += Bitmap_ImageFailed;
            var stream = await files.First().OpenReadAsync();
            await bitmap.SetSourceAsync(stream);
            Image1.Source = bitmap;
        }
    }

In addition, I had to add the file types for .jpg, .tif and .png as well as File Open Picker to the Declarations.

解决方案

You can figure out all necessary information in MSDN article File access permissions

In addition to the default locations, an app can access additional files and folders by declaring capabilities in the app manifest (see App capability declarations), or by calling a file picker to let the user pick files and folders for the app to access (see Open files and folders with a picker).

So if you want to read a file from users document folder you need to update your applications AppXManifest to request the Document Library Access capability.

You also need to update your AppXManifest by declaring what file type(s) you want to access. Then, even with access to the folders, you only have access to a limited set of file types. You have to specify supported files types on Declarations tab

I set a new file type (.txt) and let it role from there. And code example

async void Button_Click_2(object sender, RoutedEventArgs e)
{
    var _Name = "HelloWorld.txt";
    var _Folder = KnownFolders.DocumentsLibrary;
    var _Option = Windows.Storage.CreationCollisionOption.ReplaceExisting;

    // create file 
    var _File = await _Folder.CreateFileAsync(_Name, _Option);

    // write content
    var _WriteThis = "Hello world!";
    await Windows.Storage.FileIO.WriteTextAsync(_File, _WriteThis);

    // acquire file
    try { _File = await _Folder.GetFileAsync(_Name); }
    catch (FileNotFoundException) { /* TODO */ }

    // read content
    var _Content = await FileIO.ReadTextAsync(_File);
    await new Windows.UI.Popups.MessageDialog(_Content).ShowAsync();
}

这篇关于如何在 Windows 通用应用程序中显示来自网络文件夹或本地驱动器的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆