wpf chapter 15 dr. john abraham professor utpa. wpf –an introduction wpf provides a single...

30
WPF chapter 15 Dr. John Abraham Professor UTPA

Upload: arnold-wilkins

Post on 11-Jan-2016

230 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

WPF chapter 15

Dr. John Abraham

Professor

UTPA

Page 2: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

WPF –an introduction

• WPF provides a single platform capable of handling graphics, audio and video.

• WPF generates vector-based graphics that is resolution independent.

• WPF allows designers to define the appearance and content of a GUI by defining it in XAML.

• XAML (Extensible Application Markup Language) is implemented with XML

Page 3: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XAML - Extensible Application Markup Language

• is a declarative XML based language created by Microsoft

• used to initialize structured values and objects

Page 4: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML

• Developed in 1996 by World Wide Web consortium

• Standard for describing data

• Used to exchange data between applications

Page 5: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

What is XML?

• XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to describe data XML tags are not predefined. You must define your own tags XML uses a Document Type Definition (DTD) or an XML Schema to describe the data XML with a DTD or XML Schema is designed to be self-descriptive XML is a W3C Recommendation - 1996

Page 6: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML

• Portable, Widely supported, Open technology

• Standard for storing data (as text file) that are exchanged between applications.

• Not for displaying data

• No predefined tags – user supplies tags.

• With XML, financial information can be exchanged over the Internet

Page 7: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML vs. HTML

• XML was designed to describe data and to focus on what data is.HTML was designed to display data and to focus on how data looks.

• HTML is about displaying information, while XML is about describing information

• XML can separate data from HTML

Page 8: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

ARTICLE.XML file• <?xml version = "1.0"?>• <!-- Article structured with XML -->• <article>• <title>Simple XML</title>• <date>December 6, 2001</date>• <author>• <firstName>John</firstName>• <lastName>Doe</lastName>• </author>• <summary>XML is pretty easy.</summary>• <content>In this chapter, we present a wide variety of examples

that use XML.• </content>• </article>

Page 9: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

EXPLANATION

• Root element is article

• Child elements are title, data, author & summary

• To process this file, an XML parser is required, such as msxml (microsoft), Xerces (appache), etc.

Page 10: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML rules

• All XML elements must have a closing tag– With XML, it is illegal to omit the closing tag.

• XML tags are case sensitive• All XML elements must be properly nested.

Following is wrong:– <b><i>This text is bold and italic</b></i>

• All XML documents must have a single root element

Page 11: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML attributes

• XML elements can have attributes in the start tag, just like HTML.

• Attribute values must always be quoted

• <note date="12/11/2002">

• Avoid using attributes if you can. Use child elements instead.

Page 12: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

DTD• A Document Type Definition defines the legal building blocks

of an XML document. It defines the document structure with a list of legal elements.

• A "Valid" XML document also conforms to a DTD.• A "Valid" XML document is a "Well Formed" XML document,

which also conforms to the rules of a Document Type Definition (DTD)

• A DTD can be declared inline in your XML document, or as an external reference

• Your application can use a standard DTD to verify that the data you receive from the outside world is valid.

• You can also use a DTD to verify your own data.

