Microsoft.Web.WebView2.WinForms.WebView2未加载同一应用程序的第二个副本的视图

fxnxkyjh  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(126)

我有一个简单的桌面应用程序,它在Windows窗体中加载**.cshtml视图。我构建了它,并将其发布到我的远程计算机上的驱动器D上的特定文件夹中。如果只有一个用户从这台计算机上运行目标路径上的此应用程序示例,一切都运行正常。但如果任何其他用户尝试运行同一应用程序,当该应用程序的副本已由另一用户运行时,它看到的是空白表单。
我已经用一些日志和
try{} catch{}**块介绍了代码,但到目前为止还没有有趣的信息。返回桌面视图的服务在同一个Intranet上运行,如果你只是通过浏览器访问URL,它会一直返回结果。
可能是什么问题,我如何才能找到它出现的真正原因?
如有任何建议,我将不胜感激。

更新1CoreWebView 2 InitializationCompleted包含异常-〉请求的资源正在使用。(0x 800700 AA)

#region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.webView = new Microsoft.Web.WebView2.WinForms.WebView2();
            ((System.ComponentModel.ISupportInitialize)(this.webView)).BeginInit();
            this.SuspendLayout();
            // 
            // webView
            // 
            this.webView.AllowExternalDrop = false;
            this.webView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.webView.CreationProperties = null;
            this.webView.DefaultBackgroundColor = System.Drawing.Color.White;
            this.webView.Location = new System.Drawing.Point(10, 10);
            this.webView.Name = "webView";
            this.webView.Size = new System.Drawing.Size(690, 125);
            this.webView.TabIndex = 0;
            this.webView.ZoomFactor = 1D;
            // 
            // MainForm
            // 
            this.AccessibleDescription = "MessageBoard Wrapper";
            this.AccessibleName = "MessageBoard Wrapper";
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(710, 145);
            this.Controls.Add(this.webView);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MinimumSize = new System.Drawing.Size(250, 100);
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Text = "MessageBoard Wrapper";
            ((System.ComponentModel.ISupportInitialize)(this.webView)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

       private HttpClient GetClient()
       {
         var result = new HttpClient();
         return result;
       }

        private Microsoft.Web.WebView2.WinForms.WebView2 webView;

        private string _baseUrl = "here I have my service url";

    public MainForm()
        {
            
            InitializeComponent();
            FormBorderStyle = FormBorderStyle.None;
            DoubleBuffered = true;
            SetStyle(ControlStyles.ResizeRedraw, true);
            var client = GetClient();
                try
                {
                    var targetmUrlForCustomBusinessLogic = "url address here";
                    var response = client.GetAsync(targetmUrlForCustomBusinessLogic).Result;

                    if (response.IsSuccessStatusCode)
                    {
                        var t = Task.Run(() => response.Content.ReadAsStringAsync()).Result;

                     //here I have some code too

                    }

                }
                catch (Exception ex)
                {
                    var message = ex.Message;
                    //TODO: add logging here
                  
                }
                finally
                {
                    client.Dispose();
                }

            webView.Source = new Uri(_baseUrl);

            webView.NavigationCompleted += WebView_NavigationCompleted;

            ResizeEnd += (object sender, EventArgs e) => SaveFormSettings();
            Move += (object sender, EventArgs e) => SaveFormSettings();
            Shown += (object sender, EventArgs e) => LoadFormSettings();
        }

tpgth1q7

tpgth1q71#

我通过进行以下更改使此应用程序在多用户模式下工作。
1.我删除了webView.Source属性的初始化
1.我添加了一个为每个用户准备环境并显式触发webView控件初始化的方法。(我在MainForm内部调用此方法
private async Task InitAsync(string path) { var env = await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(userDataFolder: path); await webView.EnsureCoreWebView2Async(env); }
1.我用下一种方法为用户数据文件夹设置了pathparam
path = Path.Combine(Path.GetTempPath(), $"{Environment.UserName}");
1.我在CoreWebView 2 InitializationCompleted事件处理程序中将导航路径绑定到目标URL。
private void webView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e) { if (webView != null && webView.CoreWebView2 != null) { webView.CoreWebView2.Navigate(_baseUrl); } }
上面的所有步骤都解决了我的问题,这是因为每个新用户的应用程序的新示例都需要环境文件夹来处理webView组件。希望这能为某人保存一些时间。

相关问题