Skip to main content

Manipulating web browser scroll position on Windows Phone 7

Manipulating the browser control position on WP7 is relatively straight forward, all you need to is a couple of calls out to javascript using the InvokeScript method on the browser control. In fact this is the same pattern provided in the desktop version of WPF & Silverlight.

The only requirement is the ability to include a couple of javascript functions in a web page. Now this is only possible if you either have control over the remote content or you're using local content on the device. Recently I've been working on a phone app that uses local content. This content is designed for a particular profession who need to access to detailed and intricate data in a fast and easy manner. Each page conforms to a standard model with a simple breadcrumb style at the top to help with navigation and very detail data with embedded links. When  a page is bookmarked or the back button is used to navigate the app we are need to get the scroll positions.

So shown below is a simple test app for manipulating the scroll position, as you can see it has a set of buttons for  changing the scroll positions programmatically, so when I start the app and get the vertical & horizontal scroll positions they return '0' as expected. After zooming in and moving around the page and then getting the positions again you can see they have changed. Similarly if I enter a position and click a 'Set' button you would observe the browser scroll position changing:



So the C# code to call out to javascript is very simple and as I said before this is exactly same code I would use for WPF or desktop Silverlight application:

private void setVScrollClick(object sender, RoutedEventArgs e)
{
    try
    {
        this.webBrowser.InvokeScript("setVerticalScrollPosition", this.vScrollPos.Text);
    }
    catch (Exception exn)
    {
    }
}

private void getVScrollClick(object sender, RoutedEventArgs e)
{
    try
    {
        object pos = this.webBrowser.InvokeScript("getVerticalScrollPosition");
        this.vScrollPos.Text = pos.ToString();
    }
    catch(Exception exn)
    {
    }
}

private void getHScrollClick(object sender, RoutedEventArgs e)
{
    try
    {
        object pos = this.webBrowser.InvokeScript("getHorizontalScrollPosition");
        this.hScrollPos.Text = pos.ToString();
    }
    catch (Exception exn)
    {
    }
}

private void setHScrollClick(object sender, RoutedEventArgs e)
{
    try
    {
        this.webBrowser.InvokeScript("setHorizontalScrollPosition", this.hScrollPos.Text);
    }
    catch (Exception exn)
    {
    }
}

And finally the javascript, as I said you'll need some ability to get these functions included in the page directly or into a javascript file. We had the ability when the local content was installed on the device to inject these functions into the javascript  file:

function getVerticalScrollPosition() {
return document.body.scrollTop.toString();
}
function setVerticalScrollPosition(position) {
document.body.scrollTop = position;
}
function getHorizontalScrollPosition() {
return document.body.scrollLeft.toString();
}
function setHorizontalScrollPosition(position) {
document.body.scrollLeft = position;
}

There is one small but important fact to remember when calling out to javascript from managed code is any value returned from javascript has to be a string value. This means if the 'getVerticalScrollPosition' method above returned a number value it would not be marshalled to managed code.

The application I used for this post can be found here.






Comments

  1. Good day! I know this is kinda off topic but I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My site covers a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you're interested feel free to shoot me an e-mail. I look forward to hearing from you!
    Fantastic blog by the way! my weblog:
    CMS developer London

    ReplyDelete

Post a Comment

Popular posts from this blog

Implementing a busy indicator using a visual overlay in MVVM

This is a technique we use at work to lock the UI whilst some long running process is happening - preventing the user clicking on stuff whilst it's retrieving or rendering data. Now we could have done this by launching a child dialog window but that feels rather out of date and clumsy, we wanted a more modern pattern similar to the way <div> overlays are done on the web. Imagine we have the following simple WPF app and when 'Click' is pressed a busy waiting overlay is shown for the duration entered into the text box. What I'm interested in here is not the actual UI element of the busy indicator but how I go about getting this to show & hide from when using MVVM. The actual UI elements are the standard Busy Indicator coming from the WPF Toolkit : The XAML behind this window is very simple, the important part is the ViewHost. As you can see the ViewHost uses a ContentPresenter element which is bound to the view model, IMainViewModel, it contains 3 child v

Showing a message box from a ViewModel in MVVM

I was doing a code review with a client last week for a WPF app using MVVM and they asked ' How can I show a message from the ViewModel? '. What follows is how I would (and have) solved the problem in the past. When I hear the words ' show a message... ' I instantly think you mean show a transient modal message box that requires the user input before continuing ' with something else ' - once the user has interacted with the message box it will disappear. The following solution only applies to this scenario. The first solution is the easiest but is very wrong from a separation perspective. It violates the ideas behind the Model-View-Controller pattern because it places View concerns inside the ViewModel - the ViewModel now knows about the type of the View and specifically it knows how to show a message box window: The second approach addresses this concern by introducing the idea of messaging\events between the ViewModel and the View. In the example below

Custom AuthorizationHandler for SignalR Hubs

How to implement IAuthorizationRequirement for SignalR in Asp.Net Core v5.0 Been battling this for a couple of days, and eventually ended up raising an issue on Asp.Net Core gitHub  to find the answer. Wanting to do some custom authorization on a SignalR Hub when the client makes a connection (Hub is created) and when an endpoint (Hub method) is called:  I was assuming I could use the same Policy for both class & method attributes, but it ain't so - not because you can't, because you need the signatures to be different. Method implementation has a resource type of HubInnovationContext: I assumed class implementation would have a resource type of HubConnectionContext - client connects etc... This isn't the case, it's infact of type DefaultHttpContext . For me I don't even need that, it can be removed completely  from the inheritence signature and override implementation. Only other thing to note, and this could be a biggy, is the ordering of the statements in th