Exit current action with Application.Current.Shutdown

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AppEventsSample" Height="300" Width="300">
    <Grid>
    <Button Height="23" x:Name="shutdownButton" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75">Shutdown</Button>
    <Label Height="25" HorizontalAlignment="Left" Margin="10" x:Name="label1" VerticalAlignment="Top" Width="100">Application Events:</Label>
    <ListBox Margin="20" x:Name="eventsListBox" ItemsSource="{Binding}" />
    <Button Height="23" Margin="10" Name="exceptionButton" VerticalAlignment="Bottom">Exception</Button>
  </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
     // this.DataContext = ((App)Application.Current).EventLog;
    }

    protected override void OnInitialized(EventArgs e) {
      base.OnInitialized(e);
      exceptionButton.Click += new RoutedEventHandler(exceptionButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
    }

    void exceptionButton_Click(object sender, RoutedEventArgs e) {
      throw new Exception("Opps! Unhandled exception!");
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown(111);
    }

  }
}

   
    
     


Set and get data from Application.Current.Properties

image_pdfimage_print


   
  

<Window x:Class="BookApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BookApp" Height="300" Width="300">
    <Grid>
      <Button Click="MyClickEvent" Name="btnGo">Go</Button>
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BookApp
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
            Book x = new Book();
            x.author = "A";
            x.title = "title";
            x.price = (decimal)9.99;
            Application.Current.Properties["Book"] = x;
        }

        private void MyClickEvent(object sender, RoutedEventArgs e)
        {
            Book y = (Book)Application.Current.Properties["Book"];
            MessageBox.Show(y.title, y.author, MessageBoxButton.OK,MessageBoxImage.Hand);
        }
    }
    public class Book
    {
        public decimal price;
        public string title;
        public string author;
    }

}

   
    
     


Localizable Application by putting localized resource in Xaml

image_pdfimage_print


   
  
<Window x:Uid="Window_1" x:Class="LocalizableApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="LocalizableApplication" Height="300" Width="300"
    SizeToContent="WidthAndHeight"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    >
  <Window.Resources>
    <s:String x:Uid="s:String_1" x:Key="Error">Message</s:String>
  </Window.Resources>
  
  <StackPanel x:Uid="StackPanel_1">
    <TextBlock x:Uid="TextBlock_1">One line of text.</TextBlock>
    <Button x:Uid="cmdDoSomething" Click="cmd_Click" Name="cmdDoSomething">A button</Button>
    <TextBlock x:Uid="TextBlock_2">This is another line of text.</TextBlock>
    
  </StackPanel>
      
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
using System.Threading;


namespace LocalizableApplication
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        protected void cmd_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.Resources["Error"].ToString());
        }
    }
}

   
    
     


Application NavigationFailed event

image_pdfimage_print
   
  
<Application x:Class="NavigationApplication.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="StartupWindow.xaml" NavigationFailed="App_NavigationFailed">
</Application>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Windows.Threading;
using System.Windows.Navigation;

namespace NavigationApplication
{
    public partial class App : System.Windows.Application
    {
        private void App_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (e.Exception is System.Net.WebException)
            {                
                MessageBox.Show("Website " + e.Uri.ToString() + " cannot be reached.");
                e.Handled = true;                
            }
        }     
    }
}

   
    
     


Application Events Sample

image_pdfimage_print
   
  

<Application x:Class="AppEventsSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Windows.Threading;

namespace AppEventsSample {
  public partial class App : Application {
    public readonly ObservableCollection<string> EventLog = new ObservableCollection<string>();

    public App() {
      this.Activated += new EventHandler(App_Activated);
      this.Deactivated += new EventHandler(App_Deactivated);
      this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
      this.Exit += new ExitEventHandler(App_Exit);
      this.SessionEnding += new SessionEndingCancelEventHandler(App_SessionEnding);
      this.Startup += new StartupEventHandler(App_Startup);

      File.Delete(@"c:a.txt");
      EventLog.CollectionChanged += EventLog_CollectionChanged;
      EventLog.Add("App()");
    }

    void EventLog_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
      using (StreamWriter writer = new StreamWriter(@"c:a.txt", true)) {
        foreach (string item in e.NewItems) { writer.WriteLine(item); }
      }
    }

    void App_Startup(object sender, StartupEventArgs e) {
      StringBuilder args = new StringBuilder();
      for (int i = 0; i != e.Args.Length; ++i) {
        args.AppendFormat("arg[{0}]: &#039;{1}&#039; ", i, e.Args[i]);
      }

      EventLog.Add("App_Startup: args= " + args);
    }

    void App_SessionEnding(object sender, SessionEndingCancelEventArgs e) {
      EventLog.Add("App_SessionEnding: reason= " + e.ReasonSessionEnding.ToString());

      if (MessageBox.Show(e.ReasonSessionEnding.ToString(), "Session Ending", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) {
        e.Cancel = true;
      }
    }

    void App_Exit(object sender, ExitEventArgs e) {
      EventLog.Add("App_Exit: exit code=" + e.ApplicationExitCode.ToString());
    }

    void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
      EventLog.Add("App_DispatcherUnhandledException: " + e.Exception.Message);

      if (MessageBox.Show("Is exception handled?
" + e.Exception.Message, "Exception", MessageBoxButton.YesNo) == MessageBoxResult.Yes) {
        e.Handled = true;
      }
    }

    void App_Activated(object sender, EventArgs e) {
      EventLog.Add("App_Activated");
    }

    void App_Deactivated(object sender, EventArgs e) {
      EventLog.Add("App_Deactivated");
    }

  }
}

   
    
     


Single Instance Sample

image_pdfimage_print
   
  

<Application x:Class="SingleInstanceSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
         
    </Application.Resources>
</Application>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Threading;
using System.Reflection;

namespace SingleInstanceSample {
  public partial class App : System.Windows.Application {
    Mutex mutex;
    protected override void OnStartup(StartupEventArgs e) {
      base.OnStartup(e);

      string mutexName = "MyCompanyName.MyAppName";
      bool createdNew;
      mutex = new Mutex(true, mutexName, out createdNew);

      if( !createdNew ) { Shutdown(); }
    }
  }
}

   
    
     


StartupUri attribute

image_pdfimage_print
   
  
<Application x:Class="SimpleXamlApp.MyApp"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml" Exit="AppExit"
    >
</Application>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace SimpleXamlApp
{
  public partial class MyApp : Application
  {
    void AppExit(object sender, ExitEventArgs e)
    {
      MessageBox.Show("App has exited");
      }
  }
}