[WP8 Development] WebBrowser control with background code. CS Simple Interaction

Source: Internet
Author: User
Tags html sample

The premise: The environment I am currently testing is Windows Phone 8.1 Silverlight, so the control is WebBrowser, not webview.

Doing the project may involve the interaction of the Web page with the backend code, because cross-platform issues may be taken into account, so some links in the company's Web pages need to be handled by the client itself.

There are the following HTML sample code (important parts):

1 <!--List title -2   <Divclass= "AM-LIST-NEWS-BD">3     <ulclass= "Am-list">4       <Liclass= "Am-g am-list-item-dated">5         <ahref= "appnews://map/66th xx Road, Chaoyang District, Beijing"class= "AM-LIST-ITEM-HD"><imgsrc= "/public/image/xxxaddress.png"/>&nbsp;No. 66th XX Road, Chaoyang District, Beijing</a>6         <spanclass= "Am-list-date">></span>7       </Li>8       <Liclass= "Am-g am-list-item-dated">9         <ahref= "appnews://tel/010-65431234"class= "AM-LIST-ITEM-HD"><imgsrc= "/public/image/xxxphone.png"/>&nbsp;010-65431234</a>Ten         <spanclass= "Am-list-date">></span> One       </Li> A     </ul> -   </Div> - </Div> the <ahref= "appnews://youhuiquan/123456"class= "Am-btn am-btn-danger am-btn-block"role= "button">Collect Coupons</a>

Apps usually require processing the links that start with appnews://, such as the appnews://map/map link, and iOS with a system-encapsulated clgeocoder to handle it, so how does WP8 work?

1. First set up a webbrowser:

<phone:webbrowser name= "Mainwebbrowser"

isscriptenabled= "True"

Loadcompleted= "Mainwebbrowser_loadcompleted"

scriptnotify= "Mainwebbrowser_scriptnotify"/>

1.1 Where isscriptenable must be set to True, the system defaults to false, because the work for a while is to inject JavaScript code for WebBrowser, so it must be set to true.

The role of the 1.2 loadcompleted event is to add JavaScript code to webbrowser.

The 1.3 scriptnotify event is the logic for handling the added JavaScript code.

1.4 Add a statement to enable the control to navigate to the above HTML.

This is the end of the front office.

2. Start configuring the background. cs file (important part):

   Private voidMainwebbrowser_loadcompleted (Objectsender, NavigationEventArgs e) {            varLocal = (Sender asWebBrowser).            Source.localpath; if(Local.) Contains ("Discount/tid") {mainwebbrowser.invokescript ("Eval",                    @"For (var i = 0; i < document.links.length; i++) {Document.links[i].onc                            lick = function () {window.external.notify (' appnews://' + this.href);                        return false; }                     }"); }        }        Private voidMainwebbrowser_scriptnotify (Objectsender, Notifyeventargs e) {            stringstr =E.value; STR= str. Replace (the Appnews://",""); if(str. Contains ("map/") ) {str= str. Replace ("map/",""); Microsoft.Phone.Tasks.MapsTask Map=NewMicrosoft.Phone.Tasks.MapsTask () {searchterm =STR}; Map.            Show (); }            Else            {                MessageBox.Show ("This feature is not supported temporarily","Tips", Messageboxbutton.ok); }        }

The 2.1 loadcompleted event defines the JavaScript code that needs to be injected, because I have only one page to use, so I use the IF statement to judge.

Call Invokescript add JavaScript code, the current function is to click on a button to jump to the map,

And there are many links in the Web page that start with appnews, so use Window.external.notify (' appnews://' + this.href) to do the matching.

2.2 Once the operation is defined, run the code, click on a button, enter the Scriptnotify method, and the IF statement is used to process the incoming string, that is, to manipulate the href in the HTML.

2.3 Note:in the loadcompleted return be sure to write false , write True if WP8 will pop up "Do you want to search for apps from the store?" "In the MessageBox.

Because the map uses the launcher, it navigates out, so it doesn't trigger the MessageBox that pops up the search app,

If it is something else, such as the MessageBox above giving the user a hint that does not support the feature, clicking OK will pop up the search app's MessageBox.

At this point the simple interactive work is all over, thanks to the small partners in the development group to provide some code, refer to http://stackoverflow.com/questions/12703698/ Open-links-in-external-browser-in-webview-winrt

The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.

[WP8 Development] WebBrowser control with background code. CS Simple Interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.