Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

No Scroll content of WPF XBAP in iframe

5.00/5 (1 vote)
16 Feb 2010CPOL2 min read 15.1K  
No Scroll content of WPF XBAP in iframe

In my previous blog post, I discussed how to hide the navigation UI of xbap while you host Xbap (XAML browser application) in iframe. Note that xbap can be accessed directly from browser's URL address and can also be viewed in an iframe in an existing web site. In both cases, a user can have contents that require scrolling. If user developed the xbap application in such a way that the application will be accessed directly form URL address, the user can archive the goal by adding a "scrollableview" element in your XAML page. A code sample is provided below:

XML
<Page x:Class="CookieTest.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
	<ScrollViewer>
		<Grid>
			 <!--your content will go here-->
		</Grid>
	</ScrollViewer>
</Page>

Next case is if user hosted the xbap application in an iframe. In this case, there is a good chance that the user will be viewing two vertical scrollbars in user's browser if user's existing site's page is long enough to cause view a vertical scroll bar, that is the browser's vertical scrollbar. Xbap host have a defined size, which generally as long and wide as the browser window. So if your content is longer than the size of xbap host, you can lose your content from viewable content area, or you will end up having two scrollbars. Well, there are several solutions to this problem. Let's discuss with a simpler version. Make the iframe's height property as long as the xbap that is hosting the xbap. And make sure that your xbap will not grow larger than that. But if your xbap has variable content and has variable height and you got to keep the constant look, you need to do some engineering to keep things smooth. Xbap supports get and set of cookie in the site of origin. So do calculate your height for your content in xbap and then set the height as a cookie, make sure you do set the height of the content as a cookie each time the content of xbap has changed, of course in case of the content that cause the xbap to change its size. Here is a code example to set cookie for site from xbap.

JavaScript
Application.SetCookie(BrowserInteropHelper.Source, "HEIGHT=" + DemoHeight );

Now in the page, where you hosted your xbap in iframe read the cookie with JavaScript and then change the height of the iframe. That's it! You will have a smooth operation of auto grow or shrink of your xbap and won't cause any extra scrollbar. For reading the cookie does a little engineering, for example, always track the height cookie with JavaScript timer based function.

JavaScript
<script language="javascript" type="text/javascript">

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring
							(c_start,c_end));
		}
	}
	return "";
}

function InCreaseHeight()
{
	var hight = getCookie('HEIGHT');
	if(hight != "")
	{
		if(document.getElementById('framename')!=null)
		{
			if(hight<600)
			{
				hight = 600;
			}
			document.getElementById('framename').height = hight;
		}
	}
	setTimeout("InCreaseHeight()",250);
}  

</script>  

One more thing to do is call the JavaScript increase height function on body on load event of the page to kickstart the process.

JavaScript
<body onload="InCreaseHeight();">

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)