Page 13: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Internal DTD• <?xml version="1.0"?>• <!DOCTYPE note [• <!ELEMENT note (to,from,heading,body)>• <!ELEMENT to (#PCDATA)>• <!ELEMENT from (#PCDATA)>• <!ELEMENT heading (#PCDATA)>• <!ELEMENT body (#PCDATA)>• ]>• <note>• <to>Tove</to>• <from>Jani</from>• <heading>Reminder</heading>• <body>Don't forget me this weekend</body>• </note>

Page 14: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

This DTD is interpreted like this:

• !DOCTYPE note (in line 2) defines that this is a document of the type note.

• !ELEMENT note (in line 3) defines the note element as having four elements: "to,from,heading,body".

• !ELEMENT to (in line 4) defines the to element to be of the type "#PCDATA".

• !ELEMENT from (in line 5) defines the from element to be of the type "#PCDATA"

• and so on.....

Page 15: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

External DOCTYPE declaration

• If the DTD is external to your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:

• <!DOCTYPE root-element SYSTEM "filename"> • <?xml version="1.0"?>• <!DOCTYPE note SYSTEM "note.dtd">• <note>• <to>Tove</to>• <from>Jani</from>• <heading>Reminder</heading>• <body>Don't forget me this weekend!</body>• </note>

• And this is a copy of the file "note.dtd" containing the DTD:

• <!ELEMENT note (to,from,heading,body)>• <!ELEMENT to (#PCDATA)>• <!ELEMENT from (#PCDATA)>• <!ELEMENT heading (#PCDATA)>• <!ELEMENT body (#PCDATA)>

Page 16: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML Schema

XML Schema is an XML based alternative to DTD. • An XML schema describes the structure of an XML

document.• The XML Schema language is also referred to as

XML Schema Definition (XSD).• An XML Schema:

– defines elements that can appear in a document – defines attributes that can appear in a document – defines which elements are child elements – defines the order of child elements – defines the number of child elements – defines whether an element is empty or can include text – defines data types for elements and attributes – defines default and fixed values for elements and attributes

Page 17: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

A Simple XML Schema • This is a simple XML Schema file called "note.xsd" that defines the

elements of the XML document above ("note.xml"): • <?xml version="1.0"?>• <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"• targetNamespace="http://www.w3schools.com"• xmlns="http://www.w3schools.com"• elementFormDefault="qualified"><xs:element name="note">• <xs:complexType>• <xs:sequence>• <xs:element name="to" type="xs:string"/>• <xs:element name="from" type="xs:string"/>• <xs:element name="heading" type="xs:string"/>• <xs:element name="body" type="xs:string"/>• </xs:sequence>• </xs:complexType>• </xs:element></xs:schema>

Page 18: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Viewing XML files

• Raw XML files can be viewed in Mozilla, Firefox, Opera, Internet Explorer, and in Netscape 6+.

• However, to make XML documents to display like nice web pages, you will have to add some display information

• With CSS (Cascading Style Sheets) you can add display information to an XML document.

• Look at this XML file: Look at this XML file: note.xml

Page 19: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Displaying XML files with CSS

• Take a look at this XML file: The CD catalog• Then look at this style sheet: The CSS file• Finally, view:

The CD catalog formatted with the CSS file– XSL (the eXtensible Stylesheet Language) is the

preferred style sheet language of XML.– View the XML file, the XSL style sheet, and

View the result.

Page 20: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XSLT

XSLT (Extensible Stylesheet Language Tranformation) is a language for transforming XML documents into XHTML documents or to other XML documents.

• XSLT is the most important part of XSL • XSLT transforms an XML document into

another XML document • XSLT uses XPath to navigate in XML

documents

Page 21: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

XML namespaces

• XML namespace is a collection of element and attribute names.

• It prevents collisions because of same names: example:– <school:subject>math</school:subject>– <medical:subject>math</medical:subject>

Page 22: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Declarative GUI programming using XAML

<Window x:Class="XAMLIntroduction" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="A Simple Window" Height="150" Width="250">

<!-- a layout container --> <Grid Background="Gold">

<!-- a Label control --> <Label HorizontalAlignment="Center" VerticalAlignment="Center"> Welcome to WPF! </Label> </Grid></Window>

Page 23: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF
Page 24: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Starting a simple wpf application

• Create a wpf application• Go to tool box and right click• Select Add items• Select wpf components• Select ink canvas• Now you should be able to paint• However to change colors and such we

need to program. Explained below.

Page 25: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Starting a WPF app

New project

Choose WPF application

You will see the window on top and xaml at the bottom

Page 26: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Layout Controls

Grid - Grid of rows and columns. Elements are placed into cells.

Canvas – element positions are defined by their distance from top and left edges of the canvas.

stackPanel – elements are arranged in single row or column depending on their orientation.

Page 27: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Layout in Action

Demo the program

Note that margin, horizontal alignment and vertical alignment are used over and over again.

redRadioButton is the name for the radiobutton that controls the red.

Group box names are used for color and size.

Page 28: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

StackPanel

The stackPanel is the simplest of the layout containers.

It arranges its content in either vertically or horizontally depending on the orientation property setting.

Page 29: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Grid Control and canvas control

Window is made up of a grid of rows and columns.

Canvas is the painting area. A canvas allows users to position controls by defining explicit coordinates using canvas.left and canvas.top.

Page 30: WPF chapter 15 Dr. John Abraham Professor UTPA. WPF –an introduction WPF provides a single platform capable of handling graphics, audio and video. WPF

Layout in Design mode