Skip to content

Commit 37a5a75

Browse files
committed
add save file dialog usage in demo app
1 parent fc9a03a commit 37a5a75

File tree

2 files changed

+77
-23
lines changed

2 files changed

+77
-23
lines changed

prototype-workingdir/PickerUsageApp/MainWindow.xaml.cpp

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ namespace winrt::PickerUsageApp::implementation
4343
case 0:
4444
message = co_await OpenFileSDKClick(sender, args);
4545
break;
46+
case 1:
47+
message = co_await SaveFileSDKClick();
48+
break;
4649
case 2:
4750
message = co_await OpenFolderSDKClick();
4851
break;
@@ -69,6 +72,9 @@ namespace winrt::PickerUsageApp::implementation
6972
case 0:
7073
message = co_await OpenFileUWPClick(sender, args);
7174
break;
75+
case 1:
76+
message = co_await SaveFileUWPClick();
77+
break;
7278
case 2:
7379
message = co_await OpenFolderUWPClick();
7480
break;
@@ -95,7 +101,7 @@ namespace winrt::PickerUsageApp::implementation
95101
winrt::Windows::Storage::Pickers::FileOpenPicker picker{};
96102

97103
picker.as<::IInitializeWithWindow>()->Initialize(hWnd);
98-
SetPickerOptions<winrt::Windows::Storage::Pickers::FileOpenPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
104+
SetOpenPickerOptions<winrt::Windows::Storage::Pickers::FileOpenPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
99105
picker.ViewMode(Convert(m_ViewMode));
100106

101107
if (!m_MultipleSelect)
@@ -119,6 +125,33 @@ namespace winrt::PickerUsageApp::implementation
119125
co_return L"no selection";
120126
}
121127

128+
Windows::Foundation::IAsyncOperation<hstring> MainWindow::SaveFileUWPClick()
129+
{
130+
auto windowNative = this->m_inner.as<IWindowNative>();
131+
HWND hWnd = nullptr;
132+
check_hresult(windowNative->get_WindowHandle(&hWnd));
133+
134+
winrt::Windows::Storage::Pickers::FileSavePicker picker{};
135+
picker.FileTypeChoices().Insert(L"Plain Text", winrt::single_threaded_vector<hstring>({ L".txt" }));
136+
137+
picker.as<::IInitializeWithWindow>()->Initialize(hWnd);
138+
SetPickerOptions<winrt::Windows::Storage::Pickers::FileSavePicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
139+
140+
if (!m_MultipleSelect)
141+
{
142+
auto& file = co_await picker.PickSaveFileAsync();
143+
if (file != nullptr)
144+
{
145+
co_return file.Path();
146+
}
147+
}
148+
else
149+
{
150+
co_return L"File Save Picker does not support multi selection";
151+
}
152+
co_return L"no selection";
153+
}
154+
122155
winrt::Windows::Foundation::IAsyncOperation<hstring> MainWindow::OpenFolderUWPClick()
123156
{
124157
auto windowNative = this->m_inner.as<IWindowNative>();
@@ -128,7 +161,7 @@ namespace winrt::PickerUsageApp::implementation
128161
winrt::Windows::Storage::Pickers::FolderPicker picker{};
129162

130163
picker.as<::IInitializeWithWindow>()->Initialize(hWnd);
131-
SetPickerOptions<winrt::Windows::Storage::Pickers::FolderPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
164+
SetOpenPickerOptions<winrt::Windows::Storage::Pickers::FolderPicker, winrt::Windows::Storage::Pickers::PickerLocationId>(picker);
132165
picker.ViewMode(Convert(m_ViewMode));
133166

134167
if (!m_MultipleSelect)
@@ -141,7 +174,7 @@ namespace winrt::PickerUsageApp::implementation
141174
}
142175
else
143176
{
144-
co_return L"Folder multi selection is not support";
177+
co_return L"Folder Picker does not support multi selection";
145178
}
146179
co_return L"no selection";
147180

@@ -153,8 +186,7 @@ namespace winrt::PickerUsageApp::implementation
153186
auto id = AppWindow().Id();
154187
winrt::Microsoft::Storage::Pickers::FileOpenPicker picker{ id };
155188

156-
SetPickerOptions<winrt::Microsoft::Storage::Pickers::FileOpenPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
157-
//SetPickerOptions(picker);
189+
SetOpenPickerOptions<winrt::Microsoft::Storage::Pickers::FileOpenPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
158190
picker.ViewMode(m_ViewMode);
159191
if (!m_MultipleSelect)
160192
{
@@ -177,12 +209,33 @@ namespace winrt::PickerUsageApp::implementation
177209
co_return L"no selection";
178210
}
179211

