Open
Description
Description
This issue was discovered by developers developing Windows Apps with AOT enabled:
microsoft/WindowsAppSDK#4743 (comment)
The error 'Specified cast is not valid.' was thrown when developer build App with below code:
picker.FileTypeChoices.Add("Test file", [ ".txt" ]);
With new fix in CsWinRT 2.2, the error can be avoided by specifying a List type, for instance, changing above code into:
picker.FileTypeChoices.Add("Test file", new List<string> { ".txt" });
However, the original code still throws error, the difference is that the new error contains more details:
System.InvalidCastException: 'Failed to create a CCW for object of type 'System.Collections.Generic.List'1[System.String]' for interface with IID '98B9ACC1-4B56-532E-AC73-03D5291CCA90': the specified cast is not valid.'
Reporting to the CsWinRT team for further investigation.
Steps To Reproduce
- in Visual Studio, create a blank Windows App
- in the *.csproj file, add
<WindowsSdkPackageVersion>10.0.26100.45</WindowsSdkPackageVersion> <PublishAot>true</PublishAot>
- update the CsWinRT package to version 2.2.0
- In MainWindows.xaml.cs, override method
myButton_Click
private void myButton_Click(object sender, RoutedEventArgs e) { var picker = new Windows.Storage.Pickers.FileSavePicker(); WinRT.Interop.InitializeWithWindow.Initialize(picker, WinRT.Interop.WindowNative.GetWindowHandle(this)); picker.FileTypeChoices.Add("Test file", [ ".test" ]); picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; myButton.Content = "Clicked"; }
- Debug the app, click button, see the error
- This code can run as a workaround:
picker.FileTypeChoices.Add("Test file", new List<string> { ".txt" });
- for your reference, the FileTypeChoices is IDictionary, whose values are IList
Expected Behavior
The code should run without error
Version Info
CsWinRT 2.2.0
Additional Context
No response
Metadata
Metadata
Assignees
Labels
No labels