CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

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

Open optsing opened this issue 1 year ago • 6 comments

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.

  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

Version Info NET: 8.0.401 CsWinRT: 2.1.3 Microsoft.Windows.SDK.NET.Ref: 10.0.22621.45 or 10.0.26100.45

optsing avatar Sep 24 '24 12:09 optsing