I have created new WPF Project and what I would like to do is set something to do for an app on Startup and Exit.
In App.xaml.cs
I have added
public void Application_Startup(object sender, StartupEventArgs e)
{
//code here
}
And
private void Application_Exit(object sender, ExitEventArgs e)
{
// code here
}
In App.xaml I have added to generated code:
<Application
...
Startup="Application_Startup"
Exit="Application_Exit"
StartupUri="MainWindow.xaml" >
What I am getting is an error (when I try to build the app):
Error 1 'abc.App' does not contain a definition for
'Application_Startup' and no extension method 'Application_Startup'
accepting a first argument of type 'abc.App' could be found (are you
missing a using directive or an assembly
reference?) D:\abc\App.xaml 4 73 abc
I have tried to comment StartupUri or change it into App.xaml but without effect.
May anyone point me what I am doing wrong in this case ?
edit:
#S.Akbari
I have such a options like in this image.
http://i57.tinypic.com/212z4h3.png ( cannot upload here my picture because of lack of reputation)
I am reading about This Navigate to Event Handler
Remove Application_Startup and Application_Exit events in App.xaml.cs then in App.xaml right click on Application_Startup and select Navigate to Event Handler. Also do this for Application_Exit and then rebuild your project. build should succeeded.
Related
Update 3
This was actually being caused by a Post-Build action I included which uses ILMerge. See here for more details
Update2
It seems this was not directly caused by adding the command-line support, but I still don't know what did cause it. See SO question for more details.
Update
After making the below changes to allow command-line support, I cannot step through the program with this message on all my breakpoints:
The breakpoint will not currently be hit. No symbols have been loaded for this document
I checked this SO answer and found that I am missing the file Microsoft.VisualStudio.Debugger.Runtime.pdb but I have no idea where it has gone ..
Is there any reason why this would happen because of the App.xaml update?
I have a WPF application for which I need to implement command-line arguments.
Following the answer at this SO question, I amended App.xaml to remove the StartUpUri attribute:
<Application x:Class="WpfFileDeleter.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfFileDeleter"
>
<Application.Resources>
</Application.Resources>
</Application>
I then added an override method to App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
e.Args.Contains("MyTriggerArg")
{
// Do some stuff
}
}
But after inserting a break-point at the top of OnStartUp and debugging the application in Visual Studio, it just hangs in a Ready state but never actually allows me to step through the program.
I tried the following values for StartUpUri:
StartUpUri = "App.xaml"
StartUpUri = "App.xaml.cs"
StartUpUri = "App.xaml.cs.OnStartUp"
But the application just throws a "Cannot locate resource" IOException
According to ethicallogics's answer, it is enough to define startup parameter in
app_start event handler. if you delete starupuri from xaml you need to define somethingelse in sratup handler instead of that
How to start WPF based on Arguments
So App.xaml looks like this:
<Application x:Class="MonitorTool.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MonitorTool"
StartupUri="Views/SplashScreen.xaml"
Exit="Application_Exit">
</Application>
in my App.xaml.cs I got this code:
public partial class App : Application
{
private void Application_Exit(object sender, ExitEventArgs e)
{
//Some settings savin here...
}
}
When you use the XAML way for StartUp make sure the Namespace is right. I got my MainWindow.xaml in a folder called Views.
Or you create the startup like this: Startup="Application_Startup"
and create a Method in the App.xaml.cs file. Again check for namespaces to make sure everything is right here.
Since your app is building I guess this should work and you should at least reach this method.
private void Application_Startup(object sender, StartupEventArgs e)
{
MainWindow window = new MainWindow();
window.Show();
}
Note
When working with arguments you don't need to use the override OnStartup()simply do it like this:
private void Application_Startup(object sender, StartupEventArgs e)
{
string[] args = e.Args;
//Check for some value (for/foreach-loop) and do some stuff
MainWindow w = new MainWindow();
w.Show();
}
I am using Visual Studio 2012 C#. I have created a WPF application project with a main window and added a login window to my project. I want to change the startup window to be my login window but can't seem to do so.
I went to the properties but all I see there is Myproject.app - should it not display the forms of my project?
Anyway I have tried running the window from code as well like so :
Application.Run(new Login());
But that does not seem to work. It gives an error saying :
Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Application.Run(System.Windows.Window)'
To change startup window update App.xaml by changing Application.StartupUri:
<Application ... StartupUri="MainWindow.xaml">
To change the startup window programmatically go to App.xaml
remove the line StartupUri="MainWindow.xaml" (This will remove the default startup window configuration), now add the startup event Startup="Application_Startup", in App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
If(somecase)
{
MainWindow mainWindow = new MainWindow ();
mainWindow.Show();
}
else
{
OtherWindow otherWindow= new OtherWindow();
otherWindow.Show();
}
}
use Application.Current.Run Instead of Application.Run
private void frmSelectCategory_Load_1(object sender, EventArgs e)
{
}
I have removed the above function and pressed F5, but it gave me an error and stopped debugging. All controls on the form disappeared. I put this code back and run again and it works, but when I go to coding again, there is nothing on the form.
Here is the error message:
Error 1 'EbayForm.frmSelectCategory' does not contain a definition for 'frmSelectCategory_Load_1' and no extension method 'frmSelectCategory_Load_1' accepting a first argument of type 'EbayForm.frmSelectCategory' could be found (are you missing a using directive or an assembly reference?) C:\Users\Bilgehan\Documents\Visual Studio 2012\Projects\tutorials\EbayForm\EbayForm\frmSelectCategory.Designer.cs 37 55 EbayForm
Removing an event handler from your code-behind file doesn't remove the assignment from the file that's generated by the designer. You can either search for and remove it manually, or wipe out the event handler in the Properties pane.
Open the form in the designer. Select properties and clear the event for load.
Alternatively:
Search for all references of frmSelectCategory_Load_1 and removed them.
I am trying to use a .NET 4 SplashScreen in a Prism based WPF application. I have used the SpashScreen by setting the build action on the image to SplashScreen.
The application used to keep on crashing with a System.Resources.MissingManifestResourceException. Finally I figured out that if I add a StartupUri="MainWindow.xaml" in the App.Xaml file, the SplashScreen works fine.
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
</Application>
But in a prism application, we cannot have a StartupUri. Everything is done in the Bootstrapper.
So what do I need to do manually that StartupUri did to make the SplashScreen work?
Update 1: The complete exception message is:
System.Resources.MissingManifestResourceException was unhandled
Message=Could not find any resources appropriate for the specified culture or the neutral culture. Make sure
"Application.g.resources" was correctly embedded or linked into
assembly "Application" at compile time, or that all the satellite
assemblies required are loadable and fully signed.
Update 2:
I have figured out the adding or removing the StartupUri does not matter. What matters is that I have an additional WPF Window (other than App.xaml) or 2 dummy entries in the App.Resources tag.
<Application.Resources>
<Style x:Key="Dummy"/>
<Style x:Key="Dummy1"/>
</Application.Resources>
If I do not do this, the Application.g.resources file is not created in obj file and hence not embedded in the executable.
Adding two dummy resource entries was brought to my attention by this blog post.
Update 3:
My question was answered by Bob Bao on MSDN forum here. Also It seems Kent was trying to point me in the same direction.
Do not set the build action of the image to SplashScreen. Instead:
Add the code in the App OnStartup method as:
protected override void OnStartup(StartupEventArgs e)
{
SplashScreen splashScreen = new SplashScreen("splashscreen.png");
splashScreen.Show(true);
base.OnStartup(e);
Bootstrapper bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
"splashscreen.png" is one image in the project, and its "Build Action"
is "Resource".
Simply define your own entry point which firstly shows the splash screen and then bootstraps Prism. In your project properties, set the entry point to your custom entry point.
internal static class Entry
{
public static void Main(string[] args)
{
var splashScreen = ...;
splashScreen.Show();
var bootstrapper = ...;
bootstrapper....;
}
}
Please check this adress : http://prismsplashscreen.codeplex.com/
There is a full example with prism
I'm trying to write a silverlight application that takes in InitParams and then uses those InitParams to make a change to the Source of the MediaElement on the page. I'm trying to figure out the proper place to put my code.
I watched Tim Heuer's excellent video on InitParams, but in the video (which was for Silverlight 2), it shows the following on the Page.xaml.cs:
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
I don't see Page_Loaded when I open MainPage.xaml.cs, and I'm wondering if that was automatically created in the Silverlight 2 SDK and left out of the Silverlight 3 SDK. Or perhaps Tim added that in his video manually.
I find that I can go into the opening UserControl tag of MainPage.xaml and add Loaded="<New_Event_Handler>" which creates the following in MainPage.xaml.cs:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
By default, there's also the following in MainPage.xaml.cs, which is run during the Application_Startup event in App.xaml.cs:
public MainPage()
{
InitializeComponent();
}
I need to figure out where is the best place to insert my code to change the Source on my MediaElement in my xaml. Should I put it in MainPage? Should I add the Loaded event handler and put it into UserControl_Loaded? If it's supposed to be Page_Loaded, where do I find that in Silverlight 3?
Any help would be much appreciated.
"UserControl_Loaded" and "Page_Loaded" are just method names and the names don't matter (you could name the method "Foo" if you wanted). What makes these methods do anything is the fact that they are attached to the Loaded event on the UserControl (which is what you did when you edited the MainPage.xaml file).