Entwickler-Ecke

WPF / Silverlight - WPF-Anfängerfrage: Wie kann ich App.xaml.cs nutzen?


mannyk - Mi 16.03.11 13:57
Titel: WPF-Anfängerfrage: Wie kann ich App.xaml.cs nutzen?
Hallo,

ich habe ein WPF Projekt angelegt und dafür werden ja zwei Xaml-Komponenten angelegt.
Zum einen die App.xaml (Einstiegspunkt) und das Hauptfenster Window.xaml.

Nun meine Frage: Ich muss beim Programmstart ein paar Initialisierungen vornehmen, die unabhängig vom Hauptfenster Window.xaml(.cs) sind. Kann ich diese in App.xaml.cs vornehemen? Wenn ja, wie?
Der einzige Code hier ist:

C#-Quelltext
1:
2:
3:
4:
    public partial class App : Application
    {

    }

In der App.xaml steht folgendes:

XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
<Application
    x:Class="TestProject.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>


IsNull - Fr 18.03.11 15:56

Ja, so ist es vorgesehen. Du registrierst den Startup-Event und fügst dort deine eigene Logik ein:

ins app xaml: StartUp="Application_Startup"

XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
<Application
    x:Class="TestProject.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartUp="Application_Startup"
    StartupUri="Window.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>


und in deinem App.xaml.cs:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        void Application_Startup(object sender, StartupEventArgs e){
            // your custom init code
        }
    }


mannyk - Fr 18.03.11 16:05

Danke, perfekt