Freigeben über


Living Without App.xaml (…and still being able to use Blend!)

While default WPF (and Silverlight) projects make it very easy for you to get up and running with your application for a majority of scenarios, often, there are scenarios where you want better control of your application. A good example of this scenario is the Expression Blend source code itself. We perform a number of tasks (for example, displaying a splash screen) before the main application Window is shown to you.  For making this happen, we don’t use the App.xaml concept in WPF projects, but instead go with a code generated Application object which gives us better control over how things work. We then load a number of external resource dictionaries that control the look and feel of our application into the Application’s resources.

However, the lack of App.xaml causes issues when you design your application inside Blend. Blend provides a lot of design-time experiences around App.xaml – for example, all resources in App.xaml are always available in all the projects that are referenced from the main project. This is essentially a feature that lets you easily centralize your shared resources across control libraries into one location. Another example is that we allow you to link your external resource dictionaries into App.xaml, again, to make sharing of resources easier for you.

If you ever run into this, you can use another feature in Blend to work around this issue. You can include your App.xaml into the project file on a conditional basis such that when the project is loaded inside Blend, you get the normal experience that you are comfortable with. While building your application, though, App.xaml is not included in the build process, so your code continues to work as before (for example, WPF automatically creates an entry point for your application when you build App.xaml, but you might already have an entry point defined in code).

The below sample will show how you can leverage this by just making a couple of lines of edit to your project files. Essentially, the changes to the project boil down the following two lines:

<DesignTime Condition="'$(SolutionPath)'!='' AND Exists('$(SolutionPath)')">true</DesignTime>

….
<ApplicationDefinition Condition="'$(DesignTime)'=='true' AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true'" Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>

Download the project files from below:


slproject Download Sample

Hope this helps. If you have any questions, feel free to comment here or on our forums.

Regards,

Unni

Comments

  • Anonymous
    April 09, 2008
    p { margin: 1em 0; } .style1 { background-color: #ecf4fd; } While default WPF (and Silverlight) projects

  • Anonymous
    April 09, 2008
    p { margin: 1em 0; } .style1 { background-color: #ecf4fd; } While default WPF (and Silverlight) projects

  • Anonymous
    March 09, 2009
    We had problems with this on our build server... We ended up doing the following instead of what is mentioned above: <ApplicationDefinition Condition="'$(DesignTime)'=='true' AND '$(BuildingInsideVisualStudio)'!='true' AND '$(BuildingInsideExpressionBlend)'!='true' AND '$(BuildingSolutionFile)'!='true'" Include="App.xaml">      <Generator>MSBuild:Compile</Generator>      <SubType>Designer</SubType> </ApplicationDefinition> Cheers, Dean

  • Anonymous
    March 31, 2009
    A number of people have sought more clarity around how Blend and resources references work inside WPF

  • Anonymous
    April 18, 2009
    Alternative approaches that should work with the designer are described here: http://www.codeplex.com/CompositeExtensions/Thread/View.aspx?ThreadId=42919 You might find this information useful too.

  • Anonymous
    May 12, 2010
    We are using Prism 2.1 for a WPF application and recently started using Blend 3 to edit the views - only to discover that it would not display the UI, only the XAML. By adding the App.xaml file with the ResourceDictionary references and modifying the project file with the conditions provided in this post (and Dean Ward's reply), we can now see our UI in Blend 3. Thank you for this work around.

  • Anonymous
    May 25, 2010
    Great Article, Unni! We are working on a huge project using Composite WPF (Prism) 2 with MVVM. This article realy shined light into the lives of our designers and everyone else involved in constructing the user interface. Thanks!