serhio
serhio

Reputation: 28586

App.XAML where are you?

I am new in WPF. Created a new WPF UserControl. See that some people uses an app.xaml file in order to set inside application level ressources.

My solution consists of a WinForm and a WPF UserControl. I don't see somewhere any app.xaml file.

How to proceed?

Upvotes: 7

Views: 5809

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1499770

App.xaml is associated with a WPF application. If you've only got a UserControl, there's no application for it to be part of, is there?

Create a WPF application and you'll have an App.xaml to put application-level resources in.

Out of interest, why do you have WinForm if you're using a WPF user control?

EDIT: To repeat my comment: you're not going be provided with WPF Application resources smoothly when you're not creating a WPF application.

EDIT: As noted in Anthony Brien's answer, it seems you can hack it around - but I would strongly recommend against this sort of thing if you can possibly help it. Fundamentally, you're working against the expectations of the platform - and that's never a nice situation to be in.

Upvotes: 6

Eric J.
Eric J.

Reputation: 150108

This article explains in great detail how to have application wide resources in a hosted/interop scenario:

http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/

The solution path I chose (from the article) is to include App.xaml as a page element

Modify project file:

<Page Include="App.xaml" />
<Compile Include="App.xaml.cs">
  <DependentUpon>App.xaml</DependentUpon>
  <SubType>Code</SubType>
</Compile>

Add code:

public App()
{
    InitializeComponent();
}

public static void EnsureApplicationResources()
{
    if (Application.Current == null)
    {
        // create the Application object
        new App();
    }
}

Full details are in the article.

Upvotes: 2

Anthony Brien
Anthony Brien

Reputation: 6166

It is possible to have application wide WPF resources in a WinForms application. Look at http://www.wpftutorial.net/AppLevelResourcesWinForms.html

Upvotes: 6

Martin Hennings
Martin Hennings

Reputation: 16846

If you created a WinForm project, it will not have a app.xaml.

Create a WPF project instead.

Upvotes: 3

vc 74
vc 74

Reputation: 38179

You need to create a WPF or Silverlight application to get that file You are actually hosting a WPF control in a Winforms application

Upvotes: 3

Related Questions