How can I provide my own Main() method in my WPF application?
When you create a WPF application in Visual Studio the 'entry point' for the process, where the application is created and the first form displayed, is not directly apparent. Instead the start-up behaviour is controlled by setting attributes on the App.xaml, such as which form to start. At compile-time WPF generates the real entry point method using the classic static void Main() method signature in C#, or Public Shared Sub Main() in VB.NET. Inside the Main() method WPF creates an instance of one of your classes which derive from System.Windows.Application, and calls the blocking Run() method on this Application-derived class which keeps the process "alive". Generally this is a useful abstraction, hiding away some of the lower-level details of how the WPF application starts up, however sometimes you want to provide your own version of the Main() method. So how do you tell WPF to use your main method and not its own?
The way WPF knows to create the Main() method for a particular xaml file is through the build action property set for App.xaml - it has a build action of ApplicationDefinition. By changing this to Page WPF won't create the Main method and you can provide your own in a regular class file you add to the project.
It is important to note that when you provide your own Main() method that the method should be adorned with the STAThread attribute, which sets the threading model for the process. You can either look at the current generated Main() method for your project as a starting point (it should be in the obj folder) or use the following code:
[System.STAThreadAttribute()]
static void Main()
{
YourApplicationName app = new YourApplicationName();
app.InitializeComponent();
app.Run();
}
Categories:
Comments:
#
OnStartup?
posted by
Tim Hibbard
on
12/14/2007 3:02:06 AM
:
Is there a reason this is better than overriding OnStartup in App.xaml.cs?
#
about this...
posted by
Corrado Cavalli
on
12/14/2007 8:01:01 AM
:
Unfortunately your solution breaks both VS2008 and Expression Blend. Application wide resources are recognized only at runtime and Blend complains about missing startup object when you press F5.
I'd stay with App constructor or OnStartup override.
#
Breaking VS2008
posted by
JosephCooney
on
12/14/2007 10:55:16 PM
:
You're right Corrado, it does break the designer in VS2008.....I guess if I ever used the designer I would have noticed this. Another approach if you want to put something in the Main() method is to create a class with the main method in it, leave the app.xaml build type the way it is, and specify the startup object in the Application tab for the properties for the project. This works sometimes in the designer, and works fine in blend.
#
i didnt understand
posted by
פמוטים
on
2/28/2008 10:19:47 PM
:
can ypu please rewrite it all again??
#
Keep App.xaml
posted by
Anon
on
7/3/2008 4:17:32 AM
:
Here's how to keep your App.xaml file for the designer while creating your own main entry point elsewhere:
blogs.msdn.com/expression/archive/2008/04/09/creating-a-wpf-blend-project-that-loads-resources-in-code.aspx
#
Is there any new stuff in this field?
posted by
#
Is there any new stuff in this field?
posted by
#
very simple
posted by
בשמים
on
3/23/2009 8:26:31 AM
:
Any thing new?
#
The complete instruction re App.xaml
posted by
PandaWood
on
4/16/2009 9:38:13 PM
:
App.xaml also has to be deleted not just set to 'Page' from 'ApplicationDefinition'. I don't know precisely what in my App.xaml file caused it, but App.g.cs was still being generated even after setting 'ApplicationDefinition' to Page.
Only when I deleted App.xaml did everything compile (no duplicate Main defintion etc)
#
cant find the resource set in the App.xaml
posted by
shafqat
on
8/17/2009 4:47:01 PM
:
i have set buildAction property to page... and define my own main method.. but when i run the applicaiton it crashes.. it cant find the resources define in the app.xaml
please let me know what else i am missing..
thanks
#
links of london
posted by
links of london
on
10/27/2009 7:26:27 PM
:
It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.
#
support
posted by
hanyujoys
on
11/10/2009 2:40:18 PM
:
Support of the Lou Zhu, Lou Zhu worked hard
Signature--------------------------------------------------------------------------------------------------------------------
Nothing is impossible for a willing heart.
[url=
http://www.uggshelf.com/Products.html][color=black]ugg boots[/color][/url]
Signature--------------------------------------------------------------------------------------------------------------------
Nothing is impossible for a willing heart.
[url=
http://www.uggshelf.com/UGG-Bailey-Button/View-all-products.html][color=black]ugg bailey button[/color][/url]
#
timberland kids boots
posted by
classic timberland boots
on
11/12/2009 5:14:22 PM
:
After seven days spent in the classic timberland boots meadow the souls had to set out again on the eighth and came in four days to a timberland kids boots place from which they could see a shaft of light stretching from above straight through earth and heaven, like a discount timberland boots pillar, closely resembling a rainbow, only brighter and clearer; this they reached after a timberland womens boots, further day's journey and saw there in the timberland men’s boots middle of the light stretching from the heaven the ends of the bonds of it; for this light is the bond of heaven and holds its whole circumference together, like the swifter of a trireme.
http://www.officialtimberland.com/products_new.html
#
ugg boots ultra
posted by
ugg bailey button sale
on
11/17/2009 2:42:01 AM
:
After seven days spent in the ugg boots ultra meadow the souls had to set out again on the eighth and came in four days to a ugg bailey button sale place from which they could see a shaft of light stretching from above straight through earth and heaven, like a uggs bailey button pillar, closely resembling a rainbow, only brighter and clearer; this they reached after a classic tall uggs further day's journey and saw there in the uggs classic short middle of the light stretching from the heaven the ends of the bonds of it; for this light is the bond of heaven and holds its whole circumference together, like the swifter of a trireme.
http://www.bayuggs.com/specials.html
#
www.nikejordansale.com
posted by
mbt shoes clearance
on
11/21/2009 5:29:04 AM
:
welcome to our website:
http://www.nikejordansale.com ,we hope become your reliable online cheap goods supplier in china.Authentic goods with original box,we insists tha"Customer the highest,Quality first!"Our products include mbt shoes clearance,ugg classic tall sale,mens air jordan shoes and other cheap goods.We are looking forward to building sincere and long-term business relationships with you.
#
uggs on sale
posted by
uggs on sale
on
11/25/2009 7:56:01 PM
:
S11125L2UGG popular star in Hollywood several years ago had spread to uggs on sale mean the improvement of people's lives and as a force to follow the ugg boots uk star to wear UGG's wave of school is becoming everyone's most interesting content. Here Ai Sa Kaka on the boots on sale popular UGG fashion for up to one sorted out a set of current Hollywood's most sought-after actress of the ugg bailey boots UGG shoes, street beat, let's take a look at what they are wearing UGG's Bar.
http://www.uggbootssaleonline.com
#
timberland work boots
posted by
timberland work boots
on
11/26/2009 2:29:56 AM
:
S11125I2 The son of wealthy and influential Athenian parents, Plato began his timberland chukka boots philosophical career as a student of Socrates. When the master died, Plato travelled timberland waterproof boots to Egypt and Italy, studied with students of Pythagoras, and spent timberland work boots several years advising the ruling family of Syracuse. Eventually, he returned timberland custom boots to Athens and established his own school of philosophy at the Academy.For students enrolled there, Plato tried timberland high top boots both to pass on the heritage of a Socratic style of thinking and to guide their progress through mathematical learning to the achievement of abstract philosophical truth.
http://www.officalsale.com/specials.html
#
http://www.ladyugs.com/specials.html
posted by
Ugg Classic Boots
on
11/26/2009 1:21:22 PM
:
S11125H2 Towering genius disdains a beaten path with discount ugg boots. It seeks regions hitherto unexplored.A man may lead a horse to the water, but he cannot make it drink and walk with ugg classic tall .Victory wil not come to ugg lo pro button unless I go to ugg classic cardy.Goals determine what you are going to ,for example, buy ugg knightsbridge.
http://www.ladyugs.com/specials.html
#
uggs on sale
posted by
uggs on sale
on
11/28/2009 7:35:22 PM
:
S11128L2UGG popular star in Hollywood several years ago had spread to uggs on sale mean the improvement of people's lives and as a force to follow the ugg boots uk star to wear UGG's wave of school is becoming everyone's most interesting content. Here Ai Sa Kaka on the boots on sale popular UGG fashion for up to one sorted out a set of current Hollywood's most sought-after actress of the ugg bailey boots UGG shoes, street beat, let's take a look at what they are wearing UGG's Bar.
http://www.uggbootssaleonline.com
#
ed hardy clothing,ed hardy coat
posted by
edfashionclothes
on
12/5/2009 1:35:24 PM
:
Thank you for the message!With the advent of Christmas and to enlarge and refresh our market,we encourage both newcomers and loyal customers to browse our ugg boots sale promotions.The price of our ugg classic argyle knit,ugg bailey button or ugg classic cardy are lowest than anytime and anywhere,there are also lots ugg mayfaire boots and ugg sundance II boots that you can choose.If the goods you choose are more than $100,you can pick up a gift Free.Time Limited, Don't be hesitation,pick up your favorate goods,Free Shipping & Fast Delivery.
http://www.edfashionclothes.com/
#
ugg bailey button,ugg classic argyle knit boots
posted by
nikeaf1jordanshoes
on
12/5/2009 1:35:33 PM
:
Good job!Thank you for your message!It help me so much! Christmas is coming,there are big sales promotions on
http://www.nikeaf1jordanshoes.com/,ugg bailey button and ugg classic argyle knit boots on our site are lower than ever,you may be intersting in ugg mayfaire boots or ugg nightfall boots,ugg sundance II ,come on to choose your favorate goods!
#
ugg bailey button,ugg classic Boots
posted by
uggsnowbootsbest
on
12/5/2009 1:35:42 PM
:
Thank you for the great job!In order to?welcome to Christmas, recently,our company is on ugg boots promotional sales and the?prices of all products such as?ugg sundance II boots or ugg bailey button boots are lower than any time!And the biggest surprise is that?you can buy one get one free,that?is once you buy an ugg mayfaire boots , ugg coquette, or ugg classic crochet,you can get one gift for free.Don't hesitate, to get?a free present, check it on
http://www.uggsnowbootsbest.com/
#
ugg classic tall
posted by
ugg classic cardy
on
12/17/2009 8:01:51 PM
:
1217htf Progress is the activity of cheap ugg boots today and the assurance of uggs sale tomorrow .The world can be changed as discount ugg boots by man's endeavor for ugg classic cardy, and that this endeavor can lead to something new ugg classic tall and better ugg bailey button . Remember ugg classic mini,The man who has made up his mind to win ugg classic short will never say 'impossible' for uk ugg boots . For victory won't come to me with ugg classic crochet unless I go to it for usa ugg boots.
http://www.ugganow.com/
#
Cardy Ugg Boots
posted by
Cardy Ugg Boots
on
1/8/2010 4:35:28 PM
:
slLAH100108 I think this blog is good,so now I wanna introduce mbt to you.{{[MBT Chapa shoes->
http://www.cutembt.com/mbt-chapa-shoes-c-71.html]}} are a growing and popular anti-shoe inspired by the Masai people. The Masai people are known for their athletic posture and gait. It is known that none of the Masai people struggle with any back pain due to walking on hard surfaces,so they try {{[MBT Women M.Walk shoes->
http://www.cutembt.com/mbt-women-mwalk-shoes-c-42.html]}}. At last,they found the benefit of MBT Women M.Walk shoes, they trust mbt shoes.
#
uggs on sale
posted by
ugg boots
on
1/26/2010 12:00:57 PM
:
100126SLLYH
Our greatest glory uggs boots consists not in never falling but in rising every ugg classic tall time we fall.It is not enough ugg classic boots to be industrious, so are the ants. What are you industrious about ugg classic argyle knit? You have to believe in yourself. That's the ugg bailey button secret of sucess. A wise man never loses anything if he has himself. To be without some of the things you want is an indispensable part of happiness.
http://www.4ezlive.com/