簡體   English   中英

WebView2 控件在調試 WPF 項目時有效,但在通過 vdproj 安裝程序安裝后無效

[英]WebView2 control works while debugging a WPF project, but not after installed via vdproj installer

下面的解決方案,感謝 Poul Bak 和 GrooverFromHolland 指出我糟糕的異步實現,這有點幫助。 我只是在等待之后將源分配移動到相同的方法中,因此操作順序是正確的,並且還必須首先等待 CoreWebView2Environment.CreateAsync。 不完全確定為什么從安裝位置需要這樣做。

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitWebviewAsync();
        }

private async void InitWebviewAsync()
        {
            var userDataFolder = Setting.ExeShortcutPath + "iGo360UtilitySettingWPF.exe.WebView2";
            var env = await CoreWebView2Environment.CreateAsync(null, userDataFolder);

            if (webView != null)
            {
                await webView.EnsureCoreWebView2Async(env);
                string url = AuthHelper._loginUrl;
                webView.Source = new Uri(url);
            }
        }

原始問題發布如下:

我目前有一個 WPF 項目與 WebView2 控件完美配合,同時使用 Visual Studio 2019 (16.6.1) 和 Target .NET Framework 4.6.1 調試應用程序。

還安裝了以下內容:

  1. Microsoft Edge WebView2 運行時 (90.0.818.42)
  2. 微軟邊緣 (90.0.818.42)

我遇到問題的地方是當我通過 vdproj 安裝這個項目並運行應用程序時。 請注意,這與調試環境位於同一台 PC 上,這似乎更奇怪。

在寫這個問題時,我確定了我在下面提到的錯誤的原因。 為了解決這些錯誤,我從調試文件夾中復制了以下內容:

  1. runtimes\win-x86\native\WebView2Loader.dll(這解決了 DLLNotFoundException)
  2. iGo360UtilitySettingWPF.exe.WebView2(這解決了一個新錯誤,邊緣無法寫入目錄)
  3. 在 vdproj 中包含和管理這兩個目錄的最佳方法是什么,因此不必手動將它們復制或添加到我的項目中?

現在我當前的錯誤在等待行 inz InitAsync:

System.Runtime.InteropServices.COMException: '此操作返回,因為超時期限已過。 (來自 HRESULT 的異常:0x800705B4)'

我最初遇到但在上面解決的錯誤:當 WebView2 控件嘗試加載時,應用程序崩潰。 這是它在 InitAsync 中的 await 行上崩潰的構造和 init 方法:

public Login()
{
    string url = AuthHelper._loginUrl;

    InitializeComponent();

    InitAsync();

    if (webView != null)
    {
        webView.Source = new Uri(url);
        //webView.NavigateToString(url);
    }
}

private async void InitAsync()
{
    await webView.EnsureCoreWebView2Async(null);
}

並且事件查看器記錄以下錯誤:

應用程序:MyWebView2Wpf.exe 框架版本:v4.0.30319 描述:進程因未處理的異常而終止。 Exception Info: System.DllNotFoundException at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateCoreWebView2EnvironmentWithOptions(System.String, System.String, Microsoft.Web.WebView2.Core.Raw.ICoreWebView2EnvironmentOptions, Microsoft.Web.WebView2.Core.Raw.ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler ) 在 Microsoft.Web.WebView2.Core.CoreWebView2Environment+d__3.MoveNext() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndgerNotification(System.Debuging System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(System.Threading.Tasks.Task) 在 Microsoft.Web.WebView2.Wpf.WebView2+() Runtime.CompilerServic es.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) 在 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 在 MyWebView2Wpf.Login+ d__4.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__6_0(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading .ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)在 MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(系統 em.Object) 在 System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) 在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading。 ContextCallback, System.Object, Boolean) 在 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) 在 MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System. Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr , Boolean ByRef) 在 MS.Win32.HwndWrapper.WndProc (IntPtr, Int32, IntPtr, IntPtr, Z272 26C864BAC7454A8504F8EDB15D95BZ ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System. Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr,Int32,IntPtr,IntPtr)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) at System.Windows.Threading.Dispatcher.PushFrame(System. Windows.Threading.DispatcherFrame) at System.Windows.Application.RunDispatcher(System.Object) at System.Windows.Application.RunInternal(System.Windows.Window) at System.Windows.Application.Run(System.Windows.Window) at System.Windows.Application.Run() 在 MyWebView2Wpf.App.Main()

在初始化組件()之后; 你調用 InitAsync。 那時可能尚未加載 window 或 WPF 頁面。 最好在 Loaded 事件中初始化它:"

 private async void Login_Loaded(object sender, RoutedEventArgs e)
        {
            if (webView != null)
            {
                try
                {
                    await webView.EnsureCoreWebView2Async(null);
                    webView.Source = new Uri("https://etc");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

等待 CoreWebView2Environment.CreateAsync(null, userDataFolder); 在等待 webView.EnsureCoreWebView2Async 之前是必要的

並確保 webView.Source 僅在 webView.EnsureCoreWebView2Async 完成執行后才被調用。

此外,請確保正確設置了數據文件夾權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM