<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("MyResources.xaml"); ResourceDictionary rd = (ResourceDictionary)Application.LoadComponent(resourcePath); } } }
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Use Application.Current.Dispatcher.Invoke to throw an exception
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication1.MainWindow" Height="200" Width="400"> <Button Click="aClick">click</Button> </Window> //File:Window.xaml.cs using System; using System.Threading; using System.Windows; using System.Windows.Threading; namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.Title = string.Format("Running on Main UI Thread {0}", Thread.CurrentThread.ManagedThreadId); } void aClick(object sender, RoutedEventArgs e) { Application.Current.Dispatcher.Invoke(DispatcherPriority.Send,(DispatcherOperationCallback)delegate(object arg) { string msg = string.Format("Exception forwarded from secondary UI thread {0}.", 1); throw new Exception(msg, null); } , null); } } }
An animated Ellipse traces the outline of rendered text by using the path geometry of the text.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication1.Window1" Title="Using a Path Geometry to Highlight Text" Background="PowderBlue"> <StackPanel> <Button Margin="10" Grid.Column="2" Grid.Row="0" FontSize="16" Click="OnDisplayTextClick">Display Text</Button> <Canvas Margin="20" Height="150"> <Path Canvas.Top="15" Canvas.Left="15" Stroke="SteelBlue" StrokeThickness="3" Fill="LightSteelBlue" Name="path" /> <Ellipse Canvas.Top="0" Canvas.Left="0" Width="30" Height="30"> <Ellipse.Fill> <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5"> <RadialGradientBrush.GradientStops> <GradientStop Color="Yellow" Offset="0.25" /> <GradientStop Color="Transparent" Offset="1" /> </RadialGradientBrush.GradientStops> </RadialGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <MatrixTransform /> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="FrameworkElement.Loaded"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard x:Name="storyboard"> <MatrixAnimationUsingPath x:Name="matrixAnimation" Duration="0:00:40" RepeatBehavior="Forever" Storyboard.TargetProperty="RenderTransform.Matrix" /> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Ellipse.Triggers> </Ellipse> </Canvas> </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Globalization; using System.Windows; using System.Windows.Media; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } public void OnDisplayTextClick(object sender, EventArgs e) { FormattedText formattedText = new FormattedText( "asdf", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Verdana"), 96, Brushes.Black); formattedText.SetFontWeight(FontWeights.Bold); Geometry geometry = formattedText.BuildGeometry(new Point(0, 0)); PathGeometry pathGeometry = geometry.GetFlattenedPathGeometry(); path.Data = pathGeometry; matrixAnimation.PathGeometry = pathGeometry; } } }
Receive Notification When an Animation Completes
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="" Height="300" Width="300" Background="Black"> <Window.Triggers> <EventTrigger RoutedEvent="Window.Loaded"> <BeginStoryboard> <Storyboard Completed="Storyboard_Completed"> <ParallelTimeline Completed="ParallelTimeline_Completed"> <ColorAnimation Duration="0:0:1" Completed="Animation1_Completed" Storyboard.TargetProperty="Background.Color" To="White" /> <ColorAnimation Duration="0:0:2" Completed="Animation2_Completed" Storyboard.TargetName="bd" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="Black" /> </ParallelTimeline> <ColorAnimation Duration="0:0:3" Completed="Animation3_Completed" Storyboard.TargetName="rect" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" To="Firebrick" /> </Storyboard> </BeginStoryboard> </EventTrigger> </Window.Triggers> <Border x:Name="bd" Margin="20" Background="HotPink"> <Rectangle x:Name="rect" Width="100" Height="100" Fill="WhiteSmoke" /> </Border> </Window> //File:Window.xaml.cs using System; using System.Windows; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Storyboard_Completed(object sender, EventArgs e) { MessageBox.Show("Storyboard complete.", "WpfApplication1"); } private void ParallelTimeline_Completed(object sender, EventArgs e) { MessageBox.Show("ParallelTimeline complete.", "WpfApplication1"); } private void Animation1_Completed(object sender, EventArgs e) { MessageBox.Show("Animation 1 complete.", "WpfApplication1"); } private void Animation2_Completed(object sender, EventArgs e) { MessageBox.Show("Animation 2 complete.", "WpfApplication1"); } private void Animation3_Completed(object sender, EventArgs e) { MessageBox.Show("Animation 3 complete.", "WpfApplication1"); } } }
Show a Continuous Animation During an Asynchronous Process
//File:Window.xaml.cs
using System.Windows;
using System.Threading;
using System.ComponentModel;
using System.Windows.Media.Animation;
namespace WpfApplication1
{
public partial class Window1 : Window
{
private Storyboard pulseStoryboard;
private BackgroundWorker worker;
public Window1()
{
InitializeComponent();
pulseStoryboard = (Storyboard) this.Resources[“PulseStoryboard”];
pulseStoryboard.RepeatBehavior = RepeatBehavior.Forever;
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
private void button_Click(object sender, RoutedEventArgs e)
{
pulseStoryboard.Begin(this, true);
worker.RunWorkerAsync();
button.IsEnabled = false;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button.IsEnabled = true;
pulseStoryboard.Stop(this);
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
for(int i = 1; i <= 50; i++)
{
Thread.Sleep(100);
}
}
}
}
[/csharp]
Rolling Ball Animation
<Window x:Class="WpfApplication1.RollingBall" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Rolling Balls" Height="350" Width="518"> <Border BorderBrush="Gray" BorderThickness="1" Margin="4"> <Canvas> <Rectangle Fill="Gray" Width="500" Height="5" Canvas.Top="60" /> <Ellipse x:Name="ellipse1" Width="50" Height="50" Stroke="Blue" Canvas.Top="10" Canvas.Left="0"> <Ellipse.Fill> <LinearGradientBrush> <GradientStop Color="Blue" Offset="0.2" /> <GradientStop Color="LightBlue" Offset="0.8" /> </LinearGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <RotateTransform x:Name="ellipse1Rotate" CenterX="25" CenterY="25" /> </Ellipse.RenderTransform> </Ellipse> <Rectangle Fill="Gray" Width="500" Height="5" Canvas.Top="130" /> <Ellipse x:Name="ellipse2" Width="50" Height="50" Stroke="Red" Canvas.Top="80" Canvas.Left="0"> <Ellipse.Fill> <LinearGradientBrush> <GradientStop Color="Red" Offset="0.8" /> <GradientStop Color="LightSalmon" Offset="0.2" /> </LinearGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <RotateTransform x:Name="ellipse2Rotate" CenterX="25" CenterY="25" /> </Ellipse.RenderTransform> </Ellipse> <Rectangle Fill="Gray" Width="500" Height="5" Canvas.Top="200" /> <Ellipse x:Name="ellipse3" Width="50" Height="50" Stroke="Green" Canvas.Top="150" Canvas.Left="0"> <Ellipse.Fill> <LinearGradientBrush> <GradientStop Color="Green" Offset="0.5" /> <GradientStop Color="LightGreen" Offset="0.5" /> </LinearGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <RotateTransform x:Name="ellipse3Rotate" CenterX="25" CenterY="25" /> </Ellipse.RenderTransform> </Ellipse> <Rectangle Fill="Gray" Width="500" Height="5" Canvas.Top="270" /> <Ellipse x:Name="ellipse4" Width="50" Height="50" Stroke="Purple" Canvas.Top="220" Canvas.Left="0"> <Ellipse.Fill> <LinearGradientBrush> <GradientStop Color="Purple" Offset="0.5" /> <GradientStop Color="LightPink" Offset="0.2" /> </LinearGradientBrush> </Ellipse.Fill> <Ellipse.RenderTransform> <RotateTransform x:Name="ellipse4Rotate" CenterX="25" CenterY="25" /> </Ellipse.RenderTransform> </Ellipse> </Canvas> </Border> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace WpfApplication1 { public partial class RollingBall : Window { public RollingBall() { InitializeComponent(); double nRotation = 2; DoubleAnimation da = new DoubleAnimation(0, 450,TimeSpan.FromSeconds(5)); da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; ellipse1.BeginAnimation(Canvas.LeftProperty, da); da = new DoubleAnimation(0, nRotation,TimeSpan.FromSeconds(15)); da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; ellipse1Rotate.BeginAnimation(RotateTransform.AngleProperty, da); da = new DoubleAnimation(0, 450,TimeSpan.FromSeconds(15)); da.AccelerationRatio = 0.4; da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; ellipse2.BeginAnimation(Canvas.LeftProperty, da); da = new DoubleAnimation(0, nRotation,TimeSpan.FromSeconds(15)); da.AccelerationRatio = 0.4; da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; ellipse2Rotate.BeginAnimation(RotateTransform.AngleProperty, da); da = new DoubleAnimation(0, 450,TimeSpan.FromSeconds(5)); da.DecelerationRatio = 0.6; da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; ellipse3.BeginAnimation(Canvas.LeftProperty, da); da = new DoubleAnimation(0, nRotation,TimeSpan.FromSeconds(15)); da.DecelerationRatio = 0.6; da.RepeatBehavior = RepeatBehavior.Forever; da.AutoReverse = true; ellipse3Rotate.BeginAnimation(RotateTransform.AngleProperty, da); } } }
Path animation by code, duration, RepeatBehavior
<Window x:Class="WpfApplication1.PathAnimationExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Path Animation" Height="500" Width="518"> <Canvas Margin="5"> <Path Stroke="LightBlue"> <Path.Data> <PathGeometry x:Name="path1" Figures="M10,10 C75,20 300,20 200,120 220,220 300,220 350, 400 325,20 220,20 200,120 175,220 75,200 50,120" /> </Path.Data> </Path> <Path Stroke="DarkGoldenrod"> <Path.Fill> <RadialGradientBrush> <GradientStop Color="Gold" Offset="0" /> <GradientStop Color="DarkGoldenrod" Offset="1" /> </RadialGradientBrush> </Path.Fill> <Path.Data> <EllipseGeometry x:Name="circle1" Center="50,120" RadiusX="10" RadiusY="10" /> </Path.Data> </Path> </Canvas> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace WpfApplication1 { public partial class PathAnimationExample : Window { public PathAnimationExample() { InitializeComponent(); path1.Freeze(); // For performance benefits. PointAnimationUsingPath pa = new PointAnimationUsingPath(); pa.PathGeometry = path1; pa.Duration = TimeSpan.FromSeconds(5); pa.RepeatBehavior = RepeatBehavior.Forever; circle1.BeginAnimation(EllipseGeometry.CenterProperty, pa); } } }