Welcome to MSDN Blogs Sign in | Join | Help

Extending the Notepad sample with a Web Browser View [Ricky Tan]

The purpose of this post is to show just how easy it is to create additional views with Acropolis to extend the functionality of an application. The application shown here is the Notepad sample included as part of Acropolis CTP1. What the WebBrowserView will do is display the contents of the text in HTML format, like in a web browser.

 

Step 1: First, add the WebBrowserView. Open the Notepad sample in Visual Studio. In Solution Explorer, right-click the Notepad project and select Add | New Item. In the Add New Item dialog box that appears, select the Acropolis View (WPF) template, change the name to WebBrowserView.xaml and then click Add. In WebBrowserView.xaml.cs, add the following ViewExtensionInfo attribute to the WebBrowserView class declaration (see NotepadPartView.xaml.cs for an example):

[ViewExtensionInfo(DefaultViewTechnologies.WindowsPresentationFoundation,
                  
"Web Browser View",
                   "Web Browser View for HTML",
                   typeof(INotepadPartViewContract))]

At this point, you can build and run to see the newly-added view with its default template. Click on the view-selector button, which is on the upper-right of the window (the button in the red circle in the image below), and select Web Browser View to change the view:

Step 2: Next, we shall customize the WebBrowserView. The WebBrowser control is from Windows Forms, so we need to add references to the System.Windows.Forms and WindowsFormsIntegration assemblies to our project. Then, in WebBrowserView.xaml, add the following to the class declaration (together with the other xmlns declarations):

xmlns:WF="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

Replace the default TextBlock with the WebBrowser control, which is hosted in a WindowsFormsHost:

<WindowsFormsHost>
    <WF:WebBrowser x:Name="webBrowserControl" />
</WindowsFormsHost>

Step 3: Finally, we hook up the WebBrowser control to the value of the NotepadText property from the Part-View contract. In WebBrowserView.xaml.cs, add the following method:

public override void BindToContract(IPartViewContract contract)
{
    base.BindToContract(contract);
    INotepadPartViewContract notepadPart = contract as INotepadPartViewContract;
    if (notepadPart != null && notepadPart.NotepadText.Value != null)
    {
        this.webBrowserControl.DocumentText = notepadPart.NotepadText.Value.ToString();
    }
}

That’s it.  Build and run to get the Notepad application started up, then type in or paste in some HTML source, and use the view-selector button to switch between the default Notepad view and the web browser view. Just one ViewExtensionInfo attribute, some XAML declarations, and five lines of code is all you need to extend the Notepad sample with a WebBrowserView.

Published Tuesday, June 12, 2007 7:35 AM by acropolis
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Extending the Notepad sample with a Web Browser View - VB.NET Edition

Ricky Tan wrote a really cool post for the Acropolis Team Blog titled Extending the Notepad sample with

Tuesday, June 12, 2007 3:09 AM by Dennis "D.C." Dietrich

# Acropolis Team Blog

Be sure to check out the Acropolis Team Blog too. We'll be posting regularly to this blog to keep you

Tuesday, June 12, 2007 1:20 PM by David Hill's WebLog

# Extending the Acropolis Notepad sample with a Web Browser View

I've just posted on the Acropolis Team Blog about how easy it is to extend the Acropolis CTP1 Notepad

Tuesday, June 12, 2007 2:07 PM by Ricky Tan

# re: Extending the Notepad sample with a Web Browser View

The above example did not work for me as I needed to add the WindowsFormsIntegration DLL reference then add the xmlns:

xmlns:WFI="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

then add the change the host tag to:

<WFI:WindowsFormsHost>

  <WF:WebBrowser x:Name="webBrowserControl" />

</WFI:WindowsFormsHost>

Tuesday, June 12, 2007 9:08 PM by Glenno

# re: Extending the Notepad sample with a Web Browser View

Thanks Glenno.  I did write about that, but I should have been clearer about the DLL reference:

"Step 2: Next, we shall customize the WebBrowserView. The WebBrowser control is from Windows Forms, so we need to add references to the System.Windows.Forms and WindowsFormsIntegration assemblies to our project."

Thanks,

Ricky

Wednesday, June 13, 2007 1:21 PM by rickyt

# New and Notable 171

Acropolis David Hill's blog is here and the Team Blog is here One nice post on the Team Blog is Extending the Notepad Sample with a Web Browser View Secret Themes in Acropolis Three New Acropolis Videos WCF/SOA/WF/Orcas Jesus Rodriguez on Orcas Durable

Tuesday, December 02, 2008 9:35 PM by Sam Gentile's Blog

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker