How to display an image from a network folder or local drive in a Windows Universal App

后端 未结 1 434
有刺的猬
有刺的猬 2021-01-03 12:28

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

相关标签:
1条回答
  • 2021-01-03 12:40

    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();
    }
    
    0 讨论(0)
提交回复
热议问题