Can WebView2 currently receive Keyboard inputs - wpf

I am trying to create a software in WPF which hosts a browser (WebView2 currently 1.0.818.41) and also show a OnScreenKeyboard when there is a input field focused in the browser.
I have done this kind of stuff with CefSharp in WPF before but I cannot do it with WebView2 currently. My problem is I do not find a way to send keystrokes from the OnScreenKeyboard (or from the WPF Window) to the Browser.
In CefSharp there we have a function called ChromiumWebBrowser.GetHost().SendKeyEvent() but I cannot find something similar in WebView2.
Am I blind or is this something which is currently not implemented (or maybe not planed)?
Thank you in advance!

There is no direct way. What can be done is execute some JS, which in turn posts a message to WebView. This message can then be caught back in wv2_WebMessageReceived event.
There is extensive documentation on the interop between.NET and JS and interop between JS and .NET WPF Forms here.
A solution would be to inject a sendMessage JS function in the NavigationStarting event:
private void wv2_NavigationStarting(Microsoft.UI.Xaml.Controls.WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs args){
var sc = "function sendMessage(txt) { window.chrome.webview.postMessage(txt); }";
wv2.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(sc);
}
Now you collect input fields and add onfocus and onblur events to these input fields for example in the NavigationCompleted event like this:
private void wv2_NavigationCompleted(Microsoft.UI.Xaml.Controls.WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs args){
string script = "const collection ="+
"document.getElementsByTagName(\"input\");" +
"for (let i = 0; i < collection.length; i++){" +
"collection[i].onfocus= ()=>{ sendMessage('onFocus('+collection[i].name')'); }; " +
"collection[i].onblur= (ev)=>{ sendMessage('onBlur('+collection[i].name')'); };"+
"}";
sender.ExecuteScriptAsync(script);
}
Now catch the message in the wv2_WebMessageReceived event:
private void wv2_WebMessageReceived(Microsoft.UI.Xaml.Controls.WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2WebMessageReceivedEventArgs args)
{
var postMess = args.TryGetWebMessageAsString();
if (postMess == "onFocus(nameOfField)" )
{
// here activate the button(keyboard)
// store the Name on focusField variable
}
if (postMess == "onBlur" && paneShown)
{
// here deactivate the button(keyboard)
// release the focusField
}
}
Now you can send a click event to the input fields:
private void btn_Clicked(Object sender, EventArgs args)
{
var script = "var field "+
"= document.getElementsByName("+focusField+");" +
" field.value+=field.value"+args.keyValue();
wv2.CoreWebView2.ExecuteScriptAsync(script);
}
wv2 is an instance of WebView2 and the code is typed directly here and not compiled. Hope you get the idea.

Related

Why I cannot Navigate the html file by using WebView inside of the App in Wpf?