212+
Windows::Foundation::IAsyncOperation<hstring> MainWindow::SaveFileSDKClick()
213+
{
214+
auto id = AppWindow().Id();
215+
winrt::Microsoft::Storage::Pickers::FileSavePicker picker{ id };
216+
217+
SetPickerOptions<winrt::Microsoft::Storage::Pickers::FileSavePicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
218+
if (!m_MultipleSelect)
219+
{
220+
auto& file = co_await picker.PickSaveFileAsync();
221+
if (file != nullptr)
222+
{
223+
co_return file.Path();
224+
}
225+
}
226+
else
227+
{
228+
co_return L"FileSavePicker does not support multi selection";
229+
}
230+
co_return L"no selection";
231+
}
232+
180233
winrt::Windows::Foundation::IAsyncOperation<hstring> MainWindow::OpenFolderSDKClick()
181234
{
182235
auto id = AppWindow().Id();
183236
winrt::Microsoft::Storage::Pickers::FolderPicker picker{ id };
184237

185-
SetPickerOptions<winrt::Microsoft::Storage::Pickers::FolderPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
238+
SetOpenPickerOptions<winrt::Microsoft::Storage::Pickers::FolderPicker, winrt::Microsoft::Storage::Pickers::PickerLocationId>(picker);
186239
picker.ViewMode(m_ViewMode);
187240
if (!m_MultipleSelect)
188241
{
@@ -239,8 +292,6 @@ void winrt::PickerUsageApp::implementation::MainWindow::ViewModeSelectionChanged
239292
default:
240293
break;
241294
}
242-
243-
244295
}
245296

246297

@@ -274,5 +325,3 @@ void winrt::PickerUsageApp::implementation::MainWindow::PickerTypeChanged(winrt:
274325
{
275326
m_PickerTypeIndex = sender.as<Microsoft::UI::Xaml::Controls::RadioButtons>().SelectedIndex();
276327
}
277-
278-

prototype-workingdir/PickerUsageApp/MainWindow.xaml.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ namespace winrt::PickerUsageApp::implementation
1818
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFileSDKClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
1919
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFileUWPClick(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
2020

21+
winrt::Windows::Foundation::IAsyncOperation<hstring> SaveFileUWPClick();
22+
winrt::Windows::Foundation::IAsyncOperation<hstring> SaveFileSDKClick();
23+
2124
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFolderSDKClick();
2225
winrt::Windows::Foundation::IAsyncOperation<hstring> OpenFolderUWPClick();
2326

@@ -63,36 +66,38 @@ namespace winrt::PickerUsageApp::implementation
6366
picker.SettingsIdentifier({});
6467
break;
6568
}
66-
switch (m_FilterTypeIndex)
69+
switch (m_PickerLocationIdIndex)
6770
{
6871
case 1:
69-
picker.FileTypeFilter().Append(L".jpg");
70-
picker.FileTypeFilter().Append(L".png");
72+
picker.SuggestedStartLocation(TPickerLocationId::DocumentsLibrary);
7173
break;
7274
case 2:
73-
picker.FileTypeFilter().Append(L".jpg");
74-
picker.FileTypeFilter().Append(L".png");
75-
picker.FileTypeFilter().Append(L".json");
75+
picker.SuggestedStartLocation(TPickerLocationId::Desktop);
7676
break;
7777
default:
78-
picker.FileTypeFilter().Append(L"*");
7978
break;
8079
}
81-
switch (m_PickerLocationIdIndex)
80+
}
81+
82+
template<typename TPicker, typename TPickerLocationId> void SetOpenPickerOptions(TPicker picker)
83+
{
84+
SetPickerOptions<TPicker, TPickerLocationId>(picker);
85+
switch (m_FilterTypeIndex)
8286
{
8387
case 1:
84-
picker.SuggestedStartLocation(TPickerLocationId::DocumentsLibrary);
88+
picker.FileTypeFilter().Append(L".jpg");
89+
picker.FileTypeFilter().Append(L".png");
8590
break;
8691
case 2:
87-
picker.SuggestedStartLocation(TPickerLocationId::Desktop);
92+
picker.FileTypeFilter().Append(L".jpg");
93+
picker.FileTypeFilter().Append(L".png");
94+
picker.FileTypeFilter().Append(L".json");
8895
break;
8996
default:
97+
picker.FileTypeFilter().Append(L"*");
9098
break;
9199
}
92-
93-
94100
}
95-
96101
};
97102
}
98103

0 commit comments

Comments
 (0)