2

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

Community
  • 1
  • 1
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • you need to do sth in your `OnStartup()`Method... MainWindow w = new MainWindow(); w.Show(); ?! – Felix D. Sep 07 '16 at 12:00
  • @Motivated But the debugger never actually reaches that method – Bassie Sep 07 '16 at 12:01
  • Possible duplicate of [How to start WPF based on Arguments](http://stackoverflow.com/questions/11769113/how-to-start-wpf-based-on-arguments) – ASh Sep 07 '16 at 12:16
  • this can fix your breakpoint issue: http://stackoverflow.com/a/2155997/4610605 – Felix D. Sep 07 '16 at 12:51
  • @Motivated I tried the answer there but it doesn't help at all, as the only module it says the symbols are not loaded for is `Microsoft.VisualStudio.Debugger.Runtime.pdb`, and I cannot find this file anywhere! additionally, I have other projects which can't load these same symbols but they are still able to use breakpoints ...; – Bassie Sep 07 '16 at 13:25
  • All I can imagine is setting up a new solution and add ur code step by step ... – Felix D. Sep 07 '16 at 14:25
  • @Motivated Yes it seems that is the only way but it doesn't really solve the problem. Maybe the best thing to do is raise it with Microsoft .. – Bassie Sep 07 '16 at 14:44
  • @Motivated I managed to work out what was going wrong : http://stackoverflow.com/questions/39373630/break-points-only-working-when-project-run-from-certain-locations/39374255#39374255 – Bassie Sep 07 '16 at 16:05

2 Answers2

2

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

Community
  • 1
  • 1
FreeMan
  • 1,417
  • 14
  • 20
2

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();
}
Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • It turns out that there is actually some other issue and I cannot step through the code at all. All of my break points have this message: `The breakpoint will not currently be hit. No symbols have been loaded for this document`. Extremely annoying but apart from this the application seems to be working – Bassie Sep 07 '16 at 12:18
  • Are you running in `Debug` or `Release` mode ? – Felix D. Sep 07 '16 at 12:45
  • You can try to Clean and Rebuild your project. Somethimes this helps ! – Felix D. Sep 07 '16 at 12:46
  • I'm running in `Debug` mode - tried cleaning it but no luck! – Bassie Sep 07 '16 at 12:58