<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;
}
}
}
Windows Presentation Foundation
StartupUri attribute
<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");
}
}
}
Single Instance Sample
<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(); }
}
}
}
Application Events Sample
<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}]: '{1}' ", 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");
}
}
}
Application NavigationFailed event
<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;
}
}
}
}
Localizable Application by putting localized resource in Xaml
<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());
}
}
}
Set and get data from Application.Current.Properties
<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;
}
}