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

Windows.Storage.Pickers.FileSavePicker.FileTypeChoices.Add throw System.InvalidCastException: 'Specified cast is not valid.' with PublishAOT=true #4743

Open
optsing opened this issue Sep 24, 2024 · 7 comments

Comments

@optsing
Copy link

optsing commented Sep 24, 2024

Describe the bug

Windows.Storage.Pickers.FileSavePicker.FileTypeChoices.Add throw System.InvalidCastException: 'Specified cast is not valid.' with PublishAOT=true
With PublishAOT=false everything works fine.

Steps to reproduce the bug

  1. Create new WinAppSDK project
  2. Add <WindowsSdkPackageVersion>10.0.22621.45</WindowsSdkPackageVersion> and <PublishAot>true</PublishAot> to csproj
  3. Add to myButton_Click
var picker = new Windows.Storage.Pickers.FileSavePicker();
WinRT.Interop.InitializeWithWindow.Initialize(picker, WinRT.Interop.WindowNative.GetWindowHandle(this));
picker.FileTypeChoices.Add("Text file", [".txt"]);
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
var file = await picker.PickSaveFileAsync();

Expected behavior

After clicking on the button, the Save File dialog box should be displayed

Screenshots

No response

NuGet package version

Windows App SDK 1.6.0: 1.6.240829007

Packaging type

Packaged (MSIX)

Windows version

Insider Build (xxxxx)

IDE

Visual Studio 2022-preview

Additional context

No response

@mryderie
Copy link

mryderie commented Oct 2, 2024

Can anybody suggest a workaround for this? Our project needs both the file save picker and AOT, so this bug is a showstopper for us.

@optsing
Copy link
Author

optsing commented Oct 3, 2024

Can anybody suggest a workaround for this? Our project needs both the file save picker and AOT, so this bug is a showstopper for us.

You can call the file save picker via win32 api as described at the end of this article: https://learn.microsoft.com/en-us/uwp/api/windows.storage.pickers.filesavepicker?view=winrt-26100

To make the CsWin32 package work with AOT you need to specify “allowMarshaling”: false in NativeMethods.json and correct the original example a bit.

@mryderie
Copy link

mryderie commented Oct 3, 2024

@optsing Awesome! Works great, thank you.

@michalleptuch
Copy link

michalleptuch commented Oct 18, 2024

I had the same problem and modified this line:

fileSavePicker.FileTypeChoices.Add("Test file", new List<string> { ".test" });

to this:

fileSavePicker.FileTypeChoices.Add("Test file", new[] { ".test" });

and it works.

More info:
<WindowsSdkPackageVersion>10.0.26100.45</WindowsSdkPackageVersion>
<PublishAot>true</PublishAot>
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.6" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />

@optsing
Copy link
Author

optsing commented Oct 18, 2024

I had the same problem and modified this line:

fileSavePicker.FileTypeChoices.Add("Test file", new List<string> { ".test" });

to this:

fileSavePicker.FileTypeChoices.Add("Test file", new[] { ".test" });

and it works.

More info: <WindowsSdkPackageVersion>10.0.26100.45</WindowsSdkPackageVersion> <PublishAot>true</PublishAot> <PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.1.6" /> <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" /> <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />

Does it work in a new blank project? I have it working in a large project, even with collection expression, but it doesn't work in a smaller or empty one, even with string array or List<string>

@michalleptuch
Copy link

michalleptuch commented Dec 7, 2024

@optsing

Does it work in a new blank project? I have it working in a large project, even with collection expression, but it doesn't work in a smaller or empty one, even with string array or List

Unfortunately no, it doesn't. I started migrating one of my apps from .NET Native to .NET 9 (UWP) and my workaround doesn't work at all.

I haven’t checked it in an empty Windows App SDK project yet.

@optsing
Copy link
Author

optsing commented Dec 8, 2024

@optsing

Does it work in a new blank project? I have it working in a large project, even with collection expression, but it doesn't work in a smaller or empty one, even with string array or List

Unfortunately no, it doesn't. I started migrating one of my apps from .NET Native to .NET 9 (UWP) and my workaround doesn't work at all.

I haven’t checked it in an empty Windows App SDK project yet.

This is a common bug for UWP NET and WindowsAppSDK and was solved in CSWinRT 2.2. So the actual solution is to add a reference to the CSWinRT package version >= 2.2 for both types of projects.

I think this issue should be open as long as this bug is present in a blank project.

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