简体   繁体   中英

Windows phone 8, web browser control is opening media files with media element and navigate from application

Is there a way to detect on WP8 that web browser control is automatically opening media files using media player which then activate OnNavigateFrom event, and how to differ that event from OnNavigateFrom event that is activated when backBtn,Start or search button pressed. This is important because different code need to be activated in those cases. Is there a way to detect when web browser control is selecting URL that is some kind of media URL, and to prevent URL to be open in external application, but to open URL in web browser control or some media element that is existing in application?

You can override OnBackKeyPress Event. Define a static Boolean variable in App.xaml.cs, initialize it false. and set this true when back key is pressed.

and set it to false when OnNavigateTo event is fired

You will need to do http communication to the uri. Then, on inspecting the response, you will be able to have a clear idea about the url type , ie either browser web page or a media URL opening in a separate media player.

The RequestContext class , that you will be using for http communication ,contains information about the HTTP request in the HttpContext property. When you construct a URL from a route, you pass an instance of the RequestContext class to the RouteCollection.GetVirtualPath method.

If you see that requestContext.response.MediaType is either text/html or text/plain or image/svg+xml or application/xhtml+xml , then the url is browsable one, else for any other mime type, the browser will open default apps like the media player or the unzip utility depending upon the type of url.

This is how i had segregated the browsable uris and the downloadable uris.

    private bool IsBrowsableMimeType(RequestContext requestContext)
    {
        //If the mime is text/html or text/plain or image/svg+xml or application/xhtml+xml
        //then mime type is browsable
        //else it is downloadable
        if (requestContext.response.MediaType == null)
        {
            return true;
        }
        if (!requestContext.response.MediaType.Equals("text/html") && !requestContext.response.MediaType.Equals("text/plain") && !requestContext.response.MediaType.Equals("application/xhtml+xml") && !requestContext.response.MediaType.Equals("image/svg+xml"))
        {
            //If the url is of image mime type, then let the browser show the image, dont download it.
            if (!requestContext.response.MediaType.Equals("image/svg+xml") && requestContext.response.MediaType.Contains("image"))
            {
                return true;
            }
            return false;
        }
        else
        {
            return true;
        }
    }

This is how i had achieved it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM