question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FileSavePicker.FileTypeChoices throws InvalidCastException

See original GitHub issue

Describe the bug

Trying to execute any methods of folderPicker.FileTypeFilter will throw an exception.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Clone https://github.com/microsoft/WinUI-3-Demos/
  2. Edit 36-45 Lines of MainWindow.xaml.cs to
var savePicker = new FileSavePicker();

//Make folder Picker work in Win32
IntPtr windowHandle = (App.Current as App).WindowHandle;
InitializeWithWindowWrapper initializeWithWindowWrapper = InitializeWithWindowWrapper.FromAbi(savePicker.ThisPtr);
initializeWithWindowWrapper.Initialize(windowHandle);
folderPicker.FileTypeFilter.Add("*");

savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
savePicker.SuggestedFileName = "OUTPUT";
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
textBox.Text = file!= null ? file.Name: string.Empty;
  1. Run the app, click the Browse button.
  2. The app will crash with exception System.InvalidCastException (“Specified cast is not valid”).

| Name | Value | Type – | – | – | – ▶ | savePicker.FileTypeChoices.Add(“Plain Text”, new List<string>() { “.txt” }); | ‘savePicker.FileTypeChoices.Add(“Plain Text”, new List<string>() { “.txt” })’ threw an exception of type ‘System.InvalidCastException’ | void {System.InvalidCastException}

Expected behavior

A file save picker is opened. User can pick a file, then it’s path is shown in textbox.

Screenshots

image

Version Info

NuGet package version: [Microsoft.WinUI 3.0.0-preview2.200713.0]

Windows app type:

UWP Win32
No Yes
Windows 10 version Saw the problem?
Insider Build (20231) Yes
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

When trying to watch savePicker, I can see

| Name | Value | Type – | – | – | – ▶ | FileTypeChoices | ‘savePicker.FileTypeChoices’ threw an exception of type ‘System.InvalidCastException’ | System.Collections.Generic.IDictionary<string, System.Collections.Generic.IList<string>> {System.InvalidCastException}

I guees there is something abnormal with savePicker.FileTypeChoices.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
marb2000commented, Feb 20, 2021

I forgot to say that the demos where updated: https://github.com/microsoft/WinUI-3-Demos/ and I’m no longer see this issue.

Thanks. I’m closing the issue, feel free to reopen it in case I miss something.

1reaction
marb2000commented, Oct 12, 2020

@stevenbrix sure, demo samples will be updated. @KiruyaMomochi I will take a look to this issue to see if it repros in the next Preview.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - UWP FileSavePicker.PickSaveFileAsync() throws ...
So it's now working, I used some code of a Microsoft article (shown below). To be honest I'm struggling to see what the...
Read more >
FileSavePicker.FileTypeChoices Property - Windows
A FilePickerFileTypesOrderedMap object that contains a collection of valid file types (extensions) that the user can use to save a file.
Read more >
C# (CSharp) BitmapPropertySet.Add Examples
public async Task Save(double compression) { var picker = new FileSavePicker(); picker.FileTypeChoices.Add("Obraz JPEG", new[] { ".jpg" }); var file = await ...
Read more >
C# | Let There Be Code
IsSuccessStatusCode); throw new Exception ( « Error when requested Foursquare ... Windows 8 met à disposition les classes FileOpenPicker et FileSavePicker.
Read more >
Windows.Storage.Pickers
To set which file type extensions you want to allow, use the FileTypeFilter property on FileOpenPicker and FolderPicker , and the FileTypeChoices property...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found