• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1447
  • Last Modified:

How do I properly define a custom WPF application in code?

I want to be able to instantiate certain classes,etc. when I start my WPF app. I initially had the application and window classes auto-generated when I created my project, but they did not seem to give me access to startup events.

To this end, I have tried to create my own startup class, but I am not having much luck. I tried to create a class that inherits from 'Application' and defined a Main() entry point. The app launches but does not seem to fire the Startup event.

How does one go about doing this so that I have full control over my WPF app and can have access to the various application events like Startup?


Class GKMEMRStartup
    Inherits Application
 
    Shared Sub Main()
        Dim app As New Application
        Dim MainWindow As New Window
        app.Run(MainWindow)
    End Sub
 
    Private Sub GKMEMRStartup_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles Me.Startup
        Debug.writeline("Startup")
    End Sub
End Class

Open in new window

0
kkamm
Asked:
kkamm
  • 5
  • 4
1 Solution
 
unmeshdaveCommented:
what exactly you want to do in startup? I mean give an example, what exactly you want to do berfore window appears... btw, in XAML when you enter space in window tab it will list all the events you can handle... see attached image
window-events.png
0
 
kkammAuthor Commented:
I want to be able to check for network connectivity, run security checks, instantiate custom business objects,etc.

This is a database-oriented application and I would like to launch a different Main window if no server is available. I would also like to predefine access to various controls based on the credentials of the user.

I knew about the event listing, but the StartUp event I need is part of the Application class. I created a custom class that inherits from Application and the Startup event is listed for my derived class. I set up the handler for it, but apparently it is just not being called. Activated and LoadCompleted are also not responding.
0
 
unmeshdaveCommented:
check this,
this is from your App.xaml (check I added as Startup="Application_Startup">
===== App.xaml=====
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml" Startup="Application_Startup">
<Application.Resources>

</Application.Resources>
</Application>
==App.xaml.cs=====

public partial class App : Application
{
          private void Application_Startup(object sender, StartupEventArgs e)
           {
                  MessageBox.Show("Test");
           }
}
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
kkammAuthor Commented:
I had set up a different class for my startup object, but I already had Application.xaml and Application.vb so I am using that.

However...

I am now getting THIS error:

Error      1      Class 'GKMedsoftEMRApplication' must implement 'Sub InitializeComponent()' for interface 'System.Windows.Markup.IComponentConnector'.      C:\Documents and Settings\bgv\My Documents\Visual Studio 2008\VS 2008 Projects\GKMEMRBusinessObjects\GKMEMR_Presentation\obj\Debug\Application.g.i.vb      42      16      GKMEMR_Main

I have listed my Application code below. Not quite sure how to remedy this.

<Application x:Class="GKMedsoftEMRApplication"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        
    </Application.Resources>
</Application>
 
 
Imports GKMedsoft.EMR.BusinessObjects
 
Partial Class GKMedsoftEMRApplication
 
    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.
 
    Shared Sub Main()
        Dim app As New Application
        app.MainWindow = New GKEMRMainWindow
        app.MainWindow.Show()
        app.Run()
    End Sub
 
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        Debug.WriteLine(My.Application.Info.Title & " finalized")
    End Sub
 
    Private Sub GKMedsoftEMRApplication_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Debug.WriteLine("App Activated")
    End Sub
 
    Private Sub GKMedsoftEMRApplication_LoadCompleted(ByVal sender As Object, ByVal e As System.Windows.Navigation.NavigationEventArgs) Handles Me.LoadCompleted
        Debug.WriteLine("App Loaded")
    End Sub
 
    Private Sub GKMedsoftEMRApplication_Startup(ByVal sender As Object, ByVal e As System.Windows.StartupEventArgs) Handles Me.Startup
        Debug.WriteLine("App Loaded")
    End Sub
End Class

Open in new window

0
 
unmeshdaveCommented:
first of all , I dont understand the reason for new class which derives from Application class, as all the events you can handle inside application class itself. Just u need to initialize your clas in the main. you can move creating window & window.show to the place whereever u want in your handled events.
one more thing, you should mostly call base class event handlers from ur relevant implementations.
0
 
kkammAuthor Commented:
That was my mistake. No purpose other than I was following an example from a WPF document showing how to do a custom Startup class.

I got rid of the error,etc. and the app is launching, but I STILL have no access to ANY events for the application. In the events browser, at the  top of the code window, it lists (Declarations) and that is it.

I added the vb and xaml code blocks below.

If I drop in this:

Partial Class GKMedsoftEMRApplication Inherits Application

naturally I get a selection of the appropriate events, but they do not fire. I am presuming the XAML  block takes care of defining the application object so the 'Inherits Application' code is not necessary-Is this correct?

Do I need to define the event handlers on the XAML side?

Thanks for your help with this. It should be easy, but I am still confused...
Application.vb:
 
Partial Class GKMedsoftEMRApplication
 
    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.
 
    Shared Sub Main()
        Dim app As New Application
        app.MainWindow = New GKEMRMainWindow
        app.MainWindow.Show()
        app.Run()
    End Sub
 
    Protected Overrides Sub Finalize()
        MyBase.Finalize()
        Debug.WriteLine(My.Application.Info.Title & " finalized")
    End Sub
 
End Class
 
Application.xaml:
 
<Application x:Class="GKMedsoftEMRApplication"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        
    </Application.Resources>
</Application>

Open in new window

0
 
unmeshdaveCommented:

>>Do I need to define the event handlers on the XAML side?  

well, If you are addding event handlers in code, you dont need to define it in XAML. when i say in code, i mean it by example (Event += (Event_Handler)) statements. because for each event, Application object should know, which handler to call. so you have to assign handlers to perticular event. you can do it in code OR XAML.

>> am presuming the XAML <Application> block takes care of defining the application object so the 'Inherits Application' code is not necessary-Is this correct?

It is required. the class which you define should be type of object for which you define it(class). means if you are defining class for Application object, then that class should be of type Application only, in our code it must inherit from Application
0
 
kkammAuthor Commented:
Started from scratch. Created a fresh WPF startup project in my solution, copied over various source files from old project, changed some namespace issues, and set new project as the startup module for solution.

Application_Startup is now handling events. Most of the issues seemed to stem from auto-generated code, which I have had issues with before.

0
 
kkammAuthor Commented:
This follows the format of what ultimately worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now