<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.
<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
<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(); } } }
Bind ApplicationCommand to a handler
<Window x:Class="Commands.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Commands" Height="300" Width="300"> <Grid> <StackPanel> <StackPanel.CommandBindings> <CommandBinding Command="ApplicationCommands.Paste" Executed="InvokeApplicationCommand"/> </StackPanel.CommandBindings> <Button Command="ApplicationCommands.Paste"> Paste Something (Control + V) when I have the focus!" </Button> </StackPanel> </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 Commands { public partial class Window1 : System.Windows.Window { public Window1() { InitializeComponent(); } void InvokeApplicationCommand(object target, ExecutedRoutedEventArgs args) { MessageBox.Show("The ApplicationCommand has been invoked."); } } }
Assign ApplicationCommands.Open to Button
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CommandHandlerProcedural" > </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); StackPanel MainStackPanel = new StackPanel(); this.AddChild(MainStackPanel); Button CommandButton = new Button(); CommandButton.Command = ApplicationCommands.Open; CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"; MainStackPanel.Children.Add(CommandButton); CommandBinding OpenCmdBinding = new CommandBinding(ApplicationCommands.Open,OpenCmdExecuted,OpenCmdCanExecute); this.CommandBindings.Add(OpenCmdBinding); KeyBinding OpenCmdKeyBinding = new KeyBinding(ApplicationCommands.Open,Key.R,ModifierKeys.Control); this.InputBindings.Add(OpenCmdKeyBinding); } void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e) { MessageBox.Show("The command has been invoked."); } void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } } }
Binding command to ApplicationCommands.Redo
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CommandEnabling" Height="300" Width="300" > <Grid> <Menu VerticalAlignment="Top"> <MenuItem Header="_Edit"> <MenuItem Command="Redo" /> </MenuItem> </Menu> </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(); CommandBinding redoCommandBinding = new CommandBinding(ApplicationCommands.Redo); redoCommandBinding.CanExecute += RedoCommandCanExecute; CommandBindings.Add(redoCommandBinding); } void RedoCommandCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = DateTime.Now.Second % 2 > 1; } } }
Binding Command to ApplicationCommands.New
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="CommandHandling" Height="300" Width="300" > <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Header="_File"> <MenuItem Command="New" /> </MenuItem> </Menu> <TextBox x:Name="inputBox" TextChanged="OnTextboxTextChanged" /> </DockPanel> </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 : Window { bool unsavedChanges = false; public Window1() { InitializeComponent(); CommandBinding cmdBindingNew = new CommandBinding(ApplicationCommands.New); cmdBindingNew.Executed += NewCommandHandler; CommandBindings.Add(cmdBindingNew); } void NewCommandHandler(object sender, ExecutedRoutedEventArgs e) { if (unsavedChanges) { MessageBoxResult result = MessageBox.Show(this,"Save changes to existing document?", "New",MessageBoxButton.YesNoCancel); if (result == MessageBoxResult.Cancel){ return; } if (result == MessageBoxResult.Yes){ SaveChanges(); } } inputBox.Clear(); } private void OnTextboxTextChanged(object sender, RoutedEventArgs e){ unsavedChanges = true; Console.WriteLine("changed"); } private void SaveChanges(){ unsavedChanges = false; Console.WriteLine("saved"); } } }