I make a Wpf projcect which demos how to use WebView to Navigate a html file inside of the App, but fails.
The main cs file code is below:
public MainWindow()
{
this.InitializeComponent();
this.wv.ScriptNotify += Wv_ScriptNotify;
this.Loaded += MainPage_Loaded;
}
private async void Wv_ScriptNotify(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlScriptNotifyEventArgs e)
{
//await (new MessageDialog(e.Value)).ShowAsync();
textBlock.Text = e.Value;
//返回结果给html页面
await this.wv.InvokeScriptAsync("recieve", new[] { "hehe, 我是个结果" });
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//我们事先写好了一个本地html页面用来做测试
this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
//this.wv.Source = new Uri("http://www.baidu.com");
}
And the html file index.html is inside of the project, located at Assets/index.html. Its source code is here:
https://github.com/tomxue/WebViewIssueInWpf/raw/master/WpfApp3/Assets/index.html
I put the project code onto GitHub: https://github.com/tomxue/WebViewIssueInWpf.git
If the project works well, when WebView visits the inner html file, it should show a button at first.
But I saw nothing.
More:
According to the accepted answer(Thank to Pavel Anikhouski), I changed my code as below and it now works.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//我们事先写好了一个本地html页面用来做测试
//this.wv.Source = new Uri("ms-appx-web://Assets/index.html");
//this.wv.Source = new Uri("http://www.baidu.com");
var html = File.ReadAllText("../../Assets\\index.html");
wv.NavigateToString(html);
}
It seems to be a known issue with WebView control in WindowsCommunityToolkit
You can use only absolute URIs to resources in members of the WebView control that accept string paths.
WebView controls don't recognize the ms-appx:/// prefix, so they can't read from the package (if you've created a package for your
application).
WebView controls don't recognize the File:// prefix. If you want to read a file into a WebView control, add code to your application that
reads the content of the file. Then, serialize that content into a
string, and call the NavigateToString(String) method of the WebView
control.
So, instead of loading a file this.wv.Source = new Uri("ms-appx-web://Assets/index.html"); try to read a local file and then navigate to the string
var html = File.ReadAllText("Assets\\index.html");
this.wv.NavigateToString(html);
It should work fine (I've seen the button and message at my end). Also, don't forget to copy Assets\index.html to the output directory (set Copy Always or Copy if newer)

Accessing document elements when using Windows.Forms.WebBrowser

I'm new to automating webpage access, so forgive what is probably a remedial question. I'm using C#/Windows.Forms in a console app. I need to programmatically enter the value of an input on a webpage that I cannot modify and that is running javascript. I have successfully opened the page (triggering WebBrowser.DocumentCompleted). I set browser emulation mode to IE11 (in registry), so scripts run without errors. When DocumentCompleted() triggers, I am unable to access the document elements without first viewing the document content via MessageBox.Show(), which is clearly not acceptable for my unattended app.
What do I need to do so that my document elements are accessbile in an unattended session (so I can remove MessageBox.Show() from the code below)? Details below. Thank you.
The input HTML is:
<input class="input-class" on-keyup="handleKeyPress($key)" type="password">
My DocumentCompleted event handler is:
private static void LoginPageCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = ((WebBrowser)sender);
var document = wb.Document;
// I'm trying to eliminate these 3 lines
var documentAsIHtmlDocument = (mshtml.IHTMLDocument)document.DomDocument;
var content = documentAsIHtmlDocument.documentElement.innerHTML;
MessageBox.Show(content);
String classname = null;
foreach (HtmlElement input in document.GetElementsByTagName("input"))
{
classname = input.GetAttribute("className");
if (classname == "input-class")
{
input.SetAttribute("value", password);
break;
}
}
}
The problem for me was that the page I'm accessing is being created by javascript. Even though documentComplete event was firing, the page was still not completely rendered. I have successfully processed the first page by waiting for the document elements to be available and if not available, doing Application.DoEvents(); in a loop until they are, so I know now that I'm on the right track.
This SO Question helped me: c# WebBrowser- How can I wait for javascript to finish running that runs when the document has finished loading?
Note that checking for DocumentComplete does not accurately indicate the availability of the document elements on a page generated by javascript. I needed to keep checking for the elements and running Application.DoEvents() until they became available (after the javascript generated them).
If the problem comes from the creation of a STAThread, necessary to instantiate the underlying Activex component of WebBrowser control, this is
a modified version of Hans Passant's code as shown in the SO Question you linked.
Tested in a Console project.
class Program
{
static void Main(string[] args)
{
NavigateURI(new Uri("[SomeUri]", UriKind.Absolute), "SomePassword");
Console.ReadLine();
}
private static string SomePassword = "SomePassword";
private static void NavigateURI(Uri url)
{
Thread thread = new Thread(() => {
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate(url);
Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
protected static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = ((WebBrowser)sender);
if (browser.Url == e.Url)
{
while (browser.ReadyState != WebBrowserReadyState.Complete)
{ Application.DoEvents(); }
HtmlDocument Doc = browser.Document;
if (Doc != null)
{
foreach (HtmlElement input in Doc.GetElementsByTagName("input"))
{
if (input.GetAttribute("type") == "password")
{
input.InnerText = SomePassword;
//Or
//input.SetAttribute("value", SomePassword);
break;
}
}
}
Application.ExitThread();
}
}
}

How to stop the running wcf services in silverlight when exception happens

After some digging into exception handling in silverlight and reading some useful blogs like this
Silverlight exception handling using WCF RIA Services and WCF Services I ended up implementing similar idea in the App.xaml.cs to show an error page and call another wcf service method to log the error to the event viewer:
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
{
var errorPage = new Error();
errorPage.Show();
string errorMsg = string.Format("{0} {1}", e.ExceptionObject.Message, e.ExceptionObject.StackTrace);
EventHandler<WriteIntoEventLogCompletedEventArgs> callback = (s, ev) =>
{
bool result = ev.Result;
};
(new ServiceProxy<ApplicationServiceClient>()).CallService<WriteIntoEventLogCompletedEventArgs>(callback, errorMsg);
e.Handled = true;
}
}
and this is what I have in Error.xaml.cs:
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
that basically will close the error page when user clicks on OK.
Everything works fine most of the cases.The problem happens when one of the callbacks to the wcf service cause an exception.The error page will be shown nicely and when user clicks ok, error page will get closed. But the background is still showing the busy indicator and the original service callback is still waiting for the response.I need to somehow terminate it.
I would be appriciative if anybody could help.
Thanks,
Sil
--
Thanks a lot for your helpful reply.I used the same idea and in the original service callback method added a code to check e.Error and if it is not null,close the window(it is a child window) with the busyindicator and everything works perfect now. Thanks again. Sil
My guess is that the original service callback may be completing but in an error condition. You may need to detect the error condition and set the IsBusy property of the busyindicator back to False.
Couple of things to check
Is the original service callback atleast returning successfully? You can check this by placing a breakpoint into the original service callback method.
Have you correctly handled the error condition in your callback method. For example -
void proxy_GetUserCompleted(object sender, GetUserCompletedEventArgs e)
{
if (e.Error != null)
{
getUserResult.Text = "Error getting the user.";
}
else
{
getUserResult.Text = "User name: " + e.Result.Name + ", age: " + e.Result.Age + ", is member: " + e.Result.IsMember;
}
}
Reference - http://msdn.microsoft.com/en-us/library/cc197937(v=VS.95).aspx

Win forms, log all clicks?

Is there a way to log all of the clicks in a Win Forms application? I'd like to intercept clicks and record the action and the name of the control that caused it.
Is this possible?
Thanks in advance.
UPDATE: I'm looking for an application wide solution, is there no way to add a listener to the windows event queue (or what ever it is called)?
You can do this by having your app's main form implement the IMessageFilter interface. You can screen the Window messages it gets and look for clicks. For example:
public partial class Form1 : Form, IMessageFilter {
public Form1() {
InitializeComponent();
Application.AddMessageFilter(this);
this.FormClosed += (o, e) => Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m) {
if (m.Msg == 0x201 || m.Msg == 0x203) { // Trap left click + double-click
string name = "Unknown";
Control ctl = Control.FromHandle(m.HWnd);
if (ctl != null) name = ctl.Name;
Point pos = new Point(m.LParam.ToInt32());
Console.WriteLine("Click {0} at {1}", name, pos);
}
return false;
}
}
Note that this logs all clicks in any window of your app.
You could use Spy++ or WinSpy++ to achieve this.
alt text http://www.catch22.net/sites/default/files/images/winspy1.img_assist_custom.jpg
But I'm not sure how you can achieve the same thing yourself. If it's possible you'd need to do it via a low-level Windows API hook or a message handler that gives you access to all the message in your applications queue.
Well, you could subscribe to the Click or MouseDown event of every control on the form.
use MouseEventArgs like this:
private void Form_MouseDown(object sender, System.WinForms.MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
MessageBox.Show(this,"Left Button Click");
break;
case MouseButtons.Right:
MessageBox.Show(this,"Right Button Click" );
break;
case MouseButtons.Middle:
break;
default:
break;
}
EventLog.WriteEntry("source", e.X.ToString() + " " + e.Y.ToString()); //or your own Log function
}
The NunitForms test project has a recorder application that watches for this and many other events. The code is very clever and worth a good look. It's a ThoughtWorks project.
That's the rolls Royce solution though!...
Try recursively walking the Controls collection of the form and subscibe to the event based on the type.
PK :-)

