WPF XBAPs for Mozilla Firefox in .NET Framework 3.5

Rich Internet Application columnist for ZDNET Ryan Stewart broke the news today that the WPF team are working on XBAP applications for Mozilla Firefox for the upcoming .NET Framework 3.5 (codename "orcas") release. XBAPs (Xaml Browser APplications) are a special kind of ClickOnce deployed WPF application that is hosted in the browser inside a security sandbox, and currently only work in IE6/7. XBAPs integrate well into existing web-based applications providing a fairly seamless transition from regular HTML content to WPF application, since they are hosted inside the main browser window. »

Author image

Why is the Width/Height Property for many Elements ‘NaN’ in WPF?

Developers new to WPF are sometimes a little confused when the Width and Height properties of elements report as ‘NaN’ (Not a Number) in code, when these very same elements are clearly visible and do have a width and height. What’s going on? The short answer is you can interpret ‘NaN’ to mean “not set”. The Width and Height properties of Buttons, TextBoxes and most other controls are inherited from the FrameworkElement superclass. »

Author image

How can I pass Querystring parameters to my WPF XBAP Application?

The query-string portion of the URL in a HTTP request is a useful place for passing state between pages in web applications. It is therefore not unexpected that developers would also want to use this common idiom for passing information to WPF XBAPs (Xaml Browser Applications) that they wish to integrate with their site.

Fortunately this is not too difficult, except there is one trick you need to know. The ActivationUri property of the ApplicationDeployment object gives you the Uri that launched the XBAP. To get access to this you need to add a reference to System.Deployment.dll. To get hold of the current ApplicationDeployment object for your XBAP use the static CurrentDeployment property of the ApplicationDeployment object like so.

Uri launchUri = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.ActivationUri;

If you launch this code from inside Visual Studio to debug it won’t work. The way it is launched means when you press F5 means that the CurrentDeployment property will be null and thus cause a NullReferenceException. You can use the static IsNetworkDeployed property of the ApplicationDeployment ojbect to check if you’ve been deployed over the network (for an XBAP application hosted in the browser this should be true).

if (ApplicationDeployment.IsNetworkDeployed)
{
    Uri launchUri = ApplicationDeployment.CurrentDeployment.ActivationUri;
}

So far so good, and now for the trick. In order for the deployment infrastructure to actually pass anything through to you via this API you need to set a property in your manifest. Fortunately Visual Studio 2005 gives you a convenient way to do this. Select project properties in Visual Studio 2005 and then go to the publish tab, as shown in figure below.
Publish Tab in VS 2005

Now click the Options button and in the options dialog check the box that says “Allow URL parameters to be passed to application”. Click OK and re-build your application.
Publish Options Dialog

Once you’ve verified that the URI is being passed through you can use the Query property of the URI to return the query-string portion, and parse out any information you need from there.

»

Author image

How do I know if I am running on the UI thread in WPF?

The golden rule of WPF multi-threading is “Thou shalt only update UI using code that runs on the same thread as the UI.” If you happen to be running on another thread you use the Dispatcher property of any of your UI elements to get back on to the UI thread. This raises the question - how do I know when I’m on the UI thread? It turns out the Dispatcher has two methods you can use - CheckAccess and VerifyAccess. »

Author image

What are some of the “gotchas” with using the WPF frame control?

The WPF frame control is a content control, capable of displaying both WPF and HTML content, and allowing navigation between different pieces of content. While it is un-questionably useful there are some limitations with it that make it more so in some circumstances than others. Rendering The WPF Frame control alters the way it renders based on the type of content it is displaying. When it is displaying WPF Pages or loose XAML it uses WPF’s rendering pipeline. »

Author image

Chris Anderson's WPF book available for pre-order from Amazon

WPF architect Chris Anderson has been working on a book explaining his wonderful creation for quite some time now. The book is just under 500 pages long and should be available at the end of April, but you can pre-order it now on Amazon. I can't wait to read this one to get insights from Chris as to the "why" of certain bits of WPF design. The book also has an accompanying page on Chris' site which contains a link to the first 9 pages of the book in XPS format, and will hopefully include code downloads and such when the book is released. »

Author image

.NET 3 available as an optional software update for XP

As a few others have noted .NET 3 (the delivery vehicle WPF is included in) is now available as an optional update for XP via windows update. This should hopefully increase the adoption of WPF amongs users who are not planning on upgrading to Windows Vista. »

Author image

Xaml Decompiler from Lutz Roeder

Lutz Roeder, author of the popular .NET class browser and decompiler reflector has released a Xaml disassembler. Xaml (used for defining UI layout and vector graphics in WPF applications) is typically "compiled" into a more efficient binary representation called Baml and embedded as a resource in WPF applications where it is read at run-time. Lutz' tool reverses this "compilation" process, returning Xaml which is much easier to understand than Baml, but also easier to copy. »

Author image

Minor Issue Rendering Combo Boxes in WPF XBAP Applications

I recently noticed an issue with the rendering of ComboBox controls when they are hosted in WPF XBAP applications. The nature of this issue is such that it will probably (hopefully?) not cause anyone any problems. As the screen-shot below shows, when a ComboBox has a RotateTransform applied to it (as either a RenderTransform or LayoutTransform) the "drop down" part of the combo is not rotated. For comparison I've included a regular WPF ListBox which is also rotated. »

Author image

When I add Controls to a WPF RichTextBox They Are Always Disabled. How can I change that?

WPF FlowDocuments can contain controls like CheckBoxes and Buttons. When the document is displayed statically and is not editable the controls are enabled, however in WPF V1 when they are rendered as the content of a RichTextBox (and the FlowDocument can be edited) the controls are disabled.

This has not always been the case. During the WPF CTP releases around beta 1 it was possible to enable controls inside a FlowDocument contained within a RichTextBox by setting the IsEnabled property on the FlowDocument to true. As WPF V1 approached the WPF team decided to disable this functionality. Although I don’t believe I’ve ever seen a reason for this it would be a difficult feature to fully support.

Fortunately there is a fairly clean work-around, for those prepared to take the risks associated with using a feature that the product team has consciously disabled. By inheriting from FlowDocument and over-riding the protected IsEnabledCore method to return true, and then using this FlowDocument sub-type inside the RichTextBox you can add enabled controls.

C# Code (WPF V1)
class EnabledFlowDocument : FlowDocument
{
    protected override bool IsEnabledCore
    {
        get
        {
            return true;
        }
    }
}

Xaml Code (WPF V1)
<RichTextBox Grid.Column=“1”>
<!– sub-class of FlowDocument –>
<my:EnabledFlowDocument FontFamily=“Segoe” FontSize=“12”>
  <Paragraph>
  This is some text inside a sub-type of flowdocument
  </Paragraph>
  <BlockUIContainer>
    <Button Content=“Click Me!” Click=“ButtonClicked”>
    </Button>
  </BlockUIContainer>
  <Paragraph>
    <Hyperlink
     NavigateUri=“http://learnwpf.com"   Click=“LinkClicked”>
     visit LearnWPF.com
    </Hyperlink>
  </Paragraph>
</my:EnabledFlowDocument>
</RichTextBox>


You can download the source code here

Note: I had to explicitly set the FontFamily and FontSize on the instance of the sub-type of FlowDocument that I created to match the “default” appearance of the regular FlowDocument, so a little more work is required to make this a drop-in replacement for the regular FlowDocument.

I first saw this work-around in the MSDN forums but have subsequently discovered it has been documented elsewhere.


»

Author image