짜잔! 그냥 문서에 있는대로 코드 짜서 파일피커를 열려고 하면 저 망할 놈의 예외가 뜹니다!
와! 나 분명 문서에 있는대로 했는데!
하지만 다행히도(?) 두 줄만 추가하면 아주 잘 열리죠.
FileOpenPicker picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
/** 추가한 두 줄 */
// Get the current window's HWND by passing in the Window object
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
// Associate the HWND with the file picker
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);
/** 근데 이 코드가 뭘 의미하는 지는 모르겠어요 */
StorageFile file = await picker.PickSingleFileAsync();
아주 잘 열린다.
파일을 선택하면 사진도, 이름도 잘 나옵니다
추가해야 할 두 줄의 출처
https://github.com/microsoft/WindowsAppSDK/issues/1188
How to use FilePicker and FolderPicker in WinUI 3 and other desktop-based apps · Issue #1188 · microsoft/WindowsAppSDK
In desktop-based apps (like WinUI 3 desktop or WPF MSIX), The FileOpenPicker, FileSavePicker, and FolderPicker APIs require a HWND associated with them, so that they know which window to display ov...
github.com
2023.02.20 추가작성
위의 코드는 window를 상속받은 클래스 내에서만 사용할 수 있다.
만일 page에서 사용하고 싶다면,
App.xaml.cs에 있는 private Window m_window => internal Window m_window로 접근제어자를 바꾸고,
//윈도우를 불러와 hwnd에 넣어줍시다.
var window = (Application.Current as App)?.m_window as MainWindow;
또한, 모든 파일을 고르게 하려고 FileTypeFilter를 지워버리면 그것도 그것대로 에러가 납니다.
만일 모든 파일로 설정하려면
picker.FileTypeFilter.Add("*");
로 더해줍시다.
아래 글에서 참고하였습니다
How to retrieve the window handle of the current WinUI 3 MainWindow from a page in a frame in a NavigationView control on the Ma
I am creating my first WinUI 3 desktop application. I have a NavigationView on my MainWindow. I have 9 different pages that I navigate to via the frame in the NavigationView. One of the pages is for
stackoverflow.com
'C# > WinUI3' 카테고리의 다른 글
[WinUI3] 창 테두리 없애기, Title, Icon... 그리고 TitleBar. (2) | 2023.02.05 |
---|---|
[WinUI3] NoResize, 생성 시 창의 크기 (0) | 2023.02.05 |
[WinUI3] Color Picker 사용기 (2) | 2023.01.28 |
[WinUI3] Navigation (0) | 2023.01.27 |
[WinUI3] ToDo리스트 만들기 - 포기 (0) | 2023.01.26 |