?
Solved

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

Posted on 2009-02-23
9
Medium Priority
?
1,432 Views
Last Modified: 2013-11-12
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
Comment
Question by:kkamm
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23737778
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
 
LVL 1

Author Comment

by:kkamm
ID: 23738740
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
 
LVL 8

Accepted Solution

by:
unmeshdave earned 1500 total points
ID: 23738838
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:kkamm
ID: 23747485
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
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23747914
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
 
LVL 1

Author Comment

by:kkamm
ID: 23749052
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
 
LVL 8

Expert Comment

by:unmeshdave
ID: 23749479

>>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
 
LVL 1

Author Comment

by:kkamm
ID: 23750177
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
 
LVL 1

Author Closing Comment

by:kkamm
ID: 31550276
This follows the format of what ultimately worked.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question