microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.85k stars 323 forks source link

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

Open optsing opened 1 month ago

optsing commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

@optsing Awesome! Works great, thank you.

michalleptuch commented 1 month ago

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 commented 1 month ago

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>