elmish / Elmish.WPF

Static WPF views for elmish programs
Other
428 stars 69 forks source link

What is the appropriate way to set events? #436

Closed RickyYCheng closed 2 years ago

RickyYCheng commented 2 years ago

Hi guys. WebView2 is available in wpf already. Elmish.WPF made extra controls easily to be added so I decided to use WebView2 in Elmish.WPF! :) In my general wpf program, what i did is to set "new window requested" event to prevent app from opening my browser. First, add CoreWebView2InitializationCompleted event. Program seems like this:

... CoreWebView2InitializationCompleted += (sender, e) => { // do sth else after init. 
    myWebView2Instance.CoreWebView2.NewWindowRequested += (sender, e) => {
        e.Handled = true; 
        myWebView2Instance.Source = new Uri(e.Uri);
    };
};

After that, every time when you clicking a hyperlink and opening a new tab, the app will not pop up your browser to do open the link, but update the webview source. Everything is fine.

It seems that I cannot do that in MVU. In MVU, such as Fabulous, what i know is that the Views are dynamic and declared, which means i cannot get the instance of it easily(I'm not sure whether i can do it). And i have no idea to use bindings to set the event in Elmish.Wpf also;

Can any1 tell me what is the appropriate way to set the event? :) Appreciate!

TysonMN commented 2 years ago

I have never heard of WebView2. Can you link to some documentation that I can read?

Can you create a repo that reproduces the situation you describe?

RickyYCheng commented 2 years ago

I have never heard of WebView2. Can you link to some documentation that I can read?

Can you create a repo that reproduces the situation you describe?

That's pretty easy! :) In wpf, it seems that the “web browser” control is out of style and not easy to use. The Microsoft team create "webview2" to use EDGE(CHROMIUM) to use web-view in many frameworks such as WPF, WinForm, Win-UI and so on.

Link's here: https://docs.microsoft.com/zh-cn/microsoft-edge/webview2/

On my PC(Windows 10), I installed "Edge runtime evergreen". Then I created a PURE WPF project, reference the nuget package "Microsoft.Web.WebView2". Third, add "xmlns" in Xaml, add webview control and then set source. Finally, run app and you could see the webview.

Generally, if a link should open a new tab in the browser, in web-view, it will open your browser OR just create a new window(such as the cef-sharp). In UWP, we can just set

webviewInstance.NewWindowRequested += (sender, e) => {
    e.Handled = true; // not to open your browser 
    webViewInstance.Source = new Uri(e.Uri); // update the source
};

to let your web-view control continue to show the content of your link. Obviously, we can create a new window and show your new content, I just wanna say that the default action of web-view is just to open your browser.

What I am asking is that, in Elmish.WPF, what should we do if we want to set an event of controls in F#? Or we should just ignore MVU and set the event in C#?

TysonMN commented 2 years ago

Thanks for the link and explanation.

You can turn an event into a command. See the EventBindingsAndBehaviors sample.

Does that answer your question?

RickyYCheng commented 2 years ago

Thanks for the link and explanation.

You can turn an event into a command. See the EventBindingsAndBehaviors sample.

Does that answer your question?

That's pretty cool! I'll have a check and close the issue. Thanks for your active help! Have a nice day! :) Good luck!

BentTranberg commented 2 years ago

Link to an English version of that WebView2 page : https://docs.microsoft.com/en-us/microsoft-edge/webview2/