-
Notifications
You must be signed in to change notification settings - Fork 309
Update sample for new functionalities of Storage.Picker APIs add in 2.0-exp2 #582
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
base: release/experimental
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -154,6 +154,111 @@ namespace winrt::FilePickersAppSinglePackaged::implementation | |||||
| return filters; | ||||||
| } | ||||||
|
|
||||||
| void MainWindow::AppendChoiceFromJsonPair( | ||||||
| std::wstring_view pairText, | ||||||
| std::vector<std::pair<std::wstring, std::vector<std::wstring>>>& orderedChoices) | ||||||
| { | ||||||
| size_t colonPos = pairText.find(L':'); | ||||||
| if (colonPos == std::wstring_view::npos) | ||||||
| { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| std::wstring key{ pairText.substr(0, colonPos) }; | ||||||
| std::wstring valueStr{ pairText.substr(colonPos + 1) }; | ||||||
|
|
||||||
| key = TrimString(key); | ||||||
| if (key.empty()) | ||||||
| { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| std::vector<std::wstring> values; | ||||||
| size_t arrStart = valueStr.find(L'['); | ||||||
| size_t arrEnd = valueStr.rfind(L']'); | ||||||
| if (arrStart != std::wstring::npos && arrEnd != std::wstring::npos && arrStart < arrEnd) | ||||||
| { | ||||||
| std::wstring arrayContent = valueStr.substr(arrStart + 1, arrEnd - arrStart - 1); | ||||||
| std::wstringstream ss(arrayContent); | ||||||
| std::wstring item; | ||||||
| while (std::getline(ss, item, L',')) | ||||||
| { | ||||||
| auto trimmed = TrimString(item); | ||||||
| if (!trimmed.empty()) | ||||||
| { | ||||||
| values.emplace_back(std::move(trimmed)); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| orderedChoices.emplace_back(std::move(key), std::move(values)); | ||||||
| } | ||||||
|
|
||||||
| // This method parses the text input to preserve its insertion order. | ||||||
| // When developers coding with FileTypeChoices, the order can be directly reflected from code and this parsing is not required. | ||||||
| std::vector<std::pair<winrt::hstring, std::vector<winrt::hstring>>> MainWindow::DeserizeJsonInsertionOrder(std::wstring & jsonStr) | ||||||
| { | ||||||
| // Remove outer braces and whitespace | ||||||
| size_t start = jsonStr.find(L'{'); | ||||||
| size_t end = jsonStr.rfind(L'}'); | ||||||
| if (start == std::wstring::npos || end == std::wstring::npos || start >= end) | ||||||
| { | ||||||
| LogResult(L"Invalid JSON format"); | ||||||
| return {}; | ||||||
| } | ||||||
|
|
||||||
| jsonStr = jsonStr.substr(start + 1, end - start - 1); | ||||||
|
|
||||||
| // Split by comma (handle nested arrays properly) | ||||||
| std::vector<std::pair<std::wstring, std::vector<std::wstring>>> orderedChoices; | ||||||
| size_t pos = 0; | ||||||
| int bracketDepth = 0; | ||||||
| size_t lastPos = 0; | ||||||
|
|
||||||
| while (pos < jsonStr.length()) | ||||||
| { | ||||||
| wchar_t ch = jsonStr[pos]; | ||||||
| if (ch == L'[') bracketDepth++; | ||||||
| else if (ch == L']') bracketDepth--; | ||||||
| else if (ch == L',' && bracketDepth == 0) | ||||||
| { | ||||||
| // Found a top-level comma, process this key-value pair | ||||||
| std::wstring pair = jsonStr.substr(lastPos, pos - lastPos); | ||||||
|
|
||||||
| AppendChoiceFromJsonPair(pair, orderedChoices); | ||||||
|
|
||||||
| lastPos = pos + 1; | ||||||
| } | ||||||
| pos++; | ||||||
| } | ||||||
|
|
||||||
| // Process the last pair | ||||||
| if (lastPos < jsonStr.length()) | ||||||
| { | ||||||
| std::wstring pair = jsonStr.substr(lastPos); | ||||||
| AppendChoiceFromJsonPair(pair, orderedChoices); | ||||||
| } | ||||||
|
|
||||||
| std::vector<std::pair<winrt::hstring, std::vector<winrt::hstring>>> results; | ||||||
|
|
||||||
| // Add choices to picker in original order | ||||||
| for (const auto& choice : orderedChoices) | ||||||
| { | ||||||
| std::vector<winrt::hstring> extensions; | ||||||
| extensions.reserve(choice.second.size()); | ||||||
|
|
||||||
| for (const auto& ext : choice.second) | ||||||
| { | ||||||
| extensions.emplace_back(ext); | ||||||
| } | ||||||
|
|
||||||
| results.emplace_back(hstring{ choice.first }, std::move(extensions)); | ||||||
| LogResult(winrt::hstring{ L"Inserting choice: " + choice.first }); | ||||||
| } | ||||||
|
|
||||||
| return results; | ||||||
| } | ||||||
|
|
||||||
| winrt::fire_and_forget MainWindow::NewPickSingleFile_Click(winrt::Windows::Foundation::IInspectable const&, RoutedEventArgs const&) | ||||||
| { | ||||||
| auto lifetime = get_strong(); | ||||||
|
|
@@ -185,6 +290,27 @@ namespace winrt::FilePickersAppSinglePackaged::implementation | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (IsChecked(OpenPickerFileTypeChoicesCheckBox())) | ||||||
| { | ||||||
| auto jsonText = TrimString(OpenPickerFileTypeChoicesInput().Text().c_str()); | ||||||
| if (!jsonText.empty()) | ||||||
| { | ||||||
| try | ||||||
| { | ||||||
| for (auto& choice : DeserizeJsonInsertionOrder(jsonText)) | ||||||
| { | ||||||
| picker.FileTypeChoices().Insert(choice.first, winrt::single_threaded_vector(std::move(choice.second))); | ||||||
| } | ||||||
| } | ||||||
| catch (winrt::hresult_error const& ex) | ||||||
| { | ||||||
| std::wstring message = L"Error in New FileSavePicker: Unable to parse FileTypeChoices JSON - "; | ||||||
|
||||||
| std::wstring message = L"Error in New FileSavePicker: Unable to parse FileTypeChoices JSON - "; | |
| std::wstring message = L"Error in New FileOpenPicker: Unable to parse FileTypeChoices JSON - "; |
Copilot
AI
Jan 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error message is misleading. The error states "Error in New FileSavePicker" but this code is in the NewPickMultipleFiles_Click handler for FileOpenPicker, not FileSavePicker. The error message should indicate "Error in New FileOpenPicker" instead.
| std::wstring message = L"Error in New FileSavePicker: Unable to parse FileTypeChoices JSON - "; | |
| std::wstring message = L"Error in New FileOpenPicker: Unable to parse FileTypeChoices JSON - "; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,7 +1,10 @@ | ||||||
| #pragma once | ||||||
| #pragma once | ||||||
|
|
||||||
| #include "MainWindow.g.h" | ||||||
|
|
||||||
| #include <string> | ||||||
| #include <string_view> | ||||||
| #include <utility> | ||||||
| #include <vector> | ||||||
|
|
||||||
| namespace winrt::FilePickersAppSinglePackaged::implementation | ||||||
|
|
@@ -20,6 +23,10 @@ namespace winrt::FilePickersAppSinglePackaged::implementation | |||||
| Microsoft::Windows::Storage::Pickers::PickerLocationId GetSelectedNewLocationId(); | ||||||
| Microsoft::Windows::Storage::Pickers::PickerViewMode GetSelectedNewViewMode(); | ||||||
| std::vector<winrt::hstring> GetFileFilters(); | ||||||
| void AppendChoiceFromJsonPair( | ||||||
| std::wstring_view pairText, | ||||||
| std::vector<std::pair<std::wstring, std::vector<std::wstring>>>& orderedChoices); | ||||||
| std::vector<std::pair<winrt::hstring, std::vector<winrt::hstring>>> DeserizeJsonInsertionOrder(std::wstring & jsonStr); | ||||||
|
||||||
| std::vector<std::pair<winrt::hstring, std::vector<winrt::hstring>>> DeserizeJsonInsertionOrder(std::wstring & jsonStr); | |
| std::vector<std::pair<winrt::hstring, std::vector<winrt::hstring>>> DeserializeJsonInsertionOrder(std::wstring & jsonStr); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <packages> | ||
| <package id="Microsoft.Web.WebView2" version="1.0.3179.45" targetFramework="native" /> | ||
| <package id="Microsoft.Web.WebView2" version="1.0.3405.78" targetFramework="native" /> | ||
| <package id="Microsoft.Windows.CppWinRT" version="2.0.240405.15" targetFramework="native" /> | ||
| <package id="Microsoft.Windows.ImplementationLibrary" version="1.0.240803.1" targetFramework="native" /> | ||
| <package id="Microsoft.Windows.SDK.BuildTools" version="10.0.26100.4654" targetFramework="native" /> | ||
| <package id="Microsoft.Windows.SDK.BuildTools.MSIX" version="1.7.20250829.1" targetFramework="native" developmentDependency="true" /> | ||
| <package id="Microsoft.WindowsAppSDK" version="1.8.250916003" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.AI" version="1.8.38" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Base" version="1.8.250831001" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.DWrite" version="1.8.25090401" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Foundation" version="1.8.250906002" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.InteractiveExperiences" version="1.8.250906004" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.ML" version="1.8.2091" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Runtime" version="1.8.250916003" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Widgets" version="1.8.250904007" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.WinUI" version="1.8.250906003" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK" version="2.0.0-experimental3" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.AI" version="2.0.57-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Base" version="2.0.0-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.DWrite" version="2.0.0-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Foundation" version="2.0.8-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.InteractiveExperiences" version="2.0.3-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.ML" version="2.0.44-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Runtime" version="2.0.0-experimental3" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.Widgets" version="2.0.2-experimental" targetFramework="native" /> | ||
| <package id="Microsoft.WindowsAppSDK.WinUI" version="2.0.3-experimental" targetFramework="native" /> | ||
| </packages> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ | |
| <ProjectCapability Include="Msix" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.250907003" /> | ||
| <PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.0-experimental3" /> | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: change to exp4 after it's released. |
||
| </ItemGroup> | ||
|
|
||
| <!-- | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method name has a spelling error. The method name "DeserizeJsonInsertionOrder" should be "DeserializeJsonInsertionOrder" (missing 'ial' in 'serialize').