Я создаю свое первое настольное приложение Winui 3. У меня есть NavigationView на моем Mainwindow.
У меня есть 9 разных страниц, к которым я перемещаюсь через кадр в навигационном обзоре.
Одна из страниц предназначена для печати отчетов. Мне нужно заставить FilesavePicker работать с «отчетной страницы». Я реализовал ниже, непосредственно после примеров из Learn.microsoft.com. (Я также добавил один и тот же сегмент кода FilePicker ниже в небольшое фиктивное приложение для песчаного бокса Winui-3, но вместо этого я поместил код на код Mainwindow, и оно сработало отлично.) Мне нужно заставить файл QuicePicker для работы со страницы вместо MainWindow.
Спасибо всем за помощь. />
Я знаю, что проблема связана с получением Hwnd.
.// Retrieve the window handle (HWND) of the current WinUI 3 window.
var window = (MainWindow)Application.Current.MainWindow; (I tried this, but it did not work)
Я получаю эту ошибку из вышеуказанной строки:
(я пробовал это, но это не сработало)
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
Я получаю эту ошибку из строки выше:
Я не знаю правильный синтаксис.using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
namespace MetricReporting.Pages
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class pageReports : Page
{
public pageReports()
{
this.InitializeComponent();
}
private void ButtonBoltReport_Click(object sender, RoutedEventArgs e)
{
DisplayBOLTSaveDialog();
}
private async void DisplayBOLTSaveDialog()
{
FileSavePicker savePicker = new FileSavePicker();
// Retrieve the window handle (HWND) of the current WinUI 3 window.
var window = (MainWindow)Application.Current.MainWindow;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
// Initialize the folder picker with the window handle (HWND).
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, hWnd);
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Plain Text", new List() { ".txt" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
CachedFileManager.DeferUpdates(file);
// write to file
await FileIO.WriteTextAsync(file, file.Name);
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
ReportStatus.Text = "File " + file.Name + " was saved.";
}
else
{
ReportStatus.Text = "File " + file.Name + " couldn't be saved.";
}
}
else
{
ReportStatus.Text = "Operation cancelled.";
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/714 ... rom-a-page
Как получить ручку окна текущего Winui 3 MainWindow со страницы в кадре в контроле NavigationView на MainWindow ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение