简体   繁体   English

从 C# .net 框架 4.7.2 与 Microsoft.Windows.SDK.Contracts 一起使用 FileOpenPicker 时出现“无效窗口句柄”错误,没有 UWP

[英]“Invalid window handle” error when using FileOpenPicker from C# .net framwork 4.7.2 with Microsoft.Windows.SDK.Contracts without UWP

I am trying to use Microsoft.Windows.SDK.Contracts to access the Windows10 API from .net framework WFP application.我正在尝试使用 Microsoft.Windows.SDK.Contracts 从 .net framework WFP 应用程序访问 Windows10 API。 I want to use the FileOpenPicker() to select the image for OCR processing by Windows.Media.Ocr.我想使用 FileOpenPicker() 来选择图像以供 Windows.Media.Ocr 进行 OCR 处理。 But I met the 'Invalid window handle' error when using the picker但是我在使用选择器时遇到了“无效窗口句柄”错误

I found a post which met the similar a link issue with C++/WinRT.我发现了一个帖子,它遇到了与 C++/WinRT 类似的链接问题。 One of the answer point out " The program will crash because the FileOpenPicker looks for a CoreWindow on the current thread to serve as the owner of the dialog. But we are a Win32 desktop app without a CoreWindow."其中一个答案指出“程序会崩溃,因为 FileOpenPicker 在当前线程上寻找一个 CoreWindow 作为对话框的所有者。但我们是一个没有 CoreWindow 的 Win32 桌面应用程序。” I think the root cause is the same.我认为根本原因是一样的。 But I don't know how to fix from the my code based on .net framework side.但我不知道如何从基于 .net 框架端的代码中修复。

public async void Load()
{
    var picker = new FileOpenPicker()
    {
        SuggestedStartLocation = PickerLocationId.PicturesLibrary,
        FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" },
    };

    var file = await picker.PickSingleFileAsync();
    if (file != null)
    {

    }
    else
    {

    }
}

Error message: System.Exception: 'Invalid window handle.(Exception from HRESULT:0x80070578)'错误消息:System.Exception:'无效的窗口句柄。(来自 HRESULT 的异常:0x80070578)'

Create a file with:创建一个文件:

using System;
using System.Runtime.InteropServices;

namespace <standardnamespace>
{
    [ComImport]
    [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IInitializeWithWindow
    {
        void Initialize(IntPtr hwnd);
    }
}

change your code to:将您的代码更改为:

public async void Load()
{
    var picker = new FileOpenPicker()
    {
        SuggestedStartLocation = PickerLocationId.PicturesLibrary,
        FileTypeFilter = { ".jpg", ".jpeg", ".png", ".bmp" },
    };

    ((IInitializeWithWindow)(object)picker).Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);    

    var file = await picker.PickSingleFileAsync();
    if (file != null)
    {

    }
    else
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# UWP LiveSKD 和 OneDrive 使用 FileOpenPicker 访问 - C# UWP LiveSKD and OneDrive access using FileOpenPicker 不能在 uwp c# .net 内核中使用 FileOpenPicker,还有其他方法可以在 ZD0836D75D5DBADA3AF68E53 中上传图片吗? - can't use FileOpenPicker in uwp c# .net core, any alternative ways to upload pictures in uwp? 在.NET C#中调用MQQueueManager构造函数时出现无效的句柄错误 - Invalid handle error when calling MQQueueManager constructor in .NET C# 在MediaElement中打开本地文件,而无需在C#中使用FileOpenPicker - Open local files in MediaElement without using FileOpenPicker in C# C# UWP - 如何修复 FileOpenPicker 和 StorageFile 异步错误? - C# UWP - How do I fix the FileOpenPicker and StorageFile async error? 在应用程序配置文件中找不到名为“BACEntities”的连接字符串。” 当从 .NET 5 Api 调用 'n .NET 框架 4.7.2 库时 - No connection string named 'BACEntities' could be found in the application config file.” when calling 'n .NET Framwork 4.7.2 Library from .NET 5 Api 窗口句柄C#/。NET - Window handle C#/.NET 使用 C# 从 Windows 表单(.NET Core)运行 Microsoft Edge 而不会崩溃 - Running Microsoft Edge with C# From Windows Form (.NET Core) without crashing 如何使用 C#、.NET、实体框架发送电报 - How to send telegram using C#, .NET, Entity Framwork 处理来自C#UWP的js按钮 - Handle js button from C# UWP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM