How to refresh WebView from code behind in .NET MAUI? - maui

I want to refresh webview from the code behind like I can do it when I pressed F5 key. Is it possible execute F5 key command in the code behind for webview control in .NET MAUI?

from the docs: WebView has a Reload method
WebView webView = new WebView();
...
webView.Reload();

I did this by adding RelayCommand. Passing reference to the WebView as command parameter. (I know it is wrong to do it). And calling Reload() method.
The command was bound to a button in my case. But you can try binding it to Event, using EventToCommandBehavior from CommunityToolkit.MAUI.
First EventHandler code is executed, then the Command.

Related

ICommand not binding in Loaded event using MVVMLight framework

I am wondering why binding a button inside the Loaded event in WPF page does not work and will only work after navigating to another page, and going back.
I have an inventory app and on the main page, most of the ViewModel are called because of a Back button which goes back to a specific lists and what causes that is, it will start binding the even if that command is not for that page and it will also load the collections for other pages.
So I used Loaded page event to call the necessary methods to populate the lists and also start binding commands for this specific page. I also used Unloaded page event for clean up like unsubscribing to some CRUD events.
The problem now though is, buttons does not get binding in Loaded page event. I do not know why..
I have made a miniature app to demo the problem. It can be downloaded here
(full source code included)
https://www.dropbox.com/s/qzumzyicuvrktsi/ICommandTest.zip?dl=0
This is because your views are not getting notified about the change of Command_ShowAddWindow and Command_ClickMe. Let me explain:
When your Page constructor is first run the bindings to your commands are initialized and transferred to the view, but by that time your commands are null, so the view binds both buttons' commands to null.
Then when your Loaded event is fired the commands are initialized, but the view is not getting notified about it, so it keeps command bindings to null.
The solutions to the problem are:
You manually call RaisePropertyChanged to notify the view about commands change when you initialize them:
void InitCommands()
{
Command_ShowAddWindow = new RelayCommand(Command_ShowAddWindow_Click);
Command_ClickMe = new RelayCommand(Command_ClickMe_Click);
RaisePropertyChanged("Command_ShowAddWindow");
RaisePropertyChanged("Command_ClickMe");
}
Or you initialize your commands in your ViewModel constructor before DataBindings are initialized:
public ViewModel_Page1()
{
InitCommands();
...
}

Why the OnBeforeUnload doesn't intercept the back button in my GWT app?

I have a hook on the beforeUnload event. If i try to click on a link, reload or close the tab or the navigator, the app ask for confirmation before leaving. That's the desired behavior.
But if click on the back button or the backspace outside an input, no confirmation.
At the beginning of the html :
window.onbeforeunload = function() {
if (confirmEnabled)
return "";
}
And i use the Gwt PlaceHistoryMapper.
What did i miss ? Where did i forgot to look ?
Thanks !
As long as you stay within your app, because it's a single-page app, it doesn't by definition unload, so there's no beforeunload event.
When using Places, the equivalent is the PlaceChangeRequestEvent dispatched by the PlaceController on the EventBus. This event is also dispatched in beforeunload BTW, and is the basis for the mayStop() handling in Activities.
Outside GWT, in the JS world, an equivalent would be hashchange and/or popstate, depending on your PlaceHistoryHandler.Historian implementation (the default one uses History.newItem(), so that would be hashchange).

JavaScript: How to force a page reload on reset?

I have a page with some generated HTML that survives the form's reset button. It is a problem because that HTML is inconsistent with the values in the cached default form.
In principle I guess it could be solved easily if I could force a hard reload from the server when the user presses the reset. However I see that the Chrome browser does not support the onReset event (in fact it is deprecated in HTML5).
But perhaps I could work around the missing onReload event. Can I re-define what happens when the reset button is pressed? In my case the apply and reset buttons are located in general HTML templates which I cannot change. Can I attach a function to the button from JavaScript?
You can replace the "reset" button , by a regular button.
And use the "onClick" event, to trigger a page reload.
EDIT
oops I missed the template part,
You can add a function to a button from Javascript.
First you need to "get" the button, with something like document.getElementbyId('resetButton');
If the button doesn't have a ID, you still can to retrieve it by doing javascript dom traversal
then you can add a function like :
var resetButton = document.getElementbyId('resetButton');
resetButton.onclick= reloadPage;
function reloadPage(){
window.location.reload();
}

How to press a button with java-script event inside a web-page by code for iOS (iPhone)?

I am writing an application for iOS (iPhone).
How to press a button with java-script event inside a web-page? I need to write a code to press this button. Then the event will be executed. And I'll take result of it's work.
Thanks a lot for help!
You can use:
document.getElementById('id-of-button').click();
This will perform a click event on the button.
EDIT: to run JavaScript in a UIWebView use the stringByEvaluatingJavaScript: method. Eg:
[webView stringByEvaluatingJavaScript:#"document.getElementById('id-of-button').click();"];

Reloading a jstree without refreshing the page

I'm trying to reload a jstree without reloading or refreshing the page, the reload should happen onclick of a button, so when the button is clicked im calling
$("#tree-container").jstree('destroy');
//then calling the function that reads or renders the tree
that works fine when no tree is already rendered, however when a tree is already rendered and I click on the button I get this : "cannot call method init of undefined." the error occurs in the jstree plugin.
I even tried removing the container then re-appending it to it's parent container before calling the function that reads the tree
$("#tree-container").remove();
$('#parent-tree-container').append($('<div id="tree-container"></div>'));
//then called the function that reads/renders the tree
but that still didn't work. :(
Thanks in advance.
This will make an ajax call to refresh the tree, but not the page it's contained in:
$.jstree._reference($("#tree-container")).refresh(-1);
That is the best method, but if you prefer, you can just rebuild the jstree over the initial div without destroying it first:
$("#tree-container").jstree({ etc
Are you using jQuery 1.6.2? If so, try a lower version - 1.6.1 should work. - see here