Security Exception


   
  

<Page x:Class="WpfApplication1.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1">
    <Grid Height="379" Width="361">
    </Grid>
</Page>

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

namespace WpfApplication1
{
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();

            StreamWriter SW;
            SW = File.CreateText("c:s.txt");
            SW.WriteLine("A!");
            SW.WriteLine("B");
            SW.Close(); 
            
        }

    }
}

   
    
     


Raise exception from button click event


   
  

<Window x:Class="DispatcherUnhandledExceptionSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DispatcherUnhandledException Sample" Height="300" Width="300">
  <StackPanel>
    <Button Name="raiseRecoverableException" Click="raiseRecoverableException_Click">Raise Recoverable Exception</Button>
    <Button Name="raiseUnrecoverableException" Click="raiseUnecoverableException_Click">Raise Unrecoverable Exception</Button>
  </StackPanel>
</Window>

//File:MainWindow.xaml.cs
using System;
using System.Windows;

namespace DispatcherUnhandledExceptionSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        void raiseRecoverableException_Click(object sender, RoutedEventArgs e)
        {
            throw new DivideByZeroException("Recoverable Exception");
        }

        void raiseUnecoverableException_Click(object sender, RoutedEventArgs e)
        {
            throw new ArgumentNullException("Unrecoverable Exception");
        }
    }
}

   
    
     


Check Exception type for DispatcherUnhandledException

   
  
<Application 
    x:Class="DispatcherUnhandledExceptionSample.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml" 
    DispatcherUnhandledException="App_DispatcherUnhandledException" />

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

namespace DispatcherUnhandledExceptionSample
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            bool shutdown = false;

            if (e.Exception is DivideByZeroException)
            {
                shutdown = false;
            }
            else if (e.Exception is ArgumentNullException)
            {
                shutdown = true;
            }

            if (shutdown)
            {
                MessageBoxResult result = MessageBox.Show("Application must exit:

" + e.Exception.Message + "

Save before exit?", "app", MessageBoxButton.YesNo, MessageBoxImage.Error);
                if (result == MessageBoxResult.Yes)
                {
                }
                EventLog.WriteEntry("app", "Unrecoverable Exception: " + e.Exception.Message, EventLogEntryType.Error);
                this.Shutdown(-1);
            }
            e.Handled = true;
        }
    }
}

   
    
     


Cross Thread Exception Raising WorkerThread

   
  

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.App"
  StartupUri="MainWindow.xaml" 
  DispatcherUnhandledException="App_DispatcherUnhandledException" />

//File:Window.xaml.cs
using System; 
using System.Text;
using System.Windows;
using System.Windows.Threading; 

namespace WpfApplication1
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.Exception.InnerException.Message);
            Console.WriteLine(e.Exception.Message);
            Console.WriteLine(e.Dispatcher.Thread.ManagedThreadId);
         

            e.Handled = true;
        }
    }
}

   
    
     


Cross Thread Exception Raising

   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.SecondaryUiThreadWindow"
  Height="200" Width="400" Closed="SecondaryUiThreadWindow_Closed">
  <Button Click="r">Raise an Exception on the Secondary UI Thread</Button>

</Window>
//File:Window.xaml.cs
using System; 
using System.Threading;
using System.Windows; 
using System.Windows.Threading;

namespace WpfApplication1
{
    public partial class SecondaryUiThreadWindow : Window
    {
        public SecondaryUiThreadWindow()
        {
            InitializeComponent();

            this.Title = Thread.CurrentThread.ManagedThreadId+"";
        }
        void r(object sender, RoutedEventArgs e)
        {
            throw new Exception(Dispatcher.CurrentDispatcher.Thread.ManagedThreadId+"");
        }
        void SecondaryUiThreadWindow_Closed(object sender, EventArgs e)
        {
            Dispatcher.CurrentDispatcher.InvokeShutdown();
        }
    }
}

   
    
     


Trigger Property=FrameworkElement.IsFocused


   
     

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Page.Resources>
        <Style x:Key="widthOnFocus">
          <Style.Triggers>
            <Trigger Property="FrameworkElement.IsFocused" Value="True">
              <Trigger.EnterActions>
                <BeginStoryboard Name="changeWidth">
                  <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Width)" To="300" Duration="0:0:5" />
                  </Storyboard>
                </BeginStoryboard>
              </Trigger.EnterActions>
              <Trigger.ExitActions>
                <RemoveStoryboard BeginStoryboardName="changeWidth" />
              </Trigger.ExitActions>
            </Trigger>
          </Style.Triggers>
        </Style>
    </Page.Resources>
    
    <StackPanel>
      <TextBox Style="{StaticResource widthOnFocus}" Width="200" />
    </StackPanel>


</Page>

   
    
    
    
    
     


Playing a sound with a trigger


   
     

        

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

        <Rectangle Fill="Green">
          <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Rectangle.MouseEnter">
              <SoundPlayerAction Source="c:windowsmediading.wav" />
            </EventTrigger>
          </Rectangle.Triggers>
        </Rectangle>
</Page>