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

InvalidCastException when trying to get string array from ApplicationData #4705

Open
tipa opened this issue Sep 10, 2024 · 3 comments
Open
Assignees
Labels

Comments

@tipa
Copy link

tipa commented Sep 10, 2024

Describe the bug

When building the app with PublishAot=true, getting a string array from ApplicationData.Current.LocalSettings and casting it to the string[] type I get:
System.InvalidCastException: 'Unable to cast object of type 'WinRT.IInspectable' to type 'System.String[]'.'

Steps to reproduce the bug

Set in csproj:
<PublishAot>true</PublishAot>

ApplicationData.Current.LocalSettings.Values["test"] = new string[] { "a", "b", "c" };
var array = (string[])ApplicationData.Current.LocalSettings.Values["test"];

Expected behavior

No crash

Screenshots

image

NuGet package version

Windows App SDK 1.6.0: 1.6.240829007

Packaging type

Packaged (MSIX)

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

Visual Studio 2022

Additional context

Example project: Test.zip

@fabianoriccardi
Copy link

I think I met the same/related issue on another example (WASDK v1.6, AOT enabled):

SelectionChangedEventArgs args = new([], []);

Throws invalidcastexception: "specified cast is not valid".

@Scottj1s
Copy link
Member

@manodasanW Does this look related to the collection expression marshaling issue?

@tipa
Copy link
Author

tipa commented Sep 11, 2024

I'm also seeing crashes when setting an IEnumerable as ItemSource, like this:
comboBox.ItemsSource = Enumerable.Range(0, 10).Select(x => new TestItem(x));
No crash when using .ToList()
comboBox.ItemsSource = Enumerable.Range(0, 10).Select(x => new TestItem(x)).ToList();

image

If this is a separate issue and you want me to open a new issue, please let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants