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");
      }
  }
}

   
    
     


Using GetContentStream

image_pdfimage_print
   
  

<Window x:Class="BinaryResources.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BinaryResources" Height="300" Width="300">
    <Grid>
      <Image Source="c:image.jpg" />
    </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;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Threading;
using System.Resources;
using System.Collections;
using System.Windows.Resources;

namespace BinaryResources
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

            Uri resourcePath = new Uri("file:///c:/image.jpg", UriKind.Relative);
            StreamResourceInfo ri = Application.GetContentStream(resourcePath);
            Stream data = ri.Stream;


            // data;
        }
    }
}

   
    
     


Create and retrieve cookies from a Windows Presentation Foundation (WPF) application using SetCookie and GetCookie.

image_pdfimage_print


   
  

<Page x:Class="CookiesSampleCSharp.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowTitle="Cookies Sample">
  <StackPanel>
    <Label>Set Cookie Value:</Label>
    <TextBox Name="setCookieValueTextBox">Cookie1=Value1</TextBox>
    <Button HorizontalAlignment="Right" Name="setCookieButton" Click="setCookieButton_Click">Set Cookie</Button>
    <Label>Get Cookie Value:</Label>
    <TextBox Name="getCookieValueTextBox"></TextBox>
    <Button HorizontalAlignment="Right" Name="getCookieButton" Click="getCookieButton_Click">Get Cookie</Button>

  </StackPanel>
  
</Page>

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

namespace CookiesSampleCSharp
{
    public partial class HomePage : System.Windows.Controls.Page
    {
        public HomePage()
        {
            InitializeComponent();
        }

        void setCookieButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Application.SetCookie(BrowserInteropHelper.Source, this.setCookieValueTextBox.Text);
            }
            catch (Win32Exception ex)
            {
                MessageBox.Show(ex.Message + " (Native Error Code=" + ex.NativeErrorCode + ")");
            }
        }

        void getCookieButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.getCookieValueTextBox.Text = Application.GetCookie(BrowserInteropHelper.Source);
            }
            catch (Win32Exception ex)
            {
                MessageBox.Show(ex.Message + " (Native Error Code=" + ex.NativeErrorCode + ")");
            }
        }
    }
}

   
    
     


Get a handle to the current app and shut it down

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="My Xaml App" Height="200" Width="300" WindowStartupLocation ="CenterScreen">
  <Button Width="133" Height="24" Name="btnExitApp" Click ="btnExitApp_Clicked">
    Exit Application
  </Button>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }
    
      private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
      {
        Application.Current.Shutdown();
      }
  }
}