Gowtham
Gowtham

Reputation: 11

WebView2 Source property CoreWebView2 is null

Namespace: Microsoft.Web.WebView2.WinForms
Assembly: Microsoft.Web.WebView2.WinForms.dll
Package: Microsoft.Web.WebView2 v1.0.902.49
string userDataFolder = Path.GetTempPath() + @"WebView2UserData";
CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();

Webview Intialization

async void InitwebView()
{
    CoreWebView2Environment env = CoreWebView2Environment.CreateAsync("", userDataFolder, options).GetAwaiter().GetResult();
    await webView.EnsureCoreWebView2Async(env);
    webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2Ready;
}

Sending Post Data through webView

 public void SendPostData(string url, string postData, string headers)
    {
     byte[] postDataByte = Encoding.UTF8.GetBytes(postData);
     MemoryStream postStream = new MemoryStream(postDataByte);
     string additionalHeaders = "Content-Type: application/x-www-form-urlencoded" + 
     Environment.NewLine + headers;
     this.webView.Source = new Uri(url);
      

Navigating with WebResourceRequest

var request = webView.CoreWebView2.Environment.CreateWebResourceRequest(url,"POST",postStream, additionalHeaders);           
    webView.CoreWebView2.NavigateWithWebResourceRequest(request);
}

Upvotes: 0

Views: 1354

Answers (0)

Related Questions