Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Use something to pick files in a WinUI 3 Blank App #4100

Closed
kirilvalev opened this issue Feb 4, 2021 · 14 comments
Closed

Question: Use something to pick files in a WinUI 3 Blank App #4100

kirilvalev opened this issue Feb 4, 2021 · 14 comments

Comments

@kirilvalev
Copy link

I created a WinUI 3 Blank App and I am trying to select a file from the file system. Is there something in Winui 3 to support me doing so, FilePicker and OpenFileDialog do not seem to work with Winui3.

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Feb 4, 2021
@huoyaoyuan
Copy link

They can work with workarounds: #2716 (comment)

@kirilvalev
Copy link
Author

@huoyaoyuan, thank you. This only seems to work for WinUI in UWP projects and not for WinUI in Desktop projects.

@huoyaoyuan
Copy link

huoyaoyuan commented Feb 4, 2021

It works for me in WinUI desktop.

@kirilvalev
Copy link
Author

kirilvalev commented Feb 4, 2021

Would you please share your .csproj file?

`

WinExe
net5.0-windows10.0.18362.0
10.0.17134.0
TestProjectWinUIDesktop
app.manifest
x86;x64;arm64
win10-x86;win10-x64;win10-arm64

`

@huoyaoyuan
Copy link

The project file is unrelated. I'm just using default of preview3.
Where are you calling the picker? It must be called when a window is shown.

@kirilvalev
Copy link
Author

Thank you. I am also using WinUI preview 3. New "Blank App, Packaged (WinUI in Desktop)":

`using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace TestProjectWinUIDesktop
{
///


/// An empty window that can be used on its own or navigated to within a Frame.
///

public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}

    private async void myButton_Click(object sender, RoutedEventArgs e)
    {
        myButton.Content = "Clicked";


        // Open a text file.
        Windows.Storage.Pickers.FileOpenPicker open = new Windows.Storage.Pickers.FileOpenPicker
        {
            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary
        };
        open.FileTypeFilter.Add(".rtf");

        Windows.Storage.StorageFile file = await open.PickSingleFileAsync();

        if (file != null)
        {
            using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
                await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                // Load the file into the Document property of the RichEditBox.
                // editor.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);
            }
        }


    }
}

}`

Somehow the code feature in the editor is not formatting my code, sorry for that...

@huoyaoyuan
Copy link

You are using the default approach, which doesn't work. You need workaround from this comment: #2716 (comment)

@kirilvalev
Copy link
Author

I understand, thank you! I thought, since the ticket #2716 is closed it would work without a workaround.

@marb2000
Copy link
Contributor

marb2000 commented Feb 5, 2021

Hi there,

In Desktop, or Win32, it's required to specify which Window Handle (HWND) owns the File/Folder Picker. It's the current design. Here you can find a code snipped:

 public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private async void myButton_Click(object sender, RoutedEventArgs e)
        {
            var filePicker = new FileOpenPicker();

            //Get the Window's HWND
            var hwnd = this.As<IWindowNative>().WindowHandle;
       
            //Make folder Picker work in Win32
            var initializeWithWindow = filePicker.As<IInitializeWithWindow>();
            initializeWithWindow.Initialize(hwnd);
      
            filePicker.FileTypeFilter.Add("*");

            var folder = await filePicker.PickSingleFileAsync();
            myText.Text = folder != null ? folder.Path : string.Empty;
        }

        [ComImport]
        [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }
        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
        internal interface IWindowNative
        {
            IntPtr WindowHandle { get; }
        }
    }

@marb2000 marb2000 added area-AppWindow and removed needs-triage Issue needs to be triaged by the area owners labels Feb 5, 2021
@marb2000
Copy link
Contributor

marb2000 commented Feb 8, 2021

Closing the issue. I hope the sample is good enough.

@marb2000 marb2000 closed this as completed Feb 8, 2021
@nickrandolph
Copy link

@marb2000 the sample works fine.
A note for anyone trying to get this to work - make sure you add "using WinRT;" to the file where you're adding this code, otherwise you'll get compile issues with the "As" method. Window (as with a lot of WinUI classes) has a static As method, which conflicts with the As extension method available when adding the WinRT namespaces.

@marb2000 general comments/questions:

  • This is super ugly to have to do this - is there a plan to clean this up so the developers don't have to deal with this hacky workaround?
  • Is there a way to fall back to the UWP file picker when running in partial trust?

@mrjfalk
Copy link

mrjfalk commented Jul 24, 2021

I also ran into this issue, and while the workaround works it's not very nice experience and took some time to find this workaround.

As I use pickers in a number of places I wrote an extension method to help with this (usage: picker.SetOwnerWindow(this);), might be useful for others as well: https://gist.github.com/mrjfalk/93f7c66cabce1f68fef7102c38b2505e

@Rybasum
Copy link

Rybasum commented Nov 10, 2021

@marb2000 The only reason I have to stay with UWP is that I want to run in AppContainer. Thanks to @nickrandolph I can run WinUI 3 in AppContainer but attempting to use hWnd with a file open picker leads to an Access Denied exception. Please, could Microsoft provide a way to initialise popup windows without an explicit usage of a hWnd?

@ghostidentity
Copy link

Hi, Im not sure why its close. I tried on Winui3 but there's an issue.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants