Introduction To Xaml

  • View
    1.642

  • Download
    1

Embed Size (px)

DESCRIPTION

Charles Petzold's WPF Chapter 19-20

Text of Introduction To Xaml

  • 1. Introduction to XAML
    Kim, Hyunyoung (young_kim@kaist.ac.kr)
    Digital Media Lab.
    2010-07-07

2. Keywords
WPF
XML
XAML
Window


3. 19 XAML
4. XAML [z:ml]
WPF (Window Presentation Foundation)

: , , 3D,
XML (Extensible Markup Language)
XAML (Extensible Application Markup Language)
XML
WPF

: , ,
:
C#
System.Windows.Controls

Hello, XAML!

Button btn = new Button();
btn.Foreground = Brushes.Yellow;
btn.FontSize = 32;//
btn.Content = Hello, XAML!
5. Stand Alone XAML

WPF XAML & XML
xmlns: XML

,URL
XamlStackPanel.xaml
PresentationHost.exe
Page
XAML StackPanel, Button Page

Hello, XAML!

http://schemas.microsoft.com/winfx/2006/xaml/presentation
6.
XamlPage.xaml
Page WindowTitle
StackPanel Page
Window XAML
PresentationHost.exe XAML
Window
Window FrameworkElement
7. XamlReader.Load
XamlReader.Load
System.Windows.Markup
XAML
XamlWriter.Save XAML
Stream XmlReader
string strXaml =
""Foreground='LightSeaGreen' FontSize='24pt'>" +
"Click me!" +
"";
StringReaderstrreader = new StringReader(strXaml);
XmlTextReaderxmlreader = new XmlTextReader(strreader);
object obj = XamlReader.Load(xmlreader);
8. XAML
LoadXamlResource.xml
Button Name
LoadXamlResource.cs
Stream XamlReader.Load (StackPanel) WindowContent
XAML FindName
XAML

Hello, XAML!

Uri uri = new Uri("pack://application:,,,/LoadXamlResource.xml");
Stream stream = Application.GetResourceStream(uri).Stream;
FrameworkElement el = XamlReader.Load(stream) as FrameworkElement;
Content = el;
Button btn = el.FindName("MyButton") as Button;
if (btn != null)
btn.Click += ButtonOnClick;
9. Window Xaml
LoadXamlWindow.xml
LoadXamlWindow.cs
PresentationHost.exe XAML
XAML Window , Window Window
Window AddHandler
MainApplication RunWindow
Title="Load Xaml Window"
SizeToContent="WidthAndHeight"
ResizeMode="CanMinimize">
Uri uri = new Uri("pack://application:,,,/LoadXamlWindow.xml");
Stream stream = Application.GetResourceStream(uri).Stream;
Window win = XamlReader.Load(stream) as Window;
win.AddHandler(Button.ClickEvent, new RoutedEventHandler(ButtonOnClick));
app.Run(win);
10. XAML C#
XmlTextReader
Window
XmlTextReaderxmlreader = new XmlTextReader(dlg.FileName);
object obj = XamlReader.Load(xmlreader);
if (obj is Window)
{
Window win = obj as Window;
win.Owner = this;
win.Show();
}
Else
frame.Content = obj;
11. XAML
XAML ,
XAML
XAML
WPF
XAML WPF (: Windows Workflow Foundation, WPF)
http://schemas.microsoft.com/winfx/2006/xaml: XAML
WPF
XAML x
WPF
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml/presentation
12. Class
Class
XAML
XAML
CompileXamlWindow.xaml
XAML
x:Class="MyNamespace.MyClasName"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Petzold.CompileXamlWindow.CompileXamlWindow">
namespace Petzold.CompileXamlWindow
{
public partial class CompileXamlWindow : Window
{

}
}
13. BAML ()
XAML
CompileXamlWindow.g.cs
XAML
Lstbox elips
InitializeComponent()
XAML
public CompileXamlWindow()
{
InitializeComponent();// XAML ,

}
void ButtonOnClick(object sender, RoutedEventArgsargs)
{
Button btn = sender as Button;//

}
14. XAML


http:

src ()
UseCustomClass.xaml
UseCustomClass.cs
ColorGridBox ListBox
SelectionChanged XAML (23)
xmlns:stuff=clr-namespace:MyNamespace

using Petzold.SelectColorFromGrid;

void ColorGridBoxOnSelectionChanged(object sender, SelectionChangedEventArgsargs)
{
ColorGridBoxclrbox = args.Source as ColorGridBox;

}
15. XAML
CenteredButton.xaml
UseCustomXamlClass.xaml
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Petzold.UseCustomXamlClass.CenteredButton"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="12" />
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Petzold.UseCustomXamlClass"
x:Class="Petzold.UseCustomXamlClass.UseCustomXamlClass"
Title = "Use Custom XAML Class">

Button A
Button B
Button C
Button D
Button E


16. Contd
UseCustomXamlClass.cs
public UseCustomXamlClass()
{
InitializeComponent();
for (inti = 0; i < 5; i++)
{
CenteredButtonbtn = new CenteredButton();
btn.Content = "Button No. " + (i + 1);
stack.Children.Add(btn);
}
}
17. Main
MyApplication.xaml
ApplicationDefinition
StartupUri Main Run , Window
MyApplication.cs
MyApplication.g.cs Main

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Petzold.IncludeApplicationDefinition.MyApplication"
StartupUri="MyWindow.xaml" />
namespace Petzold.IncludeApplicationDefinition
{
public partial class MyApplication : Application
{
}
}
18.
XAML
XAML
XamlOnlyApp.xaml

XamlOnlyWindow.xaml
BAML

StartupUri="XamlOnlyWindow.xaml" />
Title="Compile XAML Only"
SizeToContent="WidthAndHeight"
ResizeMode="CanMinimize">




19. XAML C#
CompileXamlOnly
EmbeddedCodeWindow.xaml

using System.Reflection






void ListBoxOnSelection(object sender, SelectionChangedEventArgsargs)
{
string strItem = lstbox.SelectedItem as string;
System.Reflection.PropertyInfo prop =
typeof(Brushes).GetProperty(strItem);
elips.Fill = (Brush)prop.GetValue(null, null);
}




20.

DesignXamlButtonWindow.xaml


Points="0 10,10 0,20 10,30 0,40 10,50 0,
60 10,70 0,80 10,90 0,100 10" />
Source="BOOK06.ICO"
Stretch="None" />

_Read Books!

Points="0 0,10 10,20 0,30 10,40 0,50 10,
60 0,70 10,80 0,90 10,100 0" />


21. 20
22. XAML Reader
XamlCruncher
XamlCruncherAssemblyInfo.cs

XamlCruncherSettings.cs
public Dock Orientation = Dock.Left;// TextBox Frame
public intTabSpaces = 4; //
public string StartupDocument = // XAML
""/2006/xaml/presentation"" +
"xmlns:x="http://schemas.microsoft.com/winfx" +
"/2006/xaml">" +
"Hello, XAML!" +
" ";
// .
public XamlCruncherSettings()
{
FontFamily = "Lucida Console";
FontSize = 10 / 0.75;
}
23. XamlCruncher.cs
XamlCruncher.cs
Window Grid
Xaml
// DockPanel TextBox
DockPaneldock = txtbox.Parentas DockPanel;
dock.Children.Remove(txtbox);
// Grid , 3
Grid grid=new Grid();
dock.Children.Add(grid);
// Xaml
MenuItemitemXaml = new MenuItem();
itemXaml.Header = "_Xaml";
menu.Items.Insert(menu.Items.Count - 1, itemXaml);
24. Contd
void Parse()
{
StringReaderstrreader = new StringReader(txtbox.Text);
XmlTextReaderxmlreader = new XmlTextReader(strreader);
try
{
object obj = XamlReader.Load(xmlreader); //
txtbox.Foreground = SystemColors.WindowTextBrush;
if (obj is Window)
{
// Window F7
win = objas Window;
statusParse.Content = "Press F7 to display Window";
}
else
{
// Frame Content
win = null;
frameParent.Content = obj;
statusParse.Content = "OK";
}
}
catch (Exception exc)
{
// TextBox
txtbox.Foreground = Brushes.Red;
statusParse.Content = exc.Message;
}
}
25.
XamlTabSpaceDialog.xaml

XamlTabSpaceDialog.cs
XamlCruncher.cs
Tab Space
public intTabSpaces
{
set { txtbox.Text = value.ToString(); }
get { return Int32.Parse(txtbox.Text);}
}
void TextBoxOnTextChanged(object sender,TextChangedEventArgsargs)
{
intresult;
btnOk.IsEnabled = (Int32.TryParse(txtbox.Text, out result) &&
result > 0 && result < 11);
}
void OkOnClick(object sender, RoutedEventArgsargs)
{
DialogResult = true;
}
26. TextBox Frame
XamlOrientationMenuItem.cs
27. Star.xaml
Points
PointCollection
X Y

Fill
Brushes
16 RGB
(128)
scRGB
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Points="144 48, 200 222, 53 114, 235 114, 88 222"
Fill="Red"
Stroke="Blue"
StrokeThickness="5" />
Fill=FF0000
Fill=#80FF0000
Fill=sc#0.5,1,0,0
28. LinearGradientBrush
2
(Polygon) & (Fill)
(, )


Red




Red


(Brush Fill )


Red
SolidColorBrush>

(Brush SolidColorBrush )