CsWinRT
CsWinRT copied to clipboard
Windows.Storage.Pickers.FileSavePicker.FileTypeChoices.Add throw System.InvalidCastException: 'Specified cast is not valid.' with PublishAOT=true
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.
- Create new WinAppSDK project
- Add
<WindowsSdkPackageVersion>10.0.22621.45</WindowsSdkPackageVersion>and<PublishAot>true</PublishAot>to csproj - 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