Skip to content
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

System.Runtime.InteropServices.COMException: 'Invalid window handle. (0x80070578) #1415

Open
yegorisB opened this issue Dec 13, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@yegorisB
Copy link

Which version of the app?

WinUI 3 Gallery

Description

Source:
StorageFile file = await savePicker.PickSaveFileAsync();
VS 2022: System.Runtime.InteropServices.COMException: 'Invalid window handle. (0x80070578)
Consider WindowNative, InitializeWithWindow
See https://aka.ms/cswinrt/interop#windows-sdk'

image

Screenshots

image

Windows version

No response

Additional context

Edition Windows 11 Home
Version 23H2
Installed on ‎9/‎11/‎2023
OS build 22631.2861
Experience Windows Feature Experience Pack 1000.22681.1000.0

Processor 13th Gen Intel(R) Core(TM) i9-13900H 2.60 GHz
Installed RAM 16.0 GB (15.6 GB usable)

System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display

@yegorisB yegorisB added the bug Something isn't working label Dec 13, 2023
@yegorisB
Copy link
Author

yegorisB commented Dec 25, 2023

everything is all right if you use this:
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
FileSavePicker picker = new();
picker.SuggestedStartLocation = PickerLocationId.Downloads;

 picker.FileTypeChoices.Add("Rich Text", new List<string>() { ".rtf" });
 

 var hwnd = WindowNative.GetWindowHandle(this);  // App.m_window?
 InitializeWithWindow.Initialize(picker, hwnd);

 StorageFile file = await picker.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
     using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
         await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
     {
         editor.Document.SaveToStream(Microsoft.UI.Text.TextGetOptions.FormatRtf, randAccStream);
     }

     // Let Windows know that we're finished changing the file so the
     // other app can update the remote version of the file.
     FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
     if (status != FileUpdateStatus.Complete)
     {
         Windows.UI.Popups.MessageDialog errorBox =
             new Windows.UI.Popups.MessageDialog("File " + file.Name + " couldn't be saved.");
         await errorBox.ShowAsync();
     }
 }

}

instead of this:

private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Rich Text", new List<string>() { ".rtf" });

// 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
    using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
        await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
    {
        editor.Document.SaveToStream(Microsoft.UI.Text.TextGetOptions.FormatRtf, randAccStream);
    }

    // Let Windows know that we're finished changing the file so the
    // other app can update the remote version of the file.
    FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
    if (status != FileUpdateStatus.Complete)
    {
        Windows.UI.Popups.MessageDialog errorBox =
            new Windows.UI.Popups.MessageDialog("File " + file.Name + " couldn't be saved.");
        await errorBox.ShowAsync();
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant