VS Template: Flexible Application

In WPF, we support two main app models: standalone and XBAP.

Standalone apps are akin to traditional Windows apps. They run in their own window. They tend to be fully trusted (unrestricted access to the computer and its resources). And they are installed (either via an MSI, ClickOnce, or EXE distribution).

XBAPs, on the other hand, are browser hosted applications. They’re cached (using ClickOnce) and sandboxed. Users navigate to them (promptlessly) in the browser like they do any other website. (XBAP whitepaper & sandbox whitepaper)

What’s great then is, if you’ve written your app in a security & navigation friendly way, converting between standalone & XBAP is as easy as flipping a few project settings.

Flexible Application VS Template

In order to facilitate switching between app models, I’ve written a “Flexible Applcation” VS template. It enables you to maintain a single application project file and easily compile as standalone or xbap based on application configuration.

FlexibleApplication Flow Chart

How do I use it?

When creating a project, select “Flexible Application (WPF).” (Template installation structions below.)

When building/running the app, choose the configuration that you want:

Debug and Release will build a standalone app. XBAP Debug and XBAP Release will produce an XBAP

Sample

I updated the below SDK sample to work with the Flexible Application template. You can get the Beta2 code here and the June CTP code here.  Or straight to the xbap on Beta2 or xbap on June CTP.  

 

How do I install FlexibleApplication template?

  1. Copy this zip file to “%userprofile%My DocumentsVisual Studio 2005TemplatesProjectTemplatesVisual C#”. Don’t unzip the file – just copy the zip file as is.

Release notes

This version of the template does not support publishing.  For more information, see the template readme.

Some thank yous

– WPF SDK for a great cube app. (WPF SDK team’s blog)

Ashish Shetty & Dennis Cheng for their VS template help.

Beatriz Costa for her help setting up the XAML databinding

29 comments

  1. Thanks for the post. It’s quite useful.

    I’ve not experimented with configurations in VS 2005 yet, so this may be a dumb question: Can you retrofit an existing project with the ability to go between XBAP and standalone?

    I need to do some memory leak investigative debugging and that seems a little harder in the XBAP model (some tools don’t work with XBAPs). So I’d like my app to compile as standalone.

  2. You can do this by manually editing the .csproj or .vbproj.

    There are three main tags involved in making something an “XBAP”:

        <HostInBrowser>True</HostInBrowser>
        <Install>False</Install>
        <TargetZone>Internet</TargetZone>

    Other tags are required for the “XBAP” debug experience, etc:

        <EnableSecurityDebugging>false</EnableSecurityDebugging>
        <MapFileExtensions>False</MapFileExtensions>
        <StartAction>Program</StartAction>
        <StartProgram>$(WINDIR)System32PresentationHost.exe</StartProgram>
        <ApplicationExtension>.xbap</ApplicationExtension>
        <StartArguments>-debug "$(MSBuildProjectDirectory)bin$(Configuration)$(AssemblyName)
    $(ApplicationExtension)"</StartArguments>

    If you comment out all of the above tags and add…

        <Install>True</Install>

    …your app will run as a standalone. 🙂

  3. This template looks really helpful :). I had tried to manually edit the .csproj file and I missed the Internet tag. So I guess that was problem.

  4. For those interested in using the new Flexible template for xbapstandalone combo apps. Here are the instructions.

    To install the template:

    First, find the user template install path:

    Tools->Options->Project and Solutions->General
    The path for the zip file is in the “Visual Studio user project templates location:”

    Second, copy the Flexible zip file into the template location:

    Copy the .zip file. Do not extract files. VS is looking for a .zip file.
    You can get the zip file from https://scorbs.com/2006/06/04/vs-template-flexible-application/

    To start the project:

    File->New-> Project
    Click on “Visual C#”
    The Flexible template will be in “My Templates”
    Read the Readme.txt all the way through – there are some issues that will cause trouble if they are not known ahead of time.

    For a further reference on VS Templates go to this link:

    http://msdn.microsoft.com/msdnmag/issues/06/01/CodeTemplates/default.aspx

    rhogue

  5. I selected the flexible application project type, the directory, etc. and hit OK. Then I get a dialog telling me
    The project file “C:….TempFlexApp.csproj cannot be opened. The project type is not supported by this installation.”

    I haven’t investigated yet, but the message tells me the .csproj template has a project guid that my machine doesn’t know about. I have the June CTP installed. I don’t remember the date it came out, but perhaps this template is from pre-June CTP and the guids were different?

    Hopefully I can figure it out, but thought I’d make a post here in case someone else has insight while I work on it.

  6. Yes, it’s installed. I even removed it and reinstalled it, but I still get the problem. It’s not just your project…even the project template that comes with the June CTP gives the same problem.

    Come to think of it, I’ve never created a new project since upgrading to June. I just took the project guids out of my .csproj file so it would open.

    Maybe I’m suffering from having installed/removed several previous versions?

  7. Hey, thanks very much for this! It works really well, and is a real time-saver. Already helped me find out that BitmapEffects aren’t allowed in XBAPs, which would have taken me forever to figure out with my old debug methods.

    Thanks again, you can see it in action at my site!

  8. The flexible app works great for debugging. It is a bit troublesome that the Publishing feature doesn’t work, since there is no versioning. I’m not sure how to get my browser to realize the XBAP has changed and have to mage -cc each time. Is there a way around this?

  9. Hi Brian,

    Please try updating the version number on the Publish Pane of the Project Properties. That should make all the ClickOnce versioning goodness work as expected when you copy up the XBAP.

    Thanks,
    Karen

  10. Hi

    This is very cool reply from you!! I am very thankful to you for the valuable information on the conversion of Stand Alone to XBAP.

  11. Hi

    The code you mentioned seems to be pretty cool for converting the existing applications either for XBAP or to stand Alone applications.

    However when I try to convert a stand alone to XBAP by editing the .csproj IDE says that and is required and I could not able to appreciate the conversion.

    When I try to Convert XBAP to stand alone application I could able to do that with out any problem,since and are generated with in the XBAP file.

    So Please tell me how to resolve this while converting Stand alone to XBAP.

  12. Hi Satish,

    XBAPs require that you sign the ClickOnce manifests. By default, the XBAP project template create a temporary key for you and sets up the manifest signing. If you start with a standalone project and thenconvert it to an XBAP, you have to do this step manually:

    1. On the Project Properties pane, select the signing tab.

    2. Check “Sign the ClickOnce manifests”

    3. Select the desired certificate (creating a test certificate if you don’t have a specific desired cert)

    Thanks,
    Karen

  13. Hey Karen,

    I am working on a “code generation” project, our goal is to generate framework 3 applications both standalone and xbap that mainly deals with a database and is generated based on the user specifc options, we finished developing the standalone version,we are going to generate all the necessary files(.xaml,.xaml.cs etc..) for the application , the problem is that we want to convert the standalone version to an xbap version entirely from the runtime to save time rather than again developing the generated xbap version again which is mainly the same as the standalone, in other words is there a way to do the steps of converting a standalone application to an xbap application(changing the cproj file,creating the certficate etc..) through code or runtime??

    If you have any suggestions that may help in our specific scenario I am happy to hear it

    Thank You Very Much

  14. Any chance of adding publish support? We need to publish both standalone and xbap. Copying files won’t work because of references in code that look to the deployment directory to find certain files. Our app is full trust.

  15. Hi:

    I am new in XAML and WPF. I am trying to use a SQL-SERVER connection in a XBAP I can do it inside a WPF Windows application, but when I try to do the same like Load_Page() ASP.NET I get an error

    void mainPage_Loaded(object sender, RoutedEventArgs e)

    {

    // Connection Fail

    }

    I had downloaded many good books like:

    Applications = Code Markup: A Guide to the Microsoft® Windows® Presentation Foundation

    By Charles Petzold

    Programming WCF Services

    By Juval Lowy

    But no body speak about access data from SQL-SERVER database

    may be you can help me.

    Excuse my English I speak Spanish .

    Thank you

    Reiner

    reinerra@hotmail.com

  16. @bertrand yes 🙂

    @karen (at first sorry for my bad english) is it possible to pack an silverlight applkation instead of XBAP application in this template?

    P.S. thx its a great template and save a lot of time

  17. Have been looking for something some time. Is really helpfull, thanks.

    I have some questions though.
    Is there some way that i can remove the left and right button when i run in standalone app mode?
    And is it possible to run the app in maximized screenstate from start? In WPF application i can set this on the window class but not sure how to do this now.

  18. Hi,

    The Template works great with only one project but I added a class library DLL to the solution and now I am getting a security permission exception when trying to access a method in that DLL with the XBAP debug configuration from the main application. I edited the class library DLL project file to contain the same configurations as the template. The app works fine when in debug configuration (Windows).

    Previously, in another solution (non flexible config) I had a XBAP solution using a class library DLL built with another solution without security problems.

    Thanks for any help,
    Jody

  19. Brilliant!

    I read about this article in the WROX book “Beginning Microsoft Visual C# 2008” (on page 1164), tested it with Visual C# 2008 Express and it worked perfect.

Leave a Reply to Satish Cancel reply

Your email address will not be published. Required fields are marked *