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();
}
Related
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.
I try to implement Splash Screnn in WPF. I have found some nice ehample in MSDN, but there is one place:
private void _applicationInitialize(SplashScreen splashWindow)
{
Thread.Sleep(1000);
// Create the main window, but on the UI thread.
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate
{
Window1 MainWindow = new Window1();
Helper.setWin(MainWindow);
MainWindow.Show();
});
}
The problem is Helper, whats the class is there and how it must be implemented. Someone could paste an example or smth?
There is an even easier way:
http://msdn.microsoft.com/en-us/library/cc656886.aspx
Add the image file to the WPF Application project. For more information, see How to: Add Existing Items to a Project.
In Solution Explorer, select the image.
In the Properties window, click the drop-down arrow for the Build Action property.
Select SplashScreen from the drop-down list
You can use code like this do display an image on startup:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" Startup="Application_Startup">
in the code behind:
private void Application_Startup(object sender, StartupEventArgs e)
{
SplashScreen screen = new SplashScreen("Images/splash.bmp");
screen.Show(true);
}
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
I am a newbie to WPF.I have a problem in developing the WPF application where I want to start a class as start up object,after I want to Show the welcome form. When I was trying to put the main method in that class set the project properties startup object as that class.
I am getting this error "The calling thread must be STA, because many UI components require this.".
How can we resolve this error by making the main method of that class as startup object?
in top App.xaml
Exit="App_Exit"
Startup="App_Startup"
in app.xaml.cs
void App_Startup(object sender, StartupEventArgs e)
private void App_Exit(object sender, ExitEventArgs e)
There other events you can subscribe to, if you are really needing to override App as startup you need define in your Program.cs like this:
public static class Program
{
[STAThread]
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Main(string[] args)
{
And somewhere in there
App app = new App();
app.InitializeComponent();
app.Run();
When you say "welcome form" do you mean Window?
Have you tried setting the StartupUri of the App.xaml?
<Application x:Class="DemoApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="WelcomeWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
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