Silverlight 3 Out of the Browser Updates

I have a few users that are using a silverlight app that aren't recieving updates when a new release is published. Isn't this suppose to be automatic or perhaps I'm missing an option somewhere? I was also starting to think that maybe the XAP file is cached and I some how need to prevent that.
Any thoughts out there?
You need to write a few lines of code.
If you're familiar with 'one click' deployment then some of the options you're used to don't exist in Silverlight. You need to write the code yourself.
http://nerddawg.blogspot.com/2009/07/silverlight-out-of-browser-apps-how.html
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
if (Application.Current.IsRunningOutOfBrowser)
{
Application.Current.CheckAndDownloadUpdateAsync();
}
and then in your App() constructor :
Application.Current.CheckAndDownloadUpdateCompleted +=
new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
and an event handler :
void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
// http://nerddawg.blogspot.com/2009/07/silverlight-out-of-browser-apps-how.html
if (e.UpdateAvailable)
{
MessageBox.Show("The application has been updated! Please close and reopen it to load the new version.");
}
else if (e.Error != null && e.Error is PlatformNotSupportedException)
{
MessageBox.Show("An application update is available, " +
"but it requires a new version of Silverlight. " +
"Please contact tech support for further instructions.");
}
}
It only auto updates if the developer performs the CheckAndDownloadUpdateAsync() call. See updates: http://timheuer.com/blog/archive/2009/07/10/silverlight-3-released-what-is-new-and-changed.aspx#oob

Resources