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

OpenFileDialog is not working with WinUI3 preview 2 and .NET5 preview 7 for desktop app development #3295

Closed
anshul-rohilla opened this issue Sep 17, 2020 · 12 comments
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps no-issue-activity product-winui3 WinUI 3 issues question

Comments

@anshul-rohilla
Copy link

I recently started migrating an old desktop app to WinUI3. The application has simple UI but complex backend business logic. I started with creating a blank new desktop application using WinUI3 preview 2 and .NET5 preview 7 and then slowly built the new UI. Everything looks great until I encountered problem with file browser functionality. OpenFileDialog is not recognized and build is failing. I have tried following:

  1. Use FileOpenPicker instead of OpenFileDialog
    No compile time error but failed on runtime as FileOpenPicker is built for UWP.

  2. Tried changing the SDK to "Microsoft.NET.Sdk.WindowsDesktop"
    It fixes the issue with OpenFileDialog, but many other issues start surfacing.

  3. Tried changing target framework to "net5.0-windows"
    Visual Studio is not recognizing the target framework at all.

Can someone please help me out here? This is the only blocker I have.

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Sep 17, 2020
@StephenLPeters StephenLPeters added appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues labels Sep 17, 2020
@StephenLPeters
Copy link
Contributor

@stevenbrix This sound really familiar, do you remember something like this (a cswinrt bug causing file picker to fail)?

@stevenbrix
Copy link
Contributor

@anshul-rohilla can you take a look at https://github.com/microsoft/WinUI-3-Demos/tree/master/DemoBuildCs? This should be fixed by Preview3 so that you don't have any awkward steps or weird wrappers

@anshul-rohilla
Copy link
Author

@StephenLPeters @stevenbrix thank you so much for providing updates. I will wait for Preview 3, meanwhile I will check the demo link you have provided.

@ranjeshj ranjeshj removed the needs-triage Issue needs to be triaged by the area owners label Sep 18, 2020
@kirilvalev
Copy link

This is still failing with preview 3. Any update on the issue?

@cornem
Copy link

cornem commented Feb 19, 2021

This is still not working in preview 4. Here are some details:

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<PackageReference Include="Microsoft.WinUI" Version="3.0.0-preview4.210210.4" />
private async Task LoadFileAsync()
{
    var picker = new FileOpenPicker
    {
        ViewMode = PickerViewMode.Thumbnail,
        SuggestedStartLocation = PickerLocationId.PicturesLibrary
    };

    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".png");

    await picker.PickSingleFileAsync();
}

Calling this method will throw System.Runtime.InteropServices.COMException with this message: Invalid window handle. (0x80070578)".

Edit: I could get it to work by using the solution from this comment.

@wbokkers
Copy link

wbokkers commented Mar 1, 2021

The workaround mentioned by @cornem is not working when using PickMultipleFilesAsync()

See: https://gist.github.com/wbokkers/cc8bcc7bc5a646b82a4d41b337330c69#gistcomment-3649439

@Gavin-Williams
Copy link

This issue is still present in May 2022 - Windows App SDK 1.1.0-preview3. What's the deal?

@castorix
Copy link

This issue is still present in May 2022 - Windows App SDK 1.1.0-preview3. What's the deal?

Which issue ?

WinRT.Interop.InitializeWithWindow.Initialize works fine for me, with PickSingleFileAsync or PickMultipleFilesAsync
(Windows 10 21H1)

@lukedukeus
Copy link

lukedukeus commented May 31, 2022

Using this way, I get the same issue.

This is still not working in preview 4. Here are some details:

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<PackageReference Include="Microsoft.WinUI" Version="3.0.0-preview4.210210.4" />
private async Task LoadFileAsync()
{
    var picker = new FileOpenPicker
    {
        ViewMode = PickerViewMode.Thumbnail,
        SuggestedStartLocation = PickerLocationId.PicturesLibrary
    };

    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".png");

    await picker.PickSingleFileAsync();
}

Calling this method will throw System.Runtime.InteropServices.COMException with this message: Invalid window handle. (0x80070578)".

Edit: I could get it to work by using the solution from this comment.

If i use the fix mentioned above, I get System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.'

I am on windows 11 22000 and using Windows App SDK 1.1.0-preview3

@castorix Can you post your full code?

@lukedukeus
Copy link

Nevermind, this actually was a different issue caused by running the app as admin. See #2504
@Gavin-Williams is this your issue?

@Gavin-Williams
Copy link

Gavin-Williams commented Jun 1, 2022

The interop workaround is working OK. I'm just surprised that the OpenFileDialog doesn't work. The discussion at microsoft/WindowsAppSDK#1887 goes into the steps that MS will take to slightly improve the code steps to workaround over the short-term and mentions that long term (haha - seriously) the API will work as expected.

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps no-issue-activity product-winui3 WinUI 3 issues question
Projects
None yet
Development

No branches or pull requests

10 participants