Wednesday, 25 June 2014

WPF Page Hosted Windows Applications and XAML Browser Applications (XBAPs)

WPF Page Hosted Windows Applications and XAML Browser Applications (XBAPs)

In WPF, you can also make page based applications. These page based applications are of two types:

1. Pages Hosted in Windows Application
2. Stand-alone XAML Browser Application(XBAPs)

Lets try to understand these WPF Page based applications:

1. Pages Hosted in Windows Application

In this approach, you create your windows application and host some of the pages in that Windows application using NavigationWindow or Frame. This approach is generally used in WPF applications to integrate some kind of wizard in your application, to show some help contents and documentation.

Hosting Pages in Navigation Window

System.Windows.Controls.Page class is used as top level container instead of Windows. 

<Page...>
...
...
</Page>

But actually, there is one more top level container known as NavigationWindow (derives from Windows class) in which Page container is hosted. The NavigationWindow looks like an ordinary window aside from the backward and forward buttons that appear in the bar at the top. Page class is derived directly from FrameworkElement class. You can add a page to any WPF project. Just choose Project --> Add Page in visual studio.

Hosting Pages in other Window

You have to use Frame class to host your page inside other windows. You can use several frames in your window. You have to use the Source property of the Frame class to point to the page which you want to host.

<Window...>
  ....
  ....
  <Frame Source="Page2.xaml"></Frame>
  ....
  ....
</Window>

Hosting Pages in other Pages

You can also host your page into another page. Again you have to use Frames for this task. Just keep a frame container in other page where you want to host your page.

Ways to move from one page to another:

1. Forward and Back Button:

Set Page.ShowNavigationUI property set to true to show the forward and back button. By default, the property is set to true. There is also one more property called NavigationUIVisibility property which you can use to hide/show the Navigation buttons. By default its value is Automatic. But you can set it to Hidden or Visible as per your need.

2. Hyperlinks

Hyperlinks include NaviageUri property using which you can jump from one page to another.

Example: 

Navigating to other page: 

<Textblock>Click <Hyperlink NavigateUri="Page2.xaml">here</Hyperlink></TextBlock>

Fragment Navigation: Navigating to other page and at specific control on that: 

<Textblock>Click <Hyperlink NavigateUri="Page2.xaml#myTextBox">here</Hyperlink></TextBlock>

You will land on Page2.xaml and scroll up to the myTextBox control. But myTextBox control will not have any focus.

Navigating to websites:

<Textblock>Click <Hyperlink NavigateUri="http://theprofessionalspoint.blogspot.com">here</Hyperlink></TextBlock>

2. Stand-alone XAML Browser Application(XBAPs)

Page Based Applications run directly on IE or Firefox and you don't have to install this application. Just point your browser to correct location. This model is called XBAP. Client system must have .NET framework installed to run the XBAP application. The XBAP applications have limited permissions like XBAP applications cannot write files, cannot interact with system resources and registry, cannot connect to databases etc. But you can give permissions to XBAP applications when required.

To create XBAP application, you have to choose WPF Browser Application template in Visual Studio. After that if you see csproj file, you will find these four elements which point that it is a XBAP application:

<HostInBrowser>True</HostInBrowser>
<Install>False</Install>
<ApplicationExtension>.xbap</ApplicationExtension>
<TargetZone>Internet</TargetZone>

Related Article: To know the difference between Windows and Page Controls, visit my previous post.

No comments:

Post a Comment