Вы находитесь на странице: 1из 461

-

-
2010

681.3.06
32.973.26-018.2
75
75

. .

Silverlight 3. .: -, 2010.
464 .: .
ISBN 978-5-9775-0514-7

Web-
Microsoft Silverlight 3. ,
, . Web-,
Silverlight-.
Microsoft Visual Web Developer 2008 Express Edition, XAML C#, , , Silverlight-.
Silverlight . , Web-, . , Silverlight.
Silverlight-.
Web-
681.3.06
32.973.26-018.2

:

.
.

02429 24.07.00. 30.12.09.


701001/16. . . . . 37,41.
2000 .
"-", 190005, -, ., 29.
-
77.99.60.953..005770.05.09 26.05.2009 .
.

" ""
199034, -, 9 , 12

ISBN 978-5-9775-0514-7

. ., 2010
, "-", 2010

.................................................................................................................. 1
- ! .............................................................................. 1
Silverlight ......................................................................................................... 2
......................................................................................................... 2
........................................................................................................... 3
.................................................................................................. 4
...................................................................................................................... 5

I. SILVERLIGHT.
..................................................................... 7
1. Silverlight .............................................................................. 9
WWW ........................................................................................................ 9
: Web- ...................................................................... 9
: Web- .............................................................. 13
: Web- ............................................................. 14
Web- ....................... 16
HTML+CSS+JavaScript .............................................................................................. 16
Adobe Flash ................................................................................................................. 18
Sun Java ....................................................................................................................... 19
Microsoft Silverlight ............................................................................................................ 20
? ........................................................................................................................ 21

2. Silverlight.................................... 22
...................................................................................... 22
Silverlight- ................................................................................... 23
.................................................................................................................... 23
Silverlight- Web- ...................... 24
................................................................................................................ 25
................................................................................................................. 26

IV

Silverlight- .......................................................................................... 28
Silverlight-. ....................................................... 28
. .................................................................... 29
. ................................................. 31
, Silverlight- .............................................. 32
Silverlight-................................... 33
? ........................................................................................................................ 34

3. Silverlight- ............................................... 35
Microsoft Visual Web Developer 2008 Express Edition ..................................................... 35
. ................................................................................................ 39
Silverlight- ...................................................................................... 40
................................................................................................................ 42
Solution Explorer .................................................................................................... 43
Silverlight- ................................................................. 44
XAML ............................................................................. 44
. Toolbox ......................................... 48
Silverlight- .......................................................... 52
""............................................................................... 53
Silverlight- ......................................................................... 56
................................................................................................... 57
.................................................. 58
C# ................................................................... 59
C#, .......................................... 62
............................................................................................................. 65
Visual Web Developer 2008 .......................................................... 66
? ........................................................................................................................ 67

II. , , ,
........................................................................ 69
4. ............................................................ 71
, .................................................................................. 71
................................................................................................................................. 73
......................................................................................................................... 74
............................................................................................................. 75
...................................................................................... 75
............................................................. 77
............................................................................... 78
XAML-. ......................................................... 79
? ........................................................................................................................ 81

5. ................................................................... 82
........................................................................................................................ 82
"".................................................................................................. 82

"" .................................................................................................... 89
""...................................................................................................... 89
............................................................................................................................. 91
? ........................................................................................................................ 92

6. ...................................................................... 93
TextBlock .............................................................................................................. 93
TextBlock ..... 97
TextBox ............................................................................................................ 99
PasswordBox ..................................................................................... 102
Button ................................................................................................................... 103
CheckBox ............................................................................................................. 105
RadioButton ............................................................................................ 106
ListBox .................................................................................................................. 107
ComboBox .............................................................................. 109
Calendar.......................................................................................................... 110
DatePicker ............................................................................. 111
Slider ................................................................................................................ 111
ProgressBar .................................................................................. 112
ScrollViewer .................................................................................. 113
TabControl .................................................................................... 114
............................................................................. 116
? ...................................................................................................................... 119

7. ............................... 120
................................................................................................................. 120
Image...................................................................................................... 121
...................................................................... 122
.................................................................................... 124
MediaElement ........................................................................................ 124
.................................................. 126
? ...................................................................................................................... 127

8. .................................................................................. 128
................................................................................................ 128
.............................................................................................. 129
............................................................. 130
............................................................................. 132
.......................................................... 132
? ...................................................................................................................... 134

III. C# ....................................................................................... 135


9. C# ................................................. 137
, , , ............................ 137

VI

.................................................................................................................... 139
C#, Silverlight .................................................. 140
................................................................................................................. 140
........................................................................................................ 142
..................................................................................... 142
............................................................................................................... 143
.............................................................................................................. 143
......................................................................................................... 144
............................................................................................. 144
...................................................................... 144
.......................................................................... 145
...................................................................................................................... 146
......................................................................................... 146
. ........................................................ 147
........................................................................................ 148
, .......................................... 148
........................................................................................................................ 148
.................................................................................... 149
........................................................................................... 150
........................................................................................ 150
.............................................................................................. 151
............................................................................................. 152
.................................................................................................. 153
............................................................................................. 153
........................................................................................................ 155
......................................................................................................................... 155
............................................................................................... 155
................................................................................................... 157
........................................................................................................................ 158
.......................................................................................... 158
...................................................................................... 160
...................................................................................... 161
.................................................................... 161
....................................................................................................... 162
............................................................................................................................ 163
................................................................................................................ 165
.................................................................................................................... 166
? ...................................................................................................................... 167

10. C# ............................................................ 168


............................................................................................................. 168
...................................................................................................... 169
.................................................................................................................. 169
............................................................................................................. 169
........................................................................................................... 169

VII

........................................................................................................... 170
................................................................................ 171
............................................................................................. 171
........................................................................ 171
............................................................................................................ 172
............................................................................... 172
.......................................................................................... 172
............................................................................................. 173
............. 174
................. 176
null.................................................................................................... 177
................................................................................... 177
Silverlight................................................................ 177
Object ..................................................................................................... 178
String ...................................................................................................... 178
Math ....................................................................................................... 179
............................................................................... 180
................................................................................. 181
................................................................................................ 182
............................................................................................ 183
................................................................................. 186
............................................................................................ 187
................................................................... 189
......................................................................................................................... 190
............................................................................................. 190
Silverlight .......................................................... 191
Int16, Int32, Int64, UInt16, UInt32 UInt64 ................................................... 191
Double Single................................................................................................. 191
Decimal ............................................................................................................. 192
DateTime ........................................................................................................... 193
TimeSpan .......................................................................................................... 195
............................................................................. 196
...................................................................................................................... 196
................................................................................................................... 199
? ...................................................................................................................... 199

11. ........................................................................................ 200


.......................................................................................................... 200
........................................................................................................... 201
List .................................................................................................................. 201
List ............................................................................ 201
........................................................................... 202
....................................................... 202
.............................................................................. 203

VIII

...................................................................... 204
.............................................................. 205
Dictionary ........................................................................................................... 207
Dictionary ..................................................................... 207
............................................................................... 207
........................................................... 207
................................................................................... 208
........................................................................... 209
.................................................................................... 209
Queue .......................................................................................................... 210
Stack .................................................................................................................. 210
, ............................................... 211
? ...................................................................................................................... 212

12. ..................................................................................... 213


........................................................................................................ 213
.................................................................................................... 214
.................................................................................... 215
.................................................................................................... 216
......................................................................... 216
..................................................................... 218
............................................................................................ 219
? ...................................................................................................................... 220

IV. . LINQ ............ 221


13. ............................................... 223
............................................................................................................. 223
.......................................................................................... 224
Silverlight- .
............................................................................ 226
........................................................................................ 228
.................................................. 230
.................................................................................... 232
....................................................................... 233
.................................................................................... 234
...................................................................................................... 236
.......................................................... 236
.............................................................................. 237
. ........................... 239
............................................................................. 240
DataGrid ................. 241
DataGrid .................................................... 246
DataGrid ............................................ 247
? ...................................................................................................................... 248

IX

14. LINQ................................................................................................... 249


LINQ ................................................................................... 249
............................................................................................... 250
. ........................................................ 253
......................................................................................................... 254
.......................................................................................................... 255
.......................................................................................................... 256
........................................................................................................ 258
....................................................................................... 261
.
.................................................................................... 262

...................................................................................................... 264
......................................................................................... 265
? ...................................................................................................................... 267

V. SILVERLIGHT.
.................................................. 269
15. ............................................................................................. 271
......................................................... 271
....................................................................................................... 274
. .................................................................................... 276
....................................................... 276
. ......................................... 277
....................................................................................... 279
Border ............................................................................................................ 284
................................................................................................................ 285
....................................................................................................... 285
................................................................................................... 286
................................................................................................... 291
................................................................................................................. 293
.......................................................... 294
? ...................................................................................................................... 294

16. .......................................................... 295


........................................................................................................................... 295
................................................................................................. 295
......................................................................................... 296
............................................................... 298
................................................................................................................ 299
.................................................................................... 299
. ........... 303
................................................................................... 304
? ...................................................................................................................... 305

17. .......................................................................................... 306


Silverlight- ........................................................................ 306
....................................................................................... 308
...................................................................................................... 313
......................................................................................................... 317
............................................................................. 319
? ...................................................................................................................... 320

18. ................................................... 321


.................................................... 321
.................................................................. 322
...................................................................................................... 323
............................................................................................... 324
................................................................................................................. 326
............................................................... 328
- (HyperlinkButton).................................................................... 330
Web- .............................................................................. 331
? ...................................................................................................................... 331

19. ............................................................................... 332


.............................................................................................................. 332
................................................................................... 332
..................................................................................... 334
................................................................ 336
...................................................... 338
- ..................................................................................................... 340
? ...................................................................................................................... 342

VI. WEB- ..................... 343


20. .................................................... 345
............................................................................................. 346
................................................................... 346
......................................................................................................... 347
.................................................................................. 347
............................................................................................................ 349
........................................................................................................ 350
......................................................................................... 352
.............................................................. 353
........................................................................................ 354
.................................................... 354
................................................................... 356
.................................................................... 356
............................ 356

XI

...................................................................................... 358
................................................................. 359
................................................................... 361
? ...................................................................................................................... 363

21. .................................................... 364


........................................................................ 364
........................................................................... 365
WebClient ........................................................................................................ 365
............................................................................................. 366
........................................................... 367
................................................................ 369
.................................................................................... 370
................................................................................................... 370
.............................................. 371
? ...................................................................................................................... 374

22. Web- ................................................................. 375


Web- ..................................................................................................................... 375
..................................................................................................................... 376
..................................................................................................... 378
.................................................................................. 378
....................................................................................................... 380
......................................................................................................... 383
................................................................................... 386
Web-..................................................................................................... 387
Web- .............................................................................. 387
......................................................................................... 388
Web-.................................................................................. 390
................................................................................ 392
Silverlight-, Web- .. 392
Silverlight- Web- ............................................ 394
Web- ............................................................................ 395
Silverlight-, Web- .... 398
LINQ- Web- ................................................................ 399
............................................................. 401
, ............................................. 403
...................................................... 406
? ...................................................................................................................... 409

VII. .......................................................... 411


23. ............................................................................ 413
..................................................... 413
................................................................... 414

XII

.............................................................................. 415
..................................................................................... 418
? ...................................................................................................................... 420

24. Silverlight- ................................... 421


Silverlight-. .................. 421
......................................................... 422
, ................................................................................ 423
................................................................................................... 426
Silverlight- Web- .......................................................... 429
Silverlight- ............................................................................. 430
Silverlight-..................................................... 430
Silverlight- ...................... 432

......................................................................................................... 435
.................................................................................... 437


-! Microsoft Silverlight 3!
Web-! !
? ? Silverlight? , ?
Web-?

- !
,
. , , , , , ,
. ...
, , .
, . , Web-
, -...
! Web-, , Web-, Web-. Web-
Web-,
Web-, -
, , . , ( ...).
Web-
. ,
Adobe Flash. , Sun Java (
Oracle). , -

- , "" - HTML+CSS+JavaScript. ,
, Mozilla XUL, Microsoft ActiveX .
.
Microsoft Silverlight.
"" , . , ,
, .
. , , , , , .

Silverlight
Silverlight " "
1. .
 Web- , -

.
 .
 , (

), .
 , , .
 .
 Microsoft, -

SharePoint 2010.
 -

.
, , .
( Flash).


Silverlight Silverlight-. ,
, , , -

(Web-)
.
! .
Silverlight. , .


, . , Windows
.
1. Web- Silverlight.
Silverlight. Web- http://
go.microsoft.com/fwlink/?LinkId=128526.
2. Microsoft Visual Web Developer 2008 Express Edition. Web-, Silverlight-.

Visual Web Developer 2008 SP1. Silverlight 3 Tools


.

Web- http://www.microsoft.com/express/download/default.aspx
Visual Web Developer 2008,
Microsoft Visual Studio 2008 Express Edition, , , Visual Web Developer 2008. SP1. ;
Microsoft Visual Studio 2008 Express Edition .
Visual Web Developer 2008 , Microsoft SQL Server 2008 Express Edition Silverlight 2
Tools. Microsoft SQL Server 2008 Express Edition , . .
22 , .
Silverlight 2 Tools
Silverlight 3 Tools.
3. Silverlight 3 Tools. Visual Web Developer 2008, Silverlight 3.
Web- http://go.microsoft.com/fwlink/?LinkID=128219.

Silverlight 3 Tools
Silverlight. .

4. Silverlight 3.
Silverlight 3 . CHM Web- http://go.microsoft.com/fwlink/?LinkId=127106.
! .


, Silverlight-.
, .


. .

, (<>)
, . , , ,
<>. :
if (<>)


.
([]) . :
[< , >] < > < >

, .
[], , .
,
. :
string[] sPlatforms = {"HTML+CSS+JavaScript", "Flash", "Java",
"Silverlight"};

, .
 .

,
.
 , -

(. .),
, .
 .
 , .
 .
 .
 ,

.
 , -

"-", ,
.
 "-" .


.
 , Visual Web

Developer 2008 Silverlight 3, .


 , , .

Silverlight.

1.

Silverlight

2.

Silverlight

3.

Silverlight-

Silverlight

, Silverlight? , ,
Web-...
Web-? ?
... .

WWW
, . ? ? ,
, "" "", - ""?
(, , )
WWW. . ,
, (
-...).

: Web-
World Wide Web . Web-, HTML (Hypertext Markup Language ) ,
Web-. Web-, ( , -

10

I. Silverlight.

), Web-
( ). .
HTML , HTML.
;
, , ,
. ,
Web- .
Web- , ,
, Microsoft Windows.
Web- Web HTML, . ,
, Web-
txt, htm html;
Web- ,
.
Web- Web-,
, HTML , .
, . , , , , ,
HTML.
Web- Web-? --,
, .
, - Web-,
Web- -. - ,
Web-, :
http://www.compression.ru/all_anns.htm
, ? - Web- " ",
. http://www.compression.ru -
, , all_anns.htm
(, , ).
(/), , -.

1. Silverlight

11

- , , :
www.compression.ru/all_anns.htm
, , .
-, Web- ( http://www.compression.ru) . , Web- (all_anns.htm).
-:
http://www.compression.ru/video/index_ru.htm
Web- " ",
. Web-
Web- video/index_ru.htm.
- , , Web-. :
http://www.compression.ru/
, Web-
Web-.
, , , .
?
, Web. , , Web-,
, .
, Web-, Web-,
, Web. . Web-
, Web-.
Web- , Web-
. , - all_anns.htm , Web .
, Web-
, . ,
video/index_ru.htm (. ), Web-
index_ru.htm, video, , ,
.

12

I. Silverlight.

, ? Web Web- .
default.htm[l] index.htm[l] (
Web-) .
, , .
HTML
. ,
.
.
, Web- , -
. Web-, , Web-
, Web.
(Cascade Style
Sheet, CSS). HTML Web-, .
Web-.
Web-, HTML, . - , Adobe PDF, Microsoft
Word, Excel PowerPoint, Adobe Flash .
Web- , ; , Web- Web- Web.
,
, .

Web-. Web- , HTML


Web- - ( , , .). JavaScript .
Web- YouTube (http://www.youtube.com/).
HTML, -

1. Silverlight

13

,
CSS, ,
"" Web-.

: Web-
,
. Web , , . , ...
. , ,
. , ,
"" , ,
, , . !..
- ,
? , Web-, Web-
Web- ,
? !
, Web- -
( Web-) Web- ,
. ,
Web- ,
. !
- .
, Web-
, , .
, Web- , .
, ! Web- ( ) !
Web-
Web-, Web-, , , Web-. .

14

I. Silverlight.

, , Web- (
). ,
, "". (
.)
HTML .
Web- Web-,
. : ,
, , , ., .
,
" " . Web-, -, . , , Web-
( . 1.1).

. 1.1. Web- Web- Web- Mail.ru

: Web-
, - . ,
, ( ).
Web-
( , )
, , . , , , ,
.
. , , Web . Web-

1. Silverlight

15

! , ! Web-
, .
.
Web- ,
Web- ,
.
Web- , Web- ,
Web-,
. ,
, ,
, - ,
, .
!

. 1.2. Web- RSS

16

I. Silverlight.

Web- - ... , CSS, , ...


( ) Web- ,
. , "15", , ...
, Web-, Web-. . . 1.2.
,
. .


Web-
, , Web :
 ;
 ;
 ( ,

);
 ,

;
 ,

( ). , .
Web- .
.

HTML+CSS+JavaScript
Web- -: HTML, CSS Web-, JavaScript.
, Web-.

1. Silverlight

17

Web- Web- Web-.



. JavaScript ,
.
Web- Windows-,
: , , , , , ,
, , .
, , ,
Ext (http://www.extjs.com/). , . 1.2 , .
:
 HTML, CSS
JavaScript, Web-;
 HTML, CSS JavaScript
Web-;
 . ,
!
:
 -,
HTML, CSS JavaScript; ;
 ;
 -.
. , ,
, Web- HTML-
Web-. .
Web- , Web-, ,
. -.
. JavaScript . Web- Web-,
, , , . . .
, ""
HTML+CSS+JavaScript , - -

18

I. Silverlight.

. , , . , , ,
.
.

Adobe Flash
Flash FutureSplash
90- . 1996
Macromedia, , , 2006 Adobe.
Flash Web- ,
Web. Flash CS4 . ActionScript.
Flash Web-
Flash.
Flash ,
, Flash .
:
 ,
, .;
 ActionScript;
 ;
 -.
-, Flash -!
JavaScript, ActionScript . Flash
() , . , , ,
, , .

.
.

1. Silverlight

19

Flash:

Web- , . , , ,
Flash;
 ActionScript;
 Flash ;
 Flash Flash (, ,
);
 Flash , , Adobe Flash Professional CS4.
Adobe Flash , Web-. , ,
!..

Sun Java
Java Sun 1995 .
; "" , , Web-. ,
, Sun...
Java. ; Java ( Sun "-"). Java Java.
Java, , , "" Flash:
 ,
.;
 Java;
 ;
 ;
 -.
:
 Java;
 Java ;

20

I. Silverlight.

 Java -

Java, .
Java , Flash. Java
Web-.

Microsoft Silverlight

Microsoft Silverlight. , . !
Silverlight 2007 . ,
, Web-
. 2008 , , . 2009 . - .
Silverlight ""
Microsoft .NET. , Web-, Web-.
:
 , .NET, , Visual Basic C#;
 ,
, .;
 -

. ,
Microsoft Visual Studio 2008 Express Edition, Microsoft (http://www.microsoft.com/);
 ;
 ;
 -.

, Silverlight . , ""
"".
:
 Silverlight -

1. Silverlight

21

 Silverlight (-

Microsoft).
! , , .
, HTML+CSS+JavaScript, Flash Java! Silverlight !

?
.
, Silverlight-,
. 3,
, - Silverlight-.


Silverlight
WWW, , Web-,
.
""
Microsoft Silverlight 3.
Silverlight. , Silverlight, :
, . , Silverlight
.
. ...


: . .
,
"" . "" Windows- ,
, .
Web- , : Web, .
"", Web- . ,
1. Web-

2. Silverlight

23

""
.
"" ,
( , , .).

.

. , Web-, HTML+CSS+
JavaScript, HTML CSS, JavaScript. Web Flash , , ActionScript.
Silverlight , .

Silverlight- .

Silverlight-
, ""
.

Windows-
. Web- Silverlight
.

Silverlight- Web-!
.

Windows-, .
, , .
Silverlight; Silverlight-.
Windows, , Windows-. ,
, .
Silverlight , Web-
"" Silverlight-; ,

24

I. Silverlight.

, Web- Silverlight .
Windows- ,
. Windows : , , , -, .
, , ,
, .
Windows- ,
, , . , , , . ,
.
Silverlight- - ,
. .
Silverlight- , ( 18 19).
- Silverlight 3 , . ,
, , .
Silverlight 3 . ...

Silverlight-
Web-
Silverlight- .
Web- Silverlight . ( Web- 1.)
, , HTML-
Web- HTML, -
. , Web- Web-,
,
Web- .

2. Silverlight

25

, Silverlight- , Web- . (,
: ,
- , Adobe PDF .)
, Web-,
Web- Silverlight.
Web-.
Silverlight-
Web-. Web-
Silverlight . ,
, Silverlight-.

" ". " " ( , , , ,


.), , , ,
, . "",
(, , , .).
" " . "" , "" ,
,
.
"" .
" "
Silverlight . ,
.
-, ,
. ,
, .
-, " ". . , , . , , , ,
.

26

I. Silverlight.

, Silverlight, , "" .

, : ( )
.
...
, . , , ,
!.. -
; , .

? .
:
 "" , ;
 , ;
 , ;
 ,
.

. , . , .
Silverlight.
, ; ,
.
(),
.
: ,
. .
Silverlight .
 "". -

. ,
. .

2. Silverlight

27

 "". -

.
 "". , -

.
. 2.12.3 , , .

. 2.1. ""

Y1

Y2

Y3

X1

. 2.2. ""
(
)

X2
X3
. 2.3. ""

. 2.4 .
"" "" "" .
Silverlight-
. , ...
.

28

I. Silverlight.

""


""

""

. 2.4.

Silverlight-
, . , . , - , ! ,
, , .
.

Silverlight-.
, Web- Web- Silverlight. Web-, Web , Silverlight-, .
?
... .
?
.
, , Web-, . , "",
" ", , , " ".
;
.
Silverlight ,
.

2. Silverlight

29

"" Silverlight.
, .
? .
, .
, -, , -, , .
, ,
.
, Silverlight-,
, , . ;
.
,
, "", -.
Silverlight- . !

.
, . , ,
.
. , " ". , . (, ), (, )
, .
,
, .
, .
, .
"" .
?
, Silverlight
, .

30

I. Silverlight.

" " ,
, .
.
, .
. ,
,
, .
, , ,
.
, . , ,
. ; ,

, .
, :
 ,

, , , , .;
 ,

.;
 , , -

.
Silverlight- ! . ,
, .
Silverlight!
, . , .
. ,
, ,
. "", , .
, ,
" " , ,

2. Silverlight

31

" ". "". "".


.
.
, ,
(. .
; ).
, .
. ,
(, ,
).
;
, .
, ,
, . .
, , .
.

.

, ,
. , Silverlight, Microsoft.
Silverlight-.

- Windows-. ,
. , "" ,
( , ), . ,
,
.
, , , .
, , ?

32

I. Silverlight.

. , .
, , . ,
, , . ,
, , ,
. ;
,
,
.
, , ,
, , . , , - "" , .
.
- ,
. ,
.
, " ",
"", .
.
"", ,
, .
Silverlight , , . "" ;
. !

,
Silverlight-
, , Silverlight-.
, , ,
. ,
( ), ,

, . .

2. Silverlight

33

, , . .
, .
. -, , , (, )! ...
1. , , .
2. .
3. ( , , ) ,
.
, ,
Silverlight-. . ,
.
. , .

, , , .


Silverlight-
, ,
Web-
Silverlight. - .

XAML (eXtensible Application Markup Language
; "").
XML (eXtensible Markup
Language ). , XAML , ; , , , , , . ( XAML HTML,
.)
, .NET,
, Visual Basic C#. C# ( "-").

34

I. Silverlight.

XAML ,
Microsoft . C#
"" MSIL
(MicroSoft Intermediate Language Microsoft).
Silverlight.
--! , ? - ?

?
,
,
. ,
.
.
Microsoft Visual Studio 2008 Express Edition,
, Silverlight,
XAML C#. !


Silverlight-
, .
, , , , . ,
Silverlight- :
XAML C#.
Silverlight-.
Microsoft Visual Studio 2008 Express Edition, ,
Microsoft Visual Web Developer 2008
Express Edition, Web-, . , , XAML
C#.

Microsoft Visual Web Developer 2008


Express Edition
Microsoft Visual Web Developer 2008 Express Edition, ,
, Microsoft Visual Studio 2008 Express
Edition Web-.
Silverlight 3 Tools , ,
Silverlight 3.
.
Visual Web Developer 2008 (
, ). (Start),
(Programs)
(All Programs),

36

I. Silverlight.

Microsoft Visual Web Developer 2008 Express Edition. (. 3.1).

. 3.1. Microsoft Visual Web Developer 2008 Express Edition

, , . Visual Web Developer 2008


, ,
.
, .
;
, . ,

. . 3.2 Solution
Explorer, ; , , Visual Web
Developer 2008, .
,
.
 "" .
 "" -

( "", , -

3. Silverlight-

37

. 3.2; Solution Explorer Database


Explorer). ""
, "" ;
.

. 3.2. Solution Explorer ()

 "" .
 ( ). -

,
. . 3.3 ,
Toolbox,
CSS Properties Manage Styles. ,
(. 3.4);
, .
. , Visual Web Developer 2008.
. , ,
( ,
Windows).

38

I. Silverlight.

. 3.3. ,
Toolbox,
CSS Properties Manage Styles

. 3.4. Toolbox,

. , ,
. ;
. . 3.5.
Visual Web Developer 2008 . , , ,

3. Silverlight-

39

Microsoft, Web-; , ,
.

. 3.5.

(
). - Silverlight-.

.
- . .
1 2, Silverlight-
XAML C# ( , MSIL; C#). . , . ,
Silverlight,
, .
, ,
( ). (,
) : XAML-,
C#-. , , Visual Web Developer 2008,
. ,
, Visual Web Developer 2008.
,
. - ,
" " . ?
( Visual Web
Developer 2008 ) . , :
 ;
 (, , ,

. .);

40

I. Silverlight.

 (, Silverlight- -

Web-, Web-).
, ,
. ,
.
, , .
. Visual Web Developer 2008 .
Visual Web Developer 2008 . , .

Silverlight-
Silverlight- ( )
, New Project File
<Ctrl>+<Shift>+<N>. New Project
(. 3.6), (
).

. 3.6. New Project

3. Silverlight-

41

Visual Web Developer 2008,


Silverlight-,
C#. Project types "" Visual C#,
Silverlight .
Templates Silverlight Application, . . Silverlight-.
, , . Name.
, ,
Convertor.
(
), Create directory for solution. , ,
.
OK. ?
. Visual Web Developer 2008, ,
Web-
Silverlight-. New Silverlight Application (. 3.7).

. 3.7. New Silverlight Application

,
Visual Web Developer 2008 .
Host the Silverlight application in a new Web site. OK.
Visual Web Developer 2008
. . 3.8. !

42

I. Silverlight.

. 3.8. Visual Web Developer 2008


Silverlight-,
Visual Web Developer 2008, .
?
, . ,
XAML,
( ,
).
? , Visual Web
Developer 2008, . , ,
.
Visual Web Developer 2008
.
, .
, -
. Visual Web

3. Silverlight-

43

Developer 2008. , , ;
, .
, , .

Solution Explorer
Visual Web Developer 2008, , Solution Explorer (. . 3.2).
, , .
Solution Explorer , , Solution Explorer View
<Ctrl>+<Alt>+<L>.
Solution Explorer . Visual
Web Developer 2008 , .
( ),
( . 3.2).
"" Solution Explorer . "" "".
 "" Properties -

. 24.

 "" References , .

4.

 "" App.xaml

XAML, .
13.
 "" MainPage.xaml

XAML, (
).
,
"" MainPage.xaml. -
MainPage.xaml.cs. C#, (
).
.
: , XAML,
xaml, C# xaml.cs.

44

I. Silverlight.

: , , Visual
Web Developer 2008 MainPage. ,
: MainPage.xaml MainPage.xaml.cs.
: , ,
App. : App.xaml App.xaml.cs.
Solution
Explorer, .
.
Silverlight-, Visual Web
Developer 2008 , XAML
. , MainPage.xaml.
.
, .


Silverlight-
XAML ( ):
<UserControl x:Class="Convertor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot">
</Grid>
</UserControl>

?..

XAML
- , XAML.
.
XAML Silverlight. 1. ,
.
2 ,
, . . : XAML ,

3. Silverlight-

45

, ,
.
? -, , . -, ,
. -, .
, Silverlight-
-. 2 ,
, . ,
-, , .
, XAML .
, , XAML. ,
,
. , .
XAML , <
>. ( 2, ; , .)
<
.
XAML : ; .
, : , < >. <
/; . , , .
,
XAML:
<TextBox></TextBox>
TextBox

, "

".
( ).
:
<TextBox />

46

I. Silverlight.

, /.
, Visual Web Developer 2008
XAML .

- .
, . :
 XAML, , -

;


;
 < >="< >", -

.
, ,
" ", Text TextBox (. . ),
XAML;
<TextBox Text=" "></TextBox>

, , .
,
60 , MaxLength.
60.
<TextBox Text=" " MaxLength="60"></TextBox>

, .

XAML. , .
<TextBox Text=" " MaxLength="60" />

/ .
,
/ . - XAML
.

- . ?

3. Silverlight-

47

.
<Grid></Grid>

XAML Grid "" ( 2). .


XAML, , ""
.

<Grid>

<Grid>
<TextBox Text=" " MaxLength="60" />
</Grid>

- . ,
. ,
, () , "" , . .
:
<Grid>
<TextBox Text=" " MaxLength="60" />
<TextBox Text=" " MaxLength="40" />
<TextBox Text=" " />
</Grid>

"" .
? XAML ? .
XAML, Visual Web
Developer 2008 ,
. <UserControl>.
<UserControl x:Class="Convertor.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</UserControl>

2, Silverlight- , .
.
, UserControl ,
. , ,
Convertor.MainPage x:Class <UserControl>.

48

I. Silverlight.

x:Class .
. Visual Web Developer 2008
Silverlight , UserControl.
<UserControl>, , .
4. , Silverlight-, .
- <UserControl> .
, , ,
, ,
.

<UserControl> <Grid>, "":


<Grid x:Name="LayoutRoot">
</Grid>

( 2).
<Grid> x:Name LayoutRoot.
.

.
Toolbox
2 ,
. , , .
, . , !
, MainPage.xaml, XAML
. , "" Solution
Explorer (. . 3.2) .
,
Toolbox.
. , ,
Toolbox View
<Ctrl>+<Alt>+<X>.
XAML, Toolbox
,
Silverlight ( . 3.4). .

3. Silverlight-

49

"": Silverlight XAML Controls General.


"", Visual Web
Developer 2008 ; "" .
, ,
. , , .
TextBlock. ""
Silverlight XAML Controls TextBlock,
. ,
Toolbox, ,
, .
<Grid x:Name=
"LayoutRoot"> </Grid>, . . , .
.
Visual Web Developer 2008 XAML- , , .
XAML-,
, (, , ):
<Grid x:Name="LayoutRoot">
<TextBlock></TextBlock>
</Grid>

( , ). ,
, .
Text, TextBlock.
, . ,
XAML <TextBlock> .
TextBlock > . ... , !
Visual Web Developer 2008 (. 3.9).
, , Visual Web Developer 2008, .
- ,
, <Enter>.
, , <Esc>
.

50

I. Silverlight.

. 3.9.

, .
, , , .
Text Text.
Visual Web Developer 2008 <TextBlock>,

.
"".
! , Visual Web Developer 2008 .
, . XAML-, :
<Grid x:Name="LayoutRoot">
<TextBlock Text=""></TextBlock>
</Grid>

, !..
. <TextBlock>, TextAlignment (
) Right (). Text=""
TextAlignment.
Visual Web Developer 2008 , , Right. !
<Grid x:Name="LayoutRoot">
<TextBlock Text="" TextAlignment="Right"></TextBlock>
</Grid>

, . Toolbox
XAML- TextBox .

3. Silverlight-

51

<TextBox>, Text,
0. .
, ,
Silverlight .
Width (), Height () TextBox . 100 22 ,
<TextBox>.
XAML- :
<Grid x:Name="LayoutRoot">
<TextBlock Text="" TextAlignment="Right"></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
</Grid>

, , . ,
XAML, , ,
, ,
. :
<Grid x:Name="LayoutRoot">
<TextBlock Text="" TextAlignment="Right"></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
<TextBlock Text="" TextAlignment="Right"></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
</Grid>


, , . , IsReadOnly ( ) true (, ""). , Visual Web Developer 2008 , .
( XAML-,
):
<TextBox Text="0" Width="100" Height="22" IsReadOnly="true"></TextBox>

,
. Button.
"" .
.
Content Button. <Button>.

52

I. Silverlight.

XAML- :
<Grid x:Name="LayoutRoot">
<TextBlock Text="" TextAlignment="Right"></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
<Button Content=""></Button>
<TextBlock Text="" TextAlignment="Right"></TextBlock>
<TextBox Text="0" Width="100" Height="22" IsReadOnly="true"></TextBox>
</Grid>

, <Ctrl>+<S>. .
, -, ,
. , Visual Web Developer 2008
, . , Web-...


Silverlight-
, Visual Web Developer 2008 .
Silverlight- Visual Web Developer 2008 Web-, HTML-.
, , , Web-
Web-.
Silverlight- Visual Web Developer 2008,
:
 Start Debugging (

) ;

 Start Debugging Debug;


 <F5>, , , .

, , , .
, , ...
, , . 3.10.
-, , . ,
...
Web-, .
!

3. Silverlight-

53

. 3.10. Silverlight-
Web-

""
2 , ""
. ,

( ).
"", -, , -,
.
, "" (. . ) "" .
. 3.10.

Grid RowDefinitions ColumnDefinitions .
, , .
, ( ) ; , , .

.
.
. RowDefinitions Grid.
, . <Grid>,
, XAML
<< >.< >></< >.< >>

54

I. Silverlight.

,
, , .
. Toolbox
.
:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
. . .
</Grid>

XAML-, "",
, .
.
.
, RowDefinition, .
XAML. ,
.
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
. . .
</Grid>

. . Height
RowDefinition .
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
. . .
</Grid>

.
ColumnDefinitions Grid;

3. Silverlight-

55

ColumnDefinition, . - Width
ColumnDefinition .
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
. . .
</Grid>

- , .
:
Grid.Row="< , >"
Grid.Column="< , >"

, , ,
, ,
.

, ,
.
, ,
( ):
<TextBlock Grid.Row="0" Grid.Column="0" Text=""
TextAlignment="Right"></TextBlock>


, .
XAML-:
<TextBlock Grid.Row="0" Grid.Column="0" Text=""
TextAlignment="Right"></TextBlock>
<TextBox Grid.Row="0" Grid.Column="1" Text="0" Width="100"
Height="22"></TextBox>

56

I. Silverlight.

<Button Grid.Row="1" Grid.Column="1" Content=""></Button>


<TextBlock Grid.Row="2" Grid.Column="0" Text=""
TextAlignment="Right"></TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" Text="0" Width="100" Height="22"
IsReadOnly="true"></TextBox>

.
. . 3.11. , , ,
.

. 3.11. Silverlight-

.
...

Silverlight-
. .
MainPage.xaml.cs, C#, . , "" MainPage.xaml
Solution Explorer (. . 3.2)
MainPage.xaml.cs. .
. , :
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
}

3. Silverlight-

57

MainPage class. (
, , , .)
UserControl , MainPage
.
, . , MainPage
. ,
. (: .)
MainPage ,
, , MainPage.
, .
, .
, ! . , ,
,
. - !


: ,
?
: - .
( )
x:Name. XAML,
. .
.
 , -

, .
 , . .

.
, . .

58

I. Silverlight.

:
 txtInches;
 btnConvert;
 txtMillimetres.

MainPage.xaml, . XAML x:Name, . XAML-, , (


x:Name ):
<TextBox x:Name="txtInches" Grid.Row="0" Grid.Column="1" Text="0"
Width="100" Height="22"></TextBox>
<Button x:Name="btnConvert" Grid.Row="1" Grid.Column="1"
Content=""></Button>
. . .
<TextBox x:Name="txtMillimetres" Grid.Row="2" Grid.Column="1" Text="0"
Width="100" Height="22" IsReadOnly="true"></TextBox>

, XAML, ,
, LayoutRoot (
):
<Grid x:Name="LayoutRoot">
. . .
</Grid>

Visual Web Developer 2008 . ,


.
MainPage.xaml. .


!
. ""
btnConvert.

.
< >="< >". -,
, .
Button Click " ". ,
.

3. Silverlight-

59

<Button> Click, . Visual Web Developer 2008


, . ,
; .
, <New Event Handler>, . .
Visual Web Developer 2008 :
 , ,

, ;
 C# MainPage.xaml.cs,
MainPage,

"" .

, . XAML-,
( , ,
):
<Button x:Name="btnConvert" Grid.Row="1" Grid.Column="1"
Content="" Click="btnConvert_Click"></Button>

2, ( )
. Visual Web Developer 2008 , , , .
MainPage.xaml. MainPage.xaml.cs,
.

C#
C#-, Visual Web Developer 2008
, . :
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
}

btnConvert_Click. , ,
void. ,
. ( , , , , 2.)

60

I. Silverlight.

, , .
 , , ,

. . , (,
, - ).
 , , ,

.
( ), .
, .
 sender object.
object Silverlight, , , . ,
( btnConvert).

 e
RoutedEventArgs.

RoutedEventArgs,
.

( ). .
txtInches ,
txtMillimetres. , .
, txtInches . ,
, . ( ) C# string.
C#-,

. :
btnConvert_Click.
string sInches;

C#! ( , .)
, .
string ()
sInches. Visual Web Developer 2008,

.

3. Silverlight-

61

, ;. , , .
,
txtInches. , :
sInches = txtInches.Text;

. ,
=.
txtInches Text,
. . Visual Web Developer 2008, Text
txtInches.
= , .

sInches. = C#,
.
, ,
, , .
, , sInches Text txtInches.
. , txtMillimetres , txtInches,
".". .
, ,
:
txtMillimetres.Text = sInches + ".";

+ , .
, ( sInches)
( ".") , .
,
, . , :
[< >.]< >
([< , >])

Focus txtInches, , , .
txtInches.Focus();

62

I. Silverlight.

. , ,
, .
! MainPage.xaml.cs .
- .
, ',
.

C#,

25,4 . , ,
txtInches, 25,4, .
Text TextBox . , , . , Visual Web
Developer 2008 .
.
, , . Silverlight . .
, ( ).
, , :
< >.< >
([< , >])

, , .
.
, , .
, ""
, bool.
: true ("") false ("").
true , ,
false (, - , "",
).
double
. , ?

3. Silverlight-

63

, . , .
string sInches;
double sgInches, sgMillimetres;
bool isConverted;

:
 sInches ,
txtInches;

 sgInches sgMillimetres -

;
 isConverted , -

.
sInches = txtInches.Text;

sInches ,
txtInches.
isConverted = double.TryParse(sInches, out sgInches);

.
TryParse double (-,
double , , , ).
:
 (
sInches);

 , , ,

, (sgInches). out
, ,
, .
, TryParse
true, false.
isConverted .
, . .
if (isConverted)
{
sgMillimetres = sgInches * 25.4;
}

64

I. Silverlight.

if , .
, if
( isConverted), , true, , . false, .
,
sgInches ( ), 25,4 sgMillimetres. , isConverted true, . . txtInches
, sgInches , .
25,4.
25.4. C#
.
else
{
sgMillimetres = 0;
}

, if
false, ,
else . sgMillimetres 0.
txtMillimetres.Text = sgMillimetres + ".";

, ... .
( sgMillimetres) ("."), Silverlight , , . !
.

. MainPage.xaml.cs .
, ,
.
, .
, Silverlight- - !

3. Silverlight-

65


.
, ...
Visual Web Developer 2008 . ,
, , . , , ; ,
...
, , Visual Web
Developer 2008 -, . -, , Yes. -,
,
No. .
, Visual Web
Developer 2008 Error List (. 3.12).
;
Description (-), File ,
, Line , Column
. - , .

. 3.12. Error List

, Error List,
, Visual Web Developer 2008 ,
.
,
; .
, Visual Web
Developer 2008 Output, ,
. Error
List, .
, .
.

66

I. Silverlight.


Visual Web Developer 2008
Visual Web
Developer 2008. .
, , .
, <Ctrl>+<S>. Save < , >
( ) File.
,
<Ctrl>+<Shift>+<S>. Save
All ( ) File.
, ,
. , ,
, Solution Explorer. Close File.
, ,
Close Project File.
Open Project File <Ctrl>+<Shift>+<O>.
Open Project,
Windows. ( csproj),
, Close Solution,
.

Open Project Close


Solution, Visual Web Developer 2008 . Add to
Solution, ,
. (
.)

Visual Web Developer 2008 , . Open


File ( ) , File
<Ctrl>+<O>. -

3. Silverlight-

67

Open File,
Windows. .
Visual Web Developer 2008
. Exit File.

?
, Visual Web Developer 2008
. -
Silverlight-!
- , ,
.
.

68

I. Silverlight.

II

, ,
,

4.

5.

6.

7.

8.


- Silverlight. " " Visual Web
Developer 2008, XAML C#, ,
, . !
, ,
, .
20 .
, ,
. , , .

,

,
Silverlight- Converter. Visual Web
Developer 2008 < >\Visual Studio 2008\Projects.
, , Visual Web Developer 2008
, . .
? .
.
 , , csproj.

"" ; ,
.

72

II. , , ,

 ,

, csproj.user.
 ( ), , , , sln.
Visual Web Developer 2008 ,
. , .

 ,

, suo.
 , XAML-, ,
, xaml.
 , C#-, ,
, xaml.cs.
,
, Visual Web Developer 2008 ;
XAML-, C#-.
( ), .
App,
,
App.xaml App.xaml.cs, MainPage.xaml MainPage.xaml.cs.
.
.
Properties .
, , , .
, Visual Web
Developer 2008 ( , 24).
obj , Visual Web Developer 2008 .
, .
, , , App.g.cs
MainPage.g.cs. C# App MainPage
; Visual Web Developer 2008 .
Bin .
Debug, .
, .

MainPage.

4.

73

...
 ( ), -

. DLL,
.
 , ;

. AppManifest.xaml.
.
.
 . ZIP, ,

, xap
. -, .
 Web-, ,

. Web.
.
 , , , pdb. Visual Web
Developer 2008 ( Visual
Web Developer 2008 ).

?..
Visual Web
Developer 2008, , .
, (
), exe. Silverlight- dll, . . Windows. Silverlight.
, . (
) ( ,
), , Windows. Visual Web Developer 2008
, .
, .

74

II. , , ,

, Silverlight-? . : , , .
Silverlight.
Silverlight , . , , .
, - , . ,
Visual Web Developer 2008
. (
Converter) .
, ,
3 Solution Explorer (. . 3.2). "" References,
. ""
.
, , . Silverlight .NET
, .
Visual Web Developer 2008, "" .
, .
, Silverlight 3 Tools
; (
), .
Visual Web Developer 2008.
,
, Visual Web Developer 2008 .
, , .
, ""
References Solution Explorer Add Reference. Add Reference (. 4.1).
: ,
, OK. ,

4.

75

, Component
Name . ,
Cancel.

. 4.1. Add Reference


""
References Solution Explorer
Remove.



. .


, ,
. , ,
, ( ),
.

( ). -

76

II. , , ,

"" :
, , ,
, , , (,
.).
Silverlight . ,
TextBox ( ) Controls,
Windows, , , System.
, , . -. , -
, -
.
,
(, System.Windows.Controls).
, , .
Visual Web Developer 2008 , .
.
MainPage.xaml.cs :
namespace Convertor
{
. . .
}

namespace . , , , , .
, .
, . ,
, , .
, ,
, :
namespace Converter
{
namespace Converter_Inner

4.

77

{
. . .
}
}

Converter_Inner
Converter. ...


C#.
.




.
.
. , ,
"" "". , .
, .
:
namespace Converter
{
namespace Converter_Inner
{
. . .
}
}

Convertor_Inner :
Convertor.Convertor_Inner.
, .
"" . , Converter
Converter.

78

II. , , ,


, , , , ,
.
namespace Converter
{
namespace Converter_Inner
{
class DataClass
{
}
}
}

DataClass

Convertor.Convertor_Inner.DataClass. TextBox
System.Windows.Controls.TextBox, String System.String.

! XAML- <UserControl>, , x:Class, ?


Convertor.MainPage. !


, Silverlight ,
. , -,
( TextBox ...),
- -, !
. , 3 ,
String System.String. ?
, Visual Web Developer 2008 . ,
,
, .
C#. using,
. ;
.
.
C#, . , Visual Web Developer 2008
"" .

4.

79

C#:
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Net;
System.Windows;
System.Windows.Controls;
System.Windows.Documents;
System.Windows.Input;
System.Windows.Media;
System.Windows.Media.Animation;
System.Windows.Shapes;

Visual Web Developer 2008.


.
,

. ,
.

XAML-.
C#-. XAML-?
.
XAML . .
XAML , C#. .
2, XAML
, Silverlight .
Silverlight, " " ,
MSIL, C#.
, .
, , Visual Web
Developer 2008. xmlns
<UserControl>, .
:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

,
XAML , . -

80

II. , , ,

, Silverlight.
xmlns <UserControl>:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

.
, x:
(x:Class, x:Name ). , ( ).

, ,
<UserControl> xmlns :
xmlns:<>="clr-namespace:< >;
assembly=< , ,
 >"
. ,
.

, , :
<<, >:< >>


, .
.
Calendar. ,
. Calendar Toolbox XAML- , .
, <UserControl> , Visual Web Developer 2008:
xmlns:controls="clr-namespace:System.Windows.Controls;
assembly=System.Windows.Controls"

"" System.Windows.
Controls, System.Windows.Controls.dll, ,
controls. ,
, ,
System.Windows.Controls.

4.

81

, Calendar:
<controls:Calendar></controls:Calendar>

, Calendar
controls. Visual Web Developer 2008 , System.Windows.Controls.dll, .
, , , Toolbox. Visual Web Developer 2008
XAML- : ,
, , , .

?

Silverlight: , , , . .

Silverlight: , , ,
. .


, , . ,
Silverlight.
Silverlight.
, Silverlight-.
Silverlight; .

Silverlight . 2, Silverlight-, "", "" "".


.

""
"" ,
.
"" Grid. ,
<UserControl> .
"" <Grid>, , : , , , .

5.

83

<Grid>
<TextBlock Text=""></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
<Button Content=""></Button>
<TextBlock Text=""></TextBlock>
<TextBox Text="0" Width="100" Height="22" IsReadOnly="true"></TextBox>
</Grid>

"", Width Height Grid. , .


(Double), (. . ).
<Grid Width="400" Height="300">

, , ,
, ("" ). , .
Web- "" . Grid:
 MaxWidth , -

 MaxHeight , -

 MinWidth , -

 MinHeight , -


(Double),
.
<Grid MaxWidth="600" MaxHeight="400" MinWidth="100" MinHeight="100">

Margin ,
,
. :
< >,< >,< >,< >

84

II. , , ,

, ,
.
<Grid Margin="5,10,5,10">

: HorizontalAlignment VerticalAlignment.
"" , , .
. , (),
. - .
.
, , (. . , ). ,
.
! HorizontalAlignment
HorizontalAlignment. :
 Left ;
 Center
;
 Right ;
 Stretch ,
.
VerticalAlignment VerticalAlignment.
:
 Top ;
 Center
;
 Bottom ;
 Stretch ,
.
- ,
Silverlight , Stretch ( "" -

5.

85

). , .
<Grid Width="400" Height="300" HorizontalAlignment="Left"
VerticalAlignment="Bottom">

Grid ShowGridLines. ; true


, false . , false.
<Grid ShowGridLines="true">

ShowGridLines, , , ,
.
.
, ,
, , .
Visibility.

Visibility,
: Visible ( ; ) Collapsed
( ). ,
.
Visibility ,
C#-.
<Grid x:Name=grdContainer"" Visibility="Collapsed">
. . .
grdContainer.Visibility = Visibility.Visible;

, C#- . ( Visibility),
,
(Visible).
"" , ,
, .
. ,
,
( ). , "" , . ,
.

86

II. , , ,

RowDefinitions. RowDefinitionCollection; RowDefinition,


.
, , RowDefinition,
.
, ,
. XAML
. , , Silverlight, .
:
 XAML, , -

, << >.< >>.


, , ;
 XAML, ,
.
RowDefinitions:
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
. . .
</Grid>

RowDefinitions Grid
<Grid.RowDefinitions>, <Grid>.
<Grid.RowDefinitions> <RowDefinition>,
RowDefinition. ,
.
RowDefinition Height,
. :
 ;
 Auto ,

 * .

5.

87

< >*.

, ,
.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
. . .
</Grid>

ColumnDefinitions.
ColumnDefinitionCollection
ColumnDefinition, .
ColumnDefinition Width, .
, Height RowDefinition.
<Grid>
. . .
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
. . .
</Grid>

, , :
Grid.Row="< , >"
Grid.Column="< , >"

, ,
.
<Grid>
. . .
<TextBlock Grid.Row="0" Grid.Column="0" Text=""></TextBlock>

88

II. , , ,

<TextBox Grid.Row="0" Grid.Column="1" Text="0" Width="100"


Height="22"></TextBox>
<Button Grid.Row="1" Grid.Column="1" Content=""></Button>
<TextBlock Grid.Row="2" Grid.Column="0" Text=""></TextBlock>
<TextBox Grid.Row="2" Grid.Column="1" Text="0" Width="100" Height="22"
IsReadOnly="true"></TextBox>
</Grid>


, .
Grid.RowSpan="< , >"
Grid.ColumnSpan="< ,

 >"
, ,
,
"" Grid.Row Grid.Column.
Grid.RowSpan , ; ,
Grid.Row Grid.Column, ""
. , .
Grid.ColumnSpan ,
; ,
Grid.Row Grid.Column, "" . ,
, .
<Grid>
. . .
<Button Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
Content=""></Button>
. . .
</Grid>

,
( 1)
.
, , "".

5.

89

""
"", , Silverlight.

.
"" StackPanel.
.
"" <StackPanel>,
, :
, , , .
<StackPanel>
<TextBlock Text=""></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
<Button Content=""></Button>
<TextBlock Text=""></TextBlock>
<TextBox Text="0" Width="100" Height="22" IsReadOnly="true"></TextBox>
</StackPanel>

StackPanel Orientation,
, .
Orientation, :
 Vertical

( );

 Horizontal

.
, StackPanel Width, Height, MinWidth,
MinHeight, MaxWidth, MaxHeight, Margin, HorizontalAlignment, VerticalAlignment
Visibility, "".
<StackPanel Width="200" Height="800" HorizontalAlignment="Right">

""
""
.
.
"" Canvas. .
"" <Canvas>, , : , , , .

90

II. , , ,

<Canvas>
<TextBlock Text=""></TextBlock>
<TextBox Text="0" Width="100" Height="22"></TextBox>
<Button Content=""></Button>
<TextBlock Text=""></TextBlock>
<TextBox Text="0" Width="100" Height="22" IsReadOnly="true"></TextBox>
</Canvas>

, Canvas Width, Height, MinWidth,


MinHeight, MaxWidth, MaxHeight, Margin, HorizontalAlignment, VerticalAlignment
Visibility, "".
<Canvas Width="400" Height="300" HorizontalAlignment="Center"
VerticalAlignment="Center">


:
Canvas.Left="< >"
Canvas.Top="< >"

,
.
(Double) .
, ,
.
<Canvas>
<TextBlock Canvas.Left="20" Canvas.Top="20" Text=""></TextBlock>
<TextBox Canvas.Left="100" Canvas.Top="20" Text="0" Width="100"
Height="22"></TextBox>
<Button Canvas.Left="100" Canvas.Top="60"
Content=""></Button>
<TextBlock Canvas.Left="20" Canvas.Top="100"
Text=""></TextBlock>
<TextBox Canvas.Left="100" Canvas.Top="100" Text="0" Width="100"
Height="22" IsReadOnly="true"></TextBox>
</Canvas>

, "",
. , XAML- , , . ,
Z- . Z- ""
""; Z-
Z-.

5.

91

Z-
:
Canvas.ZIndex="<Z->"

, Z- . , .
<TextBlock Canvas.Left="20" Canvas.Top="20" Canvas.ZIndex="2"
Text=""></TextBlock>
<TextBlock Canvas.Left="20" Canvas.Top="20" Canvas.ZIndex="1"
Text=""></TextBlock>

, . .
Z- ( ...).

,
. UserControl, , .


, .
Loaded.

.
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
txtInches.Text = "0";
}

, Text txtInches "0". Silverlight- .


, . (,
3.)
 sender, object (-

Silverlight, , ,
) ,
.

 e

RoutedEventArgs.

92

II. , , ,

; , .
Silverlight. : .
.

?
,
. ,
Silverlight . ,
.


Silverlight. "" Silverlight-.
.
-"". Silverlight ,
Silverlight 3 Tools.
, .
, Silverlight,
. , ,
.

TextBlock
C TextBlock
3. ;
, , - .
TextBlock .
, TextBlock <TextBlock>. (
.)
<TextBlock Text=""></TextBlock>

Text. ,
, .

94

II. , , ,

TextBlock Width, Height, MinWidth,


MinHeight, MaxWidth, MaxHeight, Margin, HorizontalAlignment, VerticalAlignment
Visibility, "" (. 5).
<TextBlock Text="" Width="100" Margin="5,0,5,0"></TextBlock>

TextAlignment . TextAlignment :
 Left ( );
 Center ;
 Right .
<TextBlock Text="" TextAlignment="Center"></TextBlock>

TextDecorations "" . TextDecorationCollection.


Silverlight ""
. XAML- :
<TextBlock Text="" TextDecorations="Underline"></TextBlock>

C#- :
lblInches.TextDecorations = TextDecorations.Underline;

lblInches .
.
3 , , . , TextDecorations , Underline ;
TextDecorationCollection,
.
C#- ,
TextDecorations null.
.
lblInches.TextDecorations = null;

, , TextWrapping. TextWrapping : NoWrap ( ; ) Wrap ( ).


<TextBlock Text=" ---- ."
TextWrapping="Wrap"></TextBlock>

6.

95

Padding
, .
:
< >,< >,< >,< >

.
<TextBlock Text="" Padding="20,2,20,2"></TextBlock>

, , .
FontFamily . ,
, .
Silverlight
Layout, Text, and Input / Text and Fonts
Silverlight.
<TextBlock Text="" FontFamily="Courier New"></TextBlock>
<TextBlock Text="" FontFamily="Verdana,Arial"></TextBlock>

FontFamily ,
Silverlight ,
, .
, . .
"Portable User Interface",
,
Silverlight .
FontSize
.
<TextBlock Text=" !"
FontFamily="Courier New" FontSize="36"></TextBlock>

11 .
FontStretch
. FontStretch. (
"" .
10.)
FontStretch FontStretches. :
 UltraCondensed, ExtraCondensed, Condensed SemiCondensed -

, ;
 Normal -

( );

96

II. , , ,

 SemiExpanded, Expanded, ExtraExpanded UltraExpanded -

, .
FontStretch, .
FontStretch XAML- :
<TextBlock Text="" TextStretch="Expanded"></TextBlock>

C#-:
lblInches.TextStretch = TextStretches.Expanded;

lblInches .

FontStretch
. , ,
...

FontStyle :
. FontStyle. FontStyles, : Normal
( ; ) Italic ( ). FontStyle, .
<TextBlock Text="" TextStyle="Italic"></TextBlock>
lblInches.TextStyle = TextStyles.Italic;

FontWeight "" .
FontWeight.
FontWeights Thin, ExtraLight, Light, Normal, Medium, SemiBold, Bold, ExtraBold,
Black ExtraBlack. FontWeight, "" , ""
"". Normal ( ), Bold .
<TextBlock Text="" TextWeight="Bold"></TextBlock>
lblInches.TextWeight = TextWeights.Bold;

LineHeight, .
.
Double.NaN ""; Silverlight

6.

97

. LineHeight
C#-, :
lblInches.LineHeight = Double.NaN;

, ,
C#-.
 ActualWidth (, , )


. , . .
,
.
 ActualHeight

. .
 Name , x:Name.

TextBlock

! TextBlock
,
.
, TextBlock Inlines.
InlineCollection
Inline.
5 .
<TextBlock>
<TextBlock.Inlines>
. . .
</TextBlock.Inlines>
</TextBlock>

Inline , ,
, Run LineBreak .
Run ,
. Text, FontFamily, FontSize, FontStretch, FontStyle, FonrWeight
TextDecorations, .
<Run FontFamily="Courier New" FontSize="18" Text=""></Run>

98

II. , , ,

, XAML-
<Run>.
.
<TextBlock> ( TextBlock)
<Run> ( ). , , Run, . ,
, .
<TextBlock FontSize="18">
<TextBlock.Inlines>
<Run FontFamily="Courier New" Text=""></Run>
<Run FontFamily="Arial" Text="TextBlock"></Run>
</TextBlock.Inlines>
</TextBlock>

"" Courier New,


"TextBlock" Arial. 18 .
, - : , . , , ( , )
, .
<TextBlock FontFamily="Courier New" FontSize="18">
<TextBlock.Inlines>
<Run FontSize="12" Text=""></Run>
<Run FontFamily="Arial" Text="TextBlock"></Run>
</TextBlock.Inlines>
</TextBlock>

"" Courier New


12 , "TextBlock" Arial 18 .
, Run,
. , XAML-
"TextBlock". (,
, .)
, LineBreak.
<TextBlock FontFamily="Courier New" FontSize="18">
<TextBlock.Inlines>
<Run FontSize="12" Text=""></Run>
<LineBreak></LineBreak>
<Run FontFamily="Arial" Text="TextBlock"></Run>

6.

99

</TextBlock.Inlines>
</TextBlock>

"" "TextBlock"
.

TextBox
C TextBox
3.
.
<TextBox Text="0"></TextBox>

Text , .
TextBox Width, Height, MinWidth, MinHeight,
MaxWidth, MaxHeight, Margin, HorizontalAlignment, VerticalAlignment
Visibility. ActualWidth, ActualHeight,
FontFamily, FontSize, FontStretch, FontStyle, FontWeight, Name, Padding,
TextAlignment, TextWrapping, TextBlock.
<TextBox Text="0" Width="100" Height="22" TextAlignment="0"></TextBlock>

MaxLength ,
, .
. 0,
; , , .
SelectedText , , .
, SelectedText
( ) ""; .
C#-.
if (txtTest.SelectedText == string.Empty)
{
. . .
}

, SelectedText txtTest TextBox


Empty string (
). == ,
, , , true,

100

II. , , ,

false. (. .
SelectedText txtTest
txtTest ), , ( ).
SelectionStart SelectionLength , , .
, .
, SelectionStart , ,
SelectionLength 0.
C#-.
if (txtTest.SelectionLength > 0)
{
. . .
}

> , ,
, , true,
false.
IsEnabled
. ; true ( ), false
. , C#-.
<TextBox x:Name="txtTest" IsEnabled="false"></TextBox>
. . .
txtTest.IsEnabled = true;

IsReadOnly
, true. false, , ,
( ).
AcceptsReturn ,
<Enter> (
, ). ; true ,
false ( ).
<TextBox Width="200" Height="100" TextWrapping="Wrap"
AcceptsReturn="true"></TextBox>

HorizontalScrollBarVisibility VerticalScrollBarVisibility , , , -

6.

101

.
ScrollBarVisibility :
 Auto ;
 Hidden , -


( );
 Visible ;
 Disabled , -

( ...).
<TextBox Width="200" Height="100" TextWrapping="Wrap"
AcceptsReturn="true" VerticalScrollBarVisibility="Visible"></TextBox>

, Windows-
; <Tab> , <Shift>+<Tab>
. Silverlight-.
"" Silverlight <Tab> , XAML. .
TabIndex.
.
<TextBox x:Name="txtInches" Text="0" TabIndex="0"></TextBlock>
<TextBox x:Name="txtMillimetres" Text="0" TabIndex="1"></TextBlock>

IsTabStop , false.
true ( ) .
TextBox. .
Focus() . true,
, false, .
.
txtInches.Focus();

SelectAll() .
.

102

II. , , ,

Select() .
:
Select(< >,
< >)


. .
TextBox .
.
SelectionChanged ,
.
private void txtTest_SelectionChanged(object sender, RoutedEventArgs e)
{
string s;
s = txtTest.SelectedText;
}

,
s , txtTest .
TextChanged , .
private void txtTest_TextChanged(object sender, TextChangedEventArgs e)
{
string s;
s = txtTest.Text;
}

3 , , . ; , SelectionChanged
RoutedEventArgs, TextChanged TextChangedEventArgs.
, . .
.

PasswordBox
, ,
. .

6.

103

PasswordBox.
,
.
<PasswordBox Password="password"></PasswordBox>

Password
.
PasswordChar , ""
.
<PasswordBox Password="password" PasswordChar="+"></PasswordBox>

PasswordChanged ,
;
TextChanged.
PasswordBox ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxLength,
MaxWidth, MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment,
Visibility Width Focus() SelectAll().

Button
Button 3.
.
<Button Content=""></Button>

Content.
<Button Content="" Width="100" Height="25"
FontStyle="Italic" IsEnabled="false"></Button>

Content , ContentControl. ContentControl


Silverlight. ,
. ,
, 5 .
<Button Width="100" Height="100">
<Button.Content>
<TextBlock Text=" ."
TextWrapping="Wrap"></TextBlock>
</Button.Content>
</Button>

104

II. , , ,

.
<Button Width="100" Height="120">
<Button.Content>
<StackPanel>
<TextBlock Text=" ."
TextWrapping="Wrap"></TextBlock>
<CheckBox></CheckBox>
</StackPanel>
</Button.Content>
</Button>

"", ! ( <CheckBox>)!
.
Content , Silverlight ( TextBlock). , , ,
TextBlock, ContentControl. Content
ContentControl.
Button, ,
, , ClickMode :
ClickMode.

 Release -

<Enter> <> ( );
 Press <Enter>

<> ;
 Hover .
<Button Content=" !"
ClickMode="Hover"></Button>

Click .
, .
Button ActualHeight,
ActualWidth, FontFamily, FontSize, FontStretch, FontStyle, FontWeight, Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

6.

105

CheckBox
CheckBox ( )
, .
.
<CheckBox Content=" "></CheckBox>

Content. ,
, , . . ContentControl.
<CheckBox Width="100" Height="120">
<CheckBox.Content>
<TextBlock Text=" ."
TextWrapping="Wrap"></TextBlock>
</CheckBox.Content>
</CheckBox>

, , .
. ,
, .
"" IsThreeState. true
Silverlight, ,
, , false (
).
<CheckBox Content=" " IsThreeState="true"></CheckBox>

IsChecked .
true , , false
( ), null ( IsThreeState true).
<CheckBox Content=" " IsChecked="true"></CheckBox>
. . .
if (chkOption.IsChecked)
{
. . .
}

, IsChecked C#-.
, , ,
.

106

II. , , ,

CheckBox Focus() Click.


, .
 Click .
 Checked .
 Unchecked .
 Indeterminate -

.
-
, , - . Click, ,
.
private void chkOption_Click(object sender, RoutedEventArgs e)
{
if (chkOption.IsChecked)
{
. . .
}
else
{
. . .
}
}

CheckBox ActualHeight, ActualWidth,


FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

RadioButton
RadioButton ( )
. .
<RadioButton Content=" "></RadioButton>


Content. ,
, . . ContentControl.

6.

107

<RadioButton Width="100" Height="120">


<RadioButton.Content>
<TextBlock Text=" ."
TextWrapping="Wrap"></TextBlock>
</RadioButton.Content>
</RadioButton>

.
(
), . .
, ,
. GroupName .
<RadioButton Content=" "
GroupName="Opt"></RadioButton>
<RadioButton Content=" " GroupName="Opt"></RadioButton>

Opt .

RadioButton ActualHeight, ActualWidth,


ClickMode, FontFamily, FontSize, FontStretch, FontStyle, FontWeight, Height,
HorizontalAlignment, IsChecked, IsEnabled, IsTabStop, IsThreeState, Margin,
MaxHeight,
MaxWidth,
MinHeight,
MinWidth,
Name,
Padding,
TabIndex,
VerticalAlignment, Visibility Width, Focus() Checked,
Click, Indeterminate Unchecked.

"":
. ...

ListBox
ListBox ( ) . .
<ListBox></ListBox>

ListBox Items,
ItemCollection.

FrameworkElement,
ContentControl, , , .
ListBoxItem, .

108

II. , , ,

, , .
<ListBox>
<ListBox.Items>
<ListBoxItem Content=""></ListBoxItem>
<ListBoxItem Content=""></ListBoxItem>
<ListBoxItem Content=""></ListBoxItem>
<ListBoxItem Content=""></ListBoxItem>
</ListBox.Items>
</ListBox>

ListBoxItem Content,
.
ContentControl,
( ;
Silverlight ), .
ListBoxItem IsSelected. true , , false
. false.
<ListBoxItem Content="" IsSelected="true"></ListBoxItem>

ListBoxItem ActualHeight, ActualWidth,


FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, Margin, MaxHeight, MaxWidth, MinHeight,
MinWidth, Name, Padding, VerticalAlignment, Visibility Width.
ListBox. .
SelectedIndex . ,
-1. C#-.
if (lstTest.SelectedIndex > -1)
{
btnGo.IsEnabled = true;
}

,
.

, -
lstTest ( SelectedIndex, -1), , , btnGo ( IsEnabled true).

6.

109

SelectionChanged ,
.
ListBox ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

ComboBox
ComboBox ( ) . .

Windows . Silverlight .

ComboBox Items,
ItemCollection.

FrameworkElement. ComboBoxItem,

ListBoxItem.
<ComboBox>
<ComboBox.Items>
<ComboBoxItem Content=""></ComboBoxItem>
<ComboBoxItem Content=""></ComboBoxItem>
<ComboBoxItem Content=""></ComboBoxItem>
<ComboBoxItem Content=""></ComboBoxItem>
</ComboBox.Items>
</ComboBox>

ListBox, ComboBox SelectedIndex SelectionChanged.


MaxDropDownHeight
.
.
, , .
 DropDownOpened .
 DropDownClosed .

110

II. , , ,

ComboBox ActualHeight, ActualWidth,


FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

Calendar
Calendar ( ) , .
System.Windows.Controls,
System.Windows.Controls.dll.
<controls:Calendar></controls:Calendar>

Visual Web Developer 2008 controls.


.

SelectedDate . !
 XAML- <>/< >/<>, ,
; -

, .
 C#-

(DateTime), 10.

<controls:Calendar SelectedDate="2009/09/08"></controls:Calendar>

8 2009 .
IsTodayHighlighted , . true ,
false . true.
SelectedDatesChanged , .
Calendar ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

6.

111

DatePicker
DatePicker ( ) , .
System.Windows.Controls,
System.Windows.Controls.dll.
<controls:DatePicker></controls:DatePicker>

Calendar, DataPicker IsTodayHighlighted


SelectedDate SelectedDatesChanged.
Text ,
, . C#-.
.
 CalendarOpened .
 CalendarClosed .
 DateValidationError ,

.
DatePicker ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

Slider
Slider ( ) . .
<Slider></Slider>

Value ,
. .
.
.
 SmallChange ,

-.
0.1.

112

II. , , ,

 LargeChange ,

.
1.
 Minimum , . 0.
 Maximum , . 10.
Orientation
. Orientation, :
 Horizontal ( );
 Vertical .
IsDirectionReversed
, . .
 false
( ).
 true .
ValueChanged .
Slider ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().

ProgressBar
ProgressBar ( )
. .
<ProgressBar></ProgressBar>

Value . .
.
.

6.

113

 Minimum ,

. 0.
 Maximum ,

. 100.
, , "" . ,
, "" .
IsIndeterminate. false
, (
), true "" .
ProgressBar ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, Margin, MaxHeight, MaxWidth, MinHeight,
MinWidth, Name, Padding, VerticalAlignment, Visibility Width.

ScrollViewer
ScrollViewer ( ) ,
- .
.
ScrollViewer ,
. ;
.
<ScrollViewer Width="100" Height="100"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ScrollViewer.Content>
<TextBox Width="200" Height="200"></TextBox>
</ScrollViewer.Content>
</ScrollViewer>

.

Content ContentControl. .
, .
<ScrollViewer Width="100" Height="100"
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">

114

II. , , ,

<ScrollViewer.Content>
<StackPanel>
<TextBlock Text=" "></TextBlock>
<TextBox Width="200" Height="200"></TextBox>
</StackPanel>
</ScrollViewer.Content>
</ScrollViewer>

"",
.
HorizontalScrollBarVisibility VerticalScrollBarVisibility , , ,
.
ScrollBarVisibility :
 Auto ;
 Hidden , -

;
 Visible ;
 Disabled , -

.
HorizontalScrollBarVisibility
VerticalScrollBarVisibility Visible.

Hidden,

ScrollViewer ActualHeight, ActualWidth,


FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus.

TabControl
TabControl ( )
Windows- ; . System.Windows.Controls,
System.Windows.Controls.dll.
<controls:TabControl></controls:TabControl>

TabControl Items,

6.

115

ItemCollection.

,
FrameworkElement;
TabItem, .
<controls:TabControl Width="300" Height="100">
<controls:TabControl.Items>
<controls:TabItem Header=" ">
<TextBlock Text=" "
TextWrapping="Wrap"></TextBlock>
</controls:TabItem>
<controls:TabItem Header=" ">
<StackPanel>
<TextBlock Text=" -"></TextBlock>
<TextBox></TextBox>
</StackPanel>
</controls:TabItem>
</controls:TabControl.Items>
</controls:TabControl>

, TabItem Content, .
ContentControl,
, .
, TabItem Header,
. object (
Silverlight, ), , ( ), .
, , ,
.
TabItem ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().
TabControl.
SelectedIndex,
. , -1 ( , ...).
TabStripPlacement , . Dock :

116

II. , , ,

 Top (

);
 Bottom ;
 Left ;
 Right .

Top, Bottom. Left


.

Right

SelectionChanged , .
TabControl ActualHeight, ActualWidth,
FontFamily,
FontSize,
FontStretch,
FontStyle,
FontWeight,
Height,
HorizontalAlignment, IsEnabled, IsTabStop, Margin, MaxHeight, MaxWidth,
MinHeight, MinWidth, Name, Padding, TabIndex, VerticalAlignment, Visibility
Width Focus().
, Silverlight . .

, , . , , , . , , Silverlight.


. .
Silverlight-
Convertor.
, ,
. , - !
Silverlight- C#
Convertor2. ( , 3.)
MainPage.xaml "", , "", 25
, . (

6.

117

.)
XAML- (, , )
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" Height="25">
</StackPanel>

:
 cboMode ,

, ;
 txtToConvert ;
 btnConvert ;
 txtConverted .

cboMode ( )
. btnConvert . txtConverted .
,
.
XAML- (,
, ):
<ComboBox x:Name="cboMode" Width="180" Margin="10,0,5,0">
<ComboBox.Items>
<ComboBoxItem Content=" "
IsSelected="true"></ComboBoxItem>
<ComboBoxItem Content=" "></ComboBoxItem>
<ComboBoxItem Content=" "></ComboBoxItem>
<ComboBoxItem Content=" "></ComboBoxItem>
</ComboBox.Items>
</ComboBox>
<TextBox x:Name="txtToConvert" Width="100" Margin="5,0,5,0"></TextBox>
<Button x:Name="btnConvert" Content="" IsEnabled="false"
Margin="5,0,5,0"></Button>
<TextBox x:Name="txtConverted" IsReadOnly="true" Width="100"
Margin="5,0,10,0"></TextBox>

. , , . . , , , ,
.
.
btnConvert Click. :
double toConvert, converted;
if (double.TryParse(txtToConvert.Text, out toConvert))

118

II. , , ,

{
if (cboMode.SelectedIndex == 0)
{
converted = toConvert * 25.4;
}
else
{
if (cboMode.SelectedIndex == 1)
{
converted = toConvert / 25.4;
}
else
{
if (cboMode.SelectedIndex == 2)
{
converted = toConvert * 0.3;
}
else
{
converted = toConvert / 0.3;
}
}
}
txtConverted.Text = converted.ToString();
}
else
{
txtConverted.Text = string.Empty;
}

txtToConvert
( double). , txtConverted
( Empty string).
.
, , cboMode . SelectedIndex cboMode,
, (0, 1, 2
3).
;
SelectedIndex , -

6.

119

, . /
, , , .
txtConverted,
ToString().
.
"" , btnConvert, - txtToConvert.
txtToConvert TextChanged:
if (txtToConvert.Text == string.Empty)
{
btnConvert.IsEnabled = false;
}
else
{
btnConvert.IsEnabled = true;
}

, !
txtToConvert , , btnConvert
.
, . .

?
, Silverlight
. , Silverlight ...
! , . - .



Silverlight,
. .
Silverlight -.
!
,
( ). , !


Silverlight
. JPEG.
Image. image.jpg , .
! , , . ( 8.) Solution
Explorer (. . 3.2) "" ( , ) Existing Item Add
. Add Existing
Item, , ,
. Solution Explorer

7.

121

, , , Visual Web Developer 2008


.
.

Image
Image. .
<Image Source="image.jpg"></Image>

Image, ,
Source, , . XAML- .

Image Silverlight
JPEG PNG, . .NET Framework Class Library for Silverlight /
System.Windows.Media.Imaging Namespace / BitmapImage Class Silverlight.

, . -
Source Image.
Silverlight ,
21.

Image Stretch,
, ,
, . Stretch :
 None , -

;
 Fill , -

, ;
 Uniform -

,
( );

122

II. , , ,

 UniformToFill

, ;
.
<Image Source="image.jpg" Stretch="None"></Image>

Image .
 ImageOpened

.
 ImageFailed ,

(,
Silverlight ).
Image ActualHeight, ActualWidth, Height,
HorizontalAlignment, Margin, MaxHeight, MaxWidth, MinHeight, MinWidth, Name,
VerticalAlignment, Visibility Width.
<Image Source="image.jpg" Width="480" Height="640"></Image>


, .
. ? .
, Source Image XAML-
, . ImageSource,
- BitmapImage. ,
.
XAML-
Source Image, Silverlight,
XAML,
ImageSource. C#- .
, , Loaded . <UserControl>,
:
<UserControl . . . Loaded="UserControl_Loaded">

<Image>, , Source , , imgSample. MainPage.xaml.cs


Visual Web Developer 2008 ""
UserControl_Loaded. .

7.

123

, , Uri,
( -) .
Uri, .
Uri uFileName;

.
uFileName = new Uri("image.jpg", UriKind.Relative);

new ,
.
.
3 ,
, , , , . ,
, new, .
.
. UriKind ,
. UriKind.Relative UriKind.RelativeOrAbsolute. ( UriKind 21.)
, Uri, , .
BitmapImage,
.
, .
System.Windows.Media.Imaging.BitmapImage bimFile;

BitmapImage System.Windows.Media.
Imaging, . , .
( ,
. 4.)
.
bimFile = new System.Windows.Media.Imaging.BitmapImage(uFileName);

BitmapImage. Uri, .

, . .

124

II. , , ,

Source imgSample.
imgSample.Source = bimFile;

! .
, Silverlight
. ,
.


Silverlight- , .
- - ,
.
, , Media.
media
( media.mp3).
, .
, .
! Solution
Explorer , ,
. Visual Web Developer 2008
Properties; - , <F4>, . Build Action
Resource. , 8.

MediaElement
MediaElement.
.
<MediaElement Source="media.mp3"></MediaElement>

MediaElement, ,
Source, ,
.
XAML- .

MediaElement Silverlight MP3, MP4


Windows Media. Graphic, Animation, and

7.

125

Media / Audio and Video / Supported Media Formats, Protocols, and Log Fields
Silverlight.

MediaElement ActualHeight, ActualWidth,


Height, HorizontalAlignment, Margin, MaxHeight, MaxWidth, MinHeight, MinWidth,
Name, Stretch, VerticalAlignment, Visibility Width.
<MediaElement Source="media.mp3" Stretch="None" Width="400"
Height="300"></MediaElement>

AutoPlay ,
. true
, false . true.
Volume 0 1. 0.5.
Balance -1 ( ) 1 ( ). 0 ( ).
IsMuted . true ,
false . false.
CurrentState , . MediaElementState
:
 Closed ( );
 Opening ;
 Individualizing , -

;
 AcquiringLicense -

(, , );
 Buffering -

;
 Playing ;
 Paused ;
 Stopped

.

C#-.

126

II. , , ,

Position . TimeSpan ,

.
10.
MediaElement
, .
.
 Play() .
 Pause() . -

Play().
 Stop() .

, .
 MediaOpened .
 MediaFailed

.
 MediaEnded .
 CurrentStateChanged
CurrentState (

).

Image MediaElement ( ) , , .
Silverlight.


, MediaElement
. ,
Image.
Source MediaElement XAML- , .
Uri. XAML- Source MediaElement,
Silverlight, XAML,
Uri.
C Uri , . , .

7.

127

, , ,
, Loaded .
MediaElement melSample.
Uri, (
-) ,
.
Uri uFileName;
uFileName = new Uri("media.mp3", UriKind.Relative);

Source melSample.
melSample.Source = uFileName;

?
Silverlight, .
- .
- .
?


,
. , ( ).
.
, ...
!


4 , Visual Web Developer 2008
. (
Windows Silverlight) (
).
? -, ,
MSIL, Silverlight. -, , . -...
"-" ! Silverlight
. , , . , , , .
, Image
image.jpg, Media media.
, !

8.

129

, , . - .
13, , . ,
!


Visual Web Developer 2008 . .
, , .
 Add Existing Item Project.
 ""
Solution Explorer (. . 3.2) Existing Item
Add . ("" , , .)
 <Shift>+<Alt>+<A>.
Add Existing Item, Windows.
.

,
.

Solution
Explorer, "". , ,
.
, - .
Rename Solution Explorer,
<F2>. , ;
<Enter> <Esc> .
,
Exclude From Project .
.

130

II. , , ,

Delete - Solution Explorer <Del>,


, ( ,
) .



, .
. ,
.
, , . ,
, .

, .
.
, (
, .).

( ).
.
, - , ,
, .
, Visual Web Developer 2008 . ,
; , . , , Properties,
.
Properties (. 8.1) . Solution Explorer .
- , Properties
Window View <F4>.

8.

131

. 8.1. Properties

, Properties , , .
Build Action Copy to Output.
Build Action , Solution Explorer.
, : Resource (
) None ( ).
Copy to Output . , .
 Do not copy , -

.
 Copy always ,

( Debug
Release, bin, , ,
).
 Copy if newer , -

, , ,
.
,
, .
.

132

II. , , ,


XAML- C#- ,
Silverlight .
, . . . , , .
Silverlight ,
. .
 Web- Silverlight

, , Web.
 Web- Silverlight Web-

, Web- Web- , , , Web.


, .
Silverlight , , .



- ,
. Silverlight, ,
, .
,
New Folder Add ""
Solution Explorer. ;
<Enter>
.
. New Folder Add, , .
,
, . . . 8.2

8.

133

medias sounds; media.mp3,


.

. 8.2. Solution Explorer

,
; , .
, . 8.2 Visual Web Developer 2008 medias, sounds, media.mp3.
. , .
! , , . , , .
, Web Silverlight-.
.
,
, Web-;
(, !).
. , "" , , Web-,
Silverlight-.
, . 8.2 media.mp3
:
medias/sounds/media.mp3

- , ,
, Visual Web Developer 2008
, . ,
.
, ...

134

II. , , ,

?
. ,
Silverlight...
, Silverlight
- C#.
, . !

III

C#
9.

C#

10.

C#

11.

12.


C#
C# .
. .
C#.
( - XAML, -,
, ), .
.
C#. ,
.

, , ,

, C# . ,
. , ; , .
:
sgMillimetres = sgInches * 25.4;

sgInches , 25.4 sgMillimetres.


3.
sgInches sgMillimetres. -

138

III. C#

, -
, .
.
( , , ).
,
.
, 25.4
, . (,
.) .
"Silverlight", 435, true
.
*. C#, , , , ( , ,
10). * sgInches 25.4 ...
... sgMillimetres
=. , , , .
:
sgMetres = sgInches * 25.4 / 1000;

: *
/. .
1. sgInches 25.4.
2. 1000.
3. sgMetres.
, .
, . ( .)
:
x = x + 2;

9. C#

139

C C# , . x 2,
x. .
, , . ,
double
sgInches, (double).
double sgInches;

class , if . .
, C#.
 .
: , , , . . .
 (;).
, , .
,
sgMetres = sgInches * 25.4 / 1000;

:
sgMetres = sgInches * 25.4 /
1000;

:
sgMetres =
sgInches *
25.4 /
1000;

, , ...
C# .
, , .


C# . , :

140

III. C#

, , ,
- . ,
.

C#, Silverlight
, Silverlight , ,
C#. , : ,
, .
, .
"" : Silverlight, "" . ,
, .
Silverlight
C#. , string () C#
String Silverlight, double ( )
Double, bool Boolean.
Visual Web Developer 2008 , C#, , Silverlight. Silverlight.
? C#,
Silverlight. :
bool bFlag;
Boolean bFlag;

bFlag ,
C# bool,
Silverlight Boolean. , .

( ) , , , . , :
"Silverlight 3"
"1234567"
" ."

9. C#

141

string. Silverlight String.


string sValue;

( ), . , 2 ,
.
, .
, .
.
. Silverlight
Unicode. 65 535 . Silverlight , .
, ,
, .
, C#, . 9.1.
9.1. , C#

Unicode

\"

0x0022

\'

0x0027

\\

0x005c

\r

0x000d

\n

0x000a

\t

0x0009

\xFFFF

Unicode ( FFFF)

, ,
:
"\"Silverlight 3\" -"

"", .
sValue = "";

142

III. C#

Empty String,
.
sValue = String.Empty;

C# , . : 45,
2563446, -74376.
, C# .
. 9.2.
9.2. C#

C#

Silverlight

int

Int32

2 147 483 648 2 147 483 647

uint

UInt32

0 4 294 967 295

short

Int16

32 768 32 767

ushort

UInt16

0 65 535

long

Int64

9 223 372 036 854 775 808


9 223 372 036 854 775 807

ulong

UInt64

0 18 446 744 073 709 551 615

int ( ) uint ( ). ,
, short (
) ushort ( ). "" , , .
, ,
. 0
1 A F
C# 0x. :
0x5f8c.


C# , .

9. C#

143

, . : 25.4, 0.63465, -234.8.


<>E<>. ,
( ):
5
23
1E-5 (10 ), 8.546E23 (8,54610 ).
, C# .
. 9.3.
9.3. C#

C#

Silverlight

double

Double

510324 1,710308

float

Single

1,51045 3,41038

decimal

Decimal

11028 7,91028

double ( ), . .
Silverlight. , , , float ( ). decimal
( )
, .

,
"" "",
true false.
bool.
Silverlight Boolean.

-
Unicode ( . , ).
,

144

III. C#

. . : 't', '\\', '\u0058'.


char.
Silverlight Char.


Silverlight .
, .
. ,
. ,
x 4,
4.
x y,
y x.
, , - .
,
"".
Silverlight
10.


,

. ? , .



Silverlight. - , , , , ,
.
,
.
 int long, float, double decimal.
 uint long, ulong, float, double decimal.
 short int, long, float, double decimal.

9. C#

145

 ushort int, uint, long, ulong, float, double decimal.


 long float, double decimal.
 ulong float, double decimal.
 float double.
 char ushort, int, uint, long, ulong, float, double decimal.

, .
, ,
, Visual Web Developer 2008.
, , , .
, , ,
.


,
.

.
( C# Silverlight),
, .
double d = 1234.567;
int c;
= (int)d;

double int.
, . . , , .

, ,
. .

,
.
 int short, ushort, uint, ulong char.
 uint short, ushort, ulong char.
 short ushort, uint, ulong char.

146

III. C#

 ushort short char.


 long short, ushort, int, uint, ulong char.
 ulong short, ushort, int, uint, long char.
 double short, ushort, int, uint, long, ulong, char, float decimal.
 float short, ushort, int, uint, long, ulong, char decimal.
 decimal short, ushort, int, uint, long, ulong, char, float double.
 char short.

ToString(),
,
.
TryParse() .
10.

. , ,
, ,
- .


, (_), , . , sgInches _decodedValue
, 2number .
C# , .
, decodedvalue DecodedValue .
. -, "", , . , , , decodedValue . , ,
decodedFromStringToDoubleValue - .
-, ()
, , decodedValue, dbMillimetres. , Silverlight
() , -

9. C#

147

()
.
-, - , . , iCount ( int), sInches ( string), dcPrice (
decimal). , ,
.
, ,
- . , i, s, t2 .

.

, . , C#,
, Silverlight, , , . ;,
.
string sInches;
double sgInches;
int i;

sInches , sgInches
i.
.
- , =:
int i = 2;

-, .
Silverlight , ""?
-, C# Silverlight
. ,
. !

148

III. C#


. .

,
. ,
Silverlight.

-
, ,
. (
10.)

,

, . , , 6.
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
. . .
}

, ,
Silverlight , .
, , .
, sender
object e RoutedEventArgs.
, ,
. .

, . .

9. C#

149


. ,
C#, . 9.4.
9.4.

++

( )

--

( )

: .
;
, .
, .
:
++r;

r , . :
s = ++r;

s.
,
. , , . :
r = 3;
s = ++r 1;


r , (4)
r, () . s 3.

150

III. C#

,
, . :
r = 3;
s = r++ 1;

r , s 2.
r (4) .
.
,
. 9.4 ,
. :
x = x + t;
l = r * 3.14;
f = -e / 2;

, - .
, .


, , +
. , :
s1 = "Microsoft";
s2 = "Silverlight";
s3 = "3";
s = s1 + " " + s2 + " " + s3;

s "Microsoft Silverlight 3".


= . :
a = 2;

, C#
.
:

9. C#

151

a = a + b;
a += b;

. +=.
, C#, . 9.5.
9.5.

a += b;

a = a + b;

a -= b;

a = a b;

a *= b;

a = a * b;

a /= b;

a = a / b;

a %= b;

a = a % b;

" + ".



. , true,
false. :
a1 = 2 < 3;
a2 = -4 > 0;
a3 = r < t;

a1 true (2 3), a2 false (-4 ), a3


r t.
C# . 9.6.
9.6.

<

>

==

152

III. C#
9.6 ()

<=

>=

!=


.
. 9.7. . 9.8 9.9 .
9.7.

( )

&&

( )

||

( )

9.8.
1

&& ()

|| ()

true

true

true

true

true

false

false

true

false

true

false

true

false

false

false

false

9.9.

! ()

true

false

false

true

:
a = (b > 0) && (c + 1 != d);
flag = !(status = 0);

9. C#

153


?
. :
<> ? < true> : < false>

true, true.
false.
s = (i > 2) ? " " : " ";

, .


C# . :
a = b + c 10;

b
c, 10. , .
:
a = b + c * 10;

c 10,
b. "
" .
? , , , .
, , .
, .
. 9.10
.
9.10. ( )

<>++
<>--

154

III. C#
9.10 ()

++<>
--<>
- !
(<>)

, , ,

* / %

, ,

+ -

< > <= >= = !=

= <>=

&&

||

.
,
.

, : , .
( ).
, ? . .
a = (b + c) * 10;

b
c, 10.
, , .
:
a = ((b + c) * 10 d) / 2 + 9;

:
1. b c.
2. 10.
3. d .

9. C#

155

4. 2.
5. 9 .
:
a = b + c * 10 d / 2 + 9;

:
1. c 10.
2. d 2.
3. b c 10.
4. d 2.
5. 9 .
, ?


,
.
, .

C# ,
. ,
.
{
b = "12";
c = a b;
}

,
.
C#-
. 3.



-
. .

156

III. C#

:
if (<>)
< true>
else
< false>

if else.
" if-else".
,
Silverlight , .
. true, true. false,
false.

.
if (x
{
a =
b =
}
else
{
a =
b =
}

== 1)
"";
1;

" ";
22222;

x
f h .
if ((x == 1) && (y > 10))
{
f = 3;
}
else
{
f = 33;
}

, true
, x 1 y 10.
, "" , true, , .

9. C#

157

if (<>)
< true>

:
if ((x == 1) && (y > 10))
{
f = 3;
}


, . :
switch (< >) {
case < 1> :
< 1>
break;
[case < 2> :
< 2>
break;]
<... case>
[default :
<, >]
}

switch, case default


break, . " switch-case".

1, 2 . . , ,
( 1, 2 . .).
, ,
default (, , ).
:
switch (a) {
case 1 :
out = "";
break;
case 2 :
out = "";
break;
case 3 :
out = "";
break;

158

III. C#

default :
out = " ";
}

, a 1, out "", 2 "", 3 "".


a - , out
" ".
break, Silverlight
, , ,
. ,
. ,
1 1, break, 2.
break :
switch (a) {
case 1 :
out = "";
case 2 :
out = "";
case 3 :
out = "";
default :
out = " ";
}

, . out " ".

,
. .
C# . .


, -
. ,
.

9. C#

159

:
for (< >; <>; <>)
< >

for. " for".


.
, ,
( 1).
, , .

. , .
, . true, ,
, .
,
. ( ). , , . ., false.
:
for (int i = 1; i < 11; i++)
{
a += 3;
b = i * 2 + 1;
}

10 . i
1 . ,
11, .

. i
1 10, .
:
for (int i = 10; i > 0; i--)
{
a += 3;
b = i * 2 + 1;
}

160

III. C#

. 10.
10 , i 0;
10
1.
for (int i = 2; i < 21; i += 2)
{
b = i * 2 + 1;
}

2, 21,
, , 10 . , 2 2, 4, 6... 20.
"" for .
" " .


,
, , .
, ,
.
, .
:
do
< >
while (<>);

do while.
" do-while".
:
do {
a = a * i + 2;
i = ++i;
} while (a < 100);

:
var a = 0, i = 1;
do {
a = a * i + 2;

9. C#

161

i = ++i;
} while (i < 20);

, . , ,
.


,
. , () , .
while (<>)
< >

while.
" while" ( "
do-while"!).
:
while (a < 100) {
a = a * i + 2;
i = ++i;
}


. C# break continue.
break .
while (a < 100) {
a = a * i + 2;
if (a > 50)
{
break;
}
++i;
}

, a 50.
continue , . .
, ,

162

III. C#

: , . .
while (a < 100) {
i = ++i;
if (i > 9 && i < 11)
{
continue;
}
a = a * i + 2;
}

a i 10
20.



, . . .
goto.
, . :
< >:

. , .
goto :
goto < >;

, Silverlight
, .
if (x > 100)
{
goto TooBig;
}
. . .
TooBig:
. . .

, x 100,
TooBig.

9. C#

163

goto (. ).
:
goto < case, >;

:
switch (a) {
case 1 :
out = " ";
break;
case 2 :
goto case 1;
case 3 :
out = "";
break;
}

, a 2, case 1.

, . .
. .
, , .
, .
.
, , .

,
. .

:
< >[] < > =
new < >[< >];

164

III. C#

< >[] < > =


{< , >};

,
.

;
, .
Array.
7
new.
string[] sPlatforms = new string[4];

sPlatforms .
string[] sPlatforms = {"HTML+CSS+JavaScript", "Flash", "Java",
"Silverlight"};

,
.
,
.
s = sPlatforms[2];

s
sPlatforms 2 ( ) "Java".
sPlatforms[3] = "Silverlight 3";

sPlatforms 3 ( ) .
C# (
).
.
int[,] iMatrix = new int[4, 4];

iMatrix
44. ,
, ,
.

9. C#

165

double[, ,] dCube = new double[3, 3, 3];

(""). ,
. .
,
.
iMatrix[1, 2] = 4;
dValue = dCube[2, 0, 1];

, . :
< >[][] < > =
new < >[< >][];

:
int[][] iMatrix2 = new int[2][];
iMatrix2[0] = new int[2];
iMatrix2[1] = new int[4];
iMatrix2[2] = new int[5];

,
.
"" , "" , "" ,
.
iMatrix2[1][2] = 2;
iMatrix2[2][4] = 7;


, C#.
, , .
"" - .
:
foreach (< > < > in <>)
< >

, foreach in.
" foreach-in".

166

III. C#

, . .
.
, . .
.
, .
; .
string output = "";
foreach (string str in sPlatforms)
{
output += str + "\r\n";
}

sPlatforms
output, .
int s = 0;
foreach (int i in iMatrix)
{
s += i;
}

s iMatrix. : , !

, C#- - . C#, .
,
.
ActionScript
: // /*...*/. :
//
a = b + c; //

, ,
.

9. C#

167

/*...*/ :
/*


*/
a = b + c;

, , ( ), . .

?
C#.
, ?
C# :
. . C#: , . .
!

10

C#
C#. , .
, , . , , .
!

C#. ,
: . , , ...
.
4. ,
, ,
. Silverlight.


2. ,
.
, ,
, .
,
. , -, .

10. C#

169


.
.

, .
9 , .
- ,
. , , , .
,
.

, , ,
, , .
. .
, , , , . , , -
.

, -
. , .
, , .
 , . -

.
 Get-, .
 Set-, -

170

III. C#

, , ( ).
get- set- .
, get-
. , , ,
() .
get- ,
. , , ,
,
.
Set- , .
,
: ,
- .

: -
!
, IsEnabled
TextBox ( ), Silverlight , , , .
, get-, set. , , set-.
; get-
. , String ( ) Length,
;
get-.
get-.
. , .

, , Web-, .

10. C#

171

, . , , . . Silverlight.
,
, . .
, , ,
, , . ,
,
.
. , ,
Microsoft SDK for Visual Studio 2008.


9 ,
. , true
false, .
C# -
, . ,
,
.
, - . , , ,
.


, : , , . .
, .


, , , .
, , , - .
, , , , . , ,
.

172

III. C#

-
, , , ,
. ( , . . .)
: ,
.
... , . .

. .


Silverlight. .
2, .
-, -, : , , ( ), , .
, .
-
, , .
Silverlight .


:
; ,
. C# .


, 7, new. :
new < >([< ,
 >]);

10. C#

173

new .
;
, .
new (, ,
), .
,
(. . , , ).
SomeClass sc;
OtherClass oc;
sc = new SomeClass();
oc = new OtherClass(10, 4.5);


, . ( ,
. . 4.)
sc = new OuterNameSpace.InnerNameSpace.SomeClass();

, . . , "", , "" ,
.
sc = new OuterNameSpace.InnerNameSpace.OuterClass.InnerClass();


9 , Silverlight , ,
, . .
. .
Silverlight .
.
, .
, , , .
, ,
, .
, 9 . ,
, . .
Array.

174

III. C#



. (. . , ),
.
sc.SomeProperty = 20;
txtMillimetres.Text = "100";
sInches = txtInches.Text;

,
(, ).
if (txtInches.Text == String.Empty)

Empty String, , Text


txtInches.
, , .
,
this; Silverlight,
. .
this.SomeProperty = 40;
SomeProperty = 40;


SomeProperty , .
, , , .
, , (
; , ), , . ,
.
sc.SomeMethod(2, 56, "");
txtInches.Focus();

. ,
, .
m = sc.OtherMethod(22);
n = (sc.OtherMethod(44) + 3) / 0.5;

, -
, .

10. C#

175

, -, ( ), -,
out.
result1 = sc.ThirdMethod(65.2, out result2);

, ,
result1, ,
result2.
.
XAML- Visual Web Developer 2008;
, . C#-,
.
private void btnConvert_Click(object sender, RoutedEventArgs e)
{
}

, , - Visual Web Developer 2008.


. (
, , ,
), , ,
. +.
btnConvert.Click = btnConvert.Click + btnConvert_Click;

, .
:
btnConvert.Click += btnConvert_Click;

"" , ,

.
btnConvert.Click = btnConvert.Click btnConvert_Click;
btnConvert.Click -= btnConvert_Click;

, ,
(, ), ,
.
a = SomeClass.SOME_NAMED_CONSTANT;

, .

176

III. C#



9 C#,
. , , , .
is true, ,
, , , . , false.
if (txtInches is TextBox)
{
. . .
}

, . . txtInches
TextBox ( ).
if ("Silveright" is Double)
{
. . .
}

"Silverlight"
.
as
, , (), .
, ,
; null.
FrameworkElement felControl;
felControl = txtInches as FrameworkElement;

felControl txtInches,
FrameworkElement. ( FrameworkElement , .)
double d;
d ="Silverlight" as Double;

d null.
9
(< >)< >. ,
?

10. C#

177

, (< >)< >


. ( 12.) as,
, null. , ,
...

null
null. ?
, .
, .
FrameworkElement felControl;
felControl = txtInches as FrameworkElement;
if (felControl != null)
. . .

null (
). ,
.
felControl = null;


, , .
?
, . Silverlight
, - .
, - , . ,
, . ( , ), . Silverlight.

Silverlight

Silverlight, -

178

III. C#

.
Silverlight.

Object
Object Silverlight;
, , , .
Object Silverlight object C#.

String
String Silverlight . ,
,
Silverlight.
Chars , .
(char).
string str = "Silverlight";
char ch;
ch = str.Chars(2);

ch 'l'
"Silverlight". ( .)
Length .
IndexOf()
. :
IndexOf(< >
[, < , >])

(char) , . , ; ,
.
string str = "Silverlight";
int i1, i2;
i1 = str.IndexOf('l', 4);
i2 = str.IndexOf("ver");

i1 6 (
'l' "Silverlight"), i2 3.

10. C#

179

String IndexOf, . , , , . .

LastIndexOf() IndexOf() ,
, .
string str = "Silverlight";
int i;
i1 = str.LastIndexOf('l');

i 6.
Substring() ,
.
Substring(< >[, < >])

,
; , . .
string str = "Silverlight", s;
s = str.Substring(6, 3);

s "lig".
String Substring, . , .

ToLower() ToUpper() , .
, String . Silverlight.

Math
Math .
E PI
e .

180

III. C#

Math . 10.1.
10.1. Math

Abs(<>)

Acos(<>)

Asin(<>)

Atan(<>)

Ceiling(<>)

Cos(<>)

Exp(<>)

Floor(<>)

Log(<>)

Log10(<>)

Max(< 1>, < 2>)

Min(< 1>, < 2>)

Pow(<>, <>)

Round(<>)

Sign(<>)

1,
, 0, , 1,

Sin(<>)

Sqrt(<>)

Tan(<>)


Silverlight,
. .

10. C#

181


, 3,
, class. , , , .
[< , >] class < >
[: < - ,
 >]
{
< >
}

,
( . 9). ,
Silverlight , ; ,
SomeClass someclass Silverlight .
-.
, Object. , ;
- . (
.)

-. .

C#, . .
 public

, ( ). , ,
, .
 internal

( ). .
 private

, ( ). .
 sealed -

. Silverlight .

182

III. C#

 abstract , . .

- ( ). , "" .
 static (

).
 partial .

sealed
abstract (, , ).
(, , ).

, ,
Silverlight-.

,
, .
abstract class SomeClass

SomeClass,
, .
-, Object.
public class OtherClass: ParentClass, ISomeInterface

OtherClass,
ParentClass ISomeInterface.


:
[< , >] < > < >
[= < >];

C#, , ,
Silverlight.
, , ,
.
f ( . field ) .

10. C#

183

.
 public ,
, (
).
 protected -
( ).
 internal , ( ).
 protected internal , ( ).
 private ,
( ).
 static .
 readonly .
.
, . (
.)
, , ,
, .
( public)
. .
protected string fName;

fName.
static public int fCounter = 0;

( !) fCounter, 0.


:
[< , >]
< > < >
([< , >])
{
< >
}

184

III. C#

C#,
, , Silverlight.
,
void.

, .

. :
< > < ,
 >

. , .
, . .
-
, out.
.
 public ,

, (
).
 protected -

( ).
 internal , -

( ).
 protected internal -

, ( ).
 private ,

( ).
 static .
 overide ,

- ( ).
 virtual - (-

).
 abstract -

- ( ).

10. C#

185

 sealed -.

, .


return. :
return < >;

, , ,
this. ,
, , .


-. :
base.< >

base -.
Silverlight ,
, . -"" .
:
public int Increment(int iPar)
{
return iPar + 1;
}
. . .
i = sc.Increment(1);

, , . sc,
1, i 2.
public void Increment2(out int iPar)
{
iPar += 1;
}

186

III. C#

. . .
i = 1;
sc.Increment2(out i);

,

. i 1,
sc, ,
2.
protected virtual string SomeMethod()

-.
protected override string SomeMethod()
{
. . .
base.SomeMethod();
. . .
}

- .
-.


Silverlight . .
. . ,
.
.
 , .
 ( -

).
 .
public SomeClass()
{
this.SomeProperty = 10;
}

, SomeProperty
SomeClass 10. ,
this.

10. C#

187

public SomeClass(int iSP)


{
this.SomeProperty = iSP;
}

, .
( );
. , SomeClass ,
.

, . .

.
.
.
 , .
 .
 .
 .
static SomeClass()
{
SomeStaticProperty = 228;
}


SomeClass 228. ,
.
SomeStaticProperty


:
[< , >] < >
< >
{
[get
{
< get->
}]

188

III. C#

[set
{
< set->
}]
}

C#, , , Silverlight. , .
.
 public , , ( ).
 protected ( ).
 internal , ( ).
 protected internal -, (
).
 private , ( ).
 static .

Get- ,
. Set- .
set- value. Silverlight,
set-.
private int fSomeProperty;
public int SomeProperty
{
get
{
return this.fSomeProperty;
}
set
{
this.fSomeProperty = value;
}
}

10. C#

189

set-,
. get-,
.
C# . :
[< , >] < >
< > { get; set; }

, Visual Web Developer 2008


, get, , set-,
.
public int SomeProperty { get; set; }



const:
[< , >] const < >
< > = < >;

C#, ,
, Silverlight.
, .
() .
.
,
, .

.
 public , -

, ( ).
 protected -

( ).
 internal , -

( ).
 protected internal -

-, (
).

190

III. C#

 private , -

( ).
public const string PLATFORM_NAME = "Silverlight";
public const int PLATFORM_VERSION = 3;

: PLATFORM_NAME
PLATFORM_VERSION.

"" .
, , , ,
. , .
 , .


( ).
 , -

.
 -

.
, , , "" . , Silverlight ,
.


,
.
SomeStructure st;
st.SomeProperty1 = 2;
st.SomeProperty2 = "eee";
st.SomeMethod();

st SomeStructure,
SomeProperty1 SomeProperty2
SomeMethod. , , , .

10. C#

191

, new.
SomeStructure st;
st = new SomeStructure(2, "eee");

, .

Silverlight
Silverlight,
.
Silverlight.

Int16, Int32, Int64, UInt16, UInt32 UInt64


, . 9.2.
MaxValue MinValue , , , .
TryParse() .
TryParse(< >, out < >)

, , , .
TryParse() true, , false .
ToString() , .

Double Single
double float
(. . 9.3).
,
. 10.2.
IsInfinity(),
IsPositiveInfinity() true,
, Not A Number, + , false
.
IsNaN(), IsNegativeInfinity()

192

III. C#
10.2. Double Single

Epsilon

MaxValue

MinValue

NaN

Not A Number ""

NegativeInfinity

PositiveInfinity

TryParse()
.
TryParse(< >,
out < >)

, , , .
TryParse() true, , false .
ToString() , .

Decimal
decimal
(. . 9.3).
, Silverlight.
.
MaxValue MinValue , , , .
TryParse() .
TryParse(< >,
out < >)

, , , .

10. C#

193

TryParse() true, , false .

ToString() , .

DateTime
DateTime .

new DateTime(<>, < >, <>
[, <>, <>, <>[, <>]])

, . .
DateTime dt;
dt = new DateTime(2009, 10, 27, 23, 0, 0);

dt ,
27 2009 23:00:00.
DateTime ,
. 10.3.
, .
10.3. DateTime

Date

DateTime,

Day

DayOfWeek

, DayOfWeek . DayOfWeek Sunday (), Monday (), Tuesday (),


Wednesday (), Thursday (), Friday () Saturday
()

DayOfYear

Hour

Millisecond

Minute

Month

Now

DateTime,
.

194

III. C#
10.3 ()

Second

TimeOfDay

TimeSpan, ( TimeSpan )

Today

DateTime, .

Year

DateTime ,
(. 10.4).
10.4. DateTime

Add(<>)


, TimeSpan

AddDays(<>)

AddHours(<>)

AddMilliseconds(<>)

AddMinutes(<>)

AddMonths(<>)

AddSeconds(<>)

AddYears(<>)

10. C#

195

DateTime dt;
dt = DateTime.Now;
dt.AddYears(2);
dt.AddMonth(6);

dt . , C#.

TimeSpan
TimeSpan
.

new TimeSpan([<>, ]<>, <>, <>[, <>])

, . .
TimeSpan ts;
ts = new TimeSpan(2, 20, 0);

ts 2 20 .
TimeSpan ,
. 10.5. , .
10.5. TimeSpan

Days

Hours

Milliseconds

Minutes

Seconds

TotalDays

TotalHours

TotalMilliseconds

TotalMinutes

TotalSeconds

196

III. C#

TimeSpan , Silverlight.


, .
struct.
[< , >] struct < >
[: < , >]
{
< >
}
,
, .

public struct SomeStructure


{
public short SomeProperty1 { get; set; }
public string SomeProperty2 [ get; set; }
public void SomeMethod()
{
. . .
}
}

.
- , -.
, , ,
, . , .
,
, . , ; get- set- .
, . () ,
, .
() .

10. C#

197

, . , struct.
[< , >]
interface < >
[: < -, >]
{
< >
}

,
. I ( .
interface). .

.
public interface ISomeInterface
{
double SomeInterfaceMethod(double db);
int SomeProperty { get; set; }
}

ISomeInterface SomeInterfaceMethod,

, SomeProperty . , .
class SomeClass: ISomeInterface
{
public double SomeInterfaceMethod(double db)
{
. . .
}
public int SomeProperty
{
get
{
. . .
}
set
{
. . .
}
}

198

III. C#

. . .
}
class OtherClass: ISomeInterface
{
public double SomeInterfaceMethod(double db)
{
. . .
}
public int SomeProperty
{
get
{
. . .
}
set
{
. . .
}
}
. . .
}

SomeClass OtherClass, . .
SomeClass sc = new SomeClass();
OtherClass oc = new OtherClass();
double d11 = sc.SomeInterfaceMethod(4.2);
double d12 = oc.SomeInterfaceMethod(0.74);

... -.
ISomeInterface isi1 = new SomeClass();
ISomeInterface isi2 = new OtherClass();
double dl1 = isi1.SomeInterfaceMethod(4.2);
double d12 = isi2.SomeInterfaceMethod(0.74);

. ,
, . , ,
, . .
. .
Silverlight , .

10. C#

199

, .
Silverlight.

, .
,
();
.
enum.
:
[< , >]
enum < >
{< , >};
,
, .

:
public enum Platforms {HTMLCSSJavaScript, Flash, Java, Silveright};

,
: ,
.
Platforms pl;
pl = Platforms.Flash;
. . .
if (plPlatformName == Platforms.Silverlight)
. . .

, - .
, .

?
! Silverlight!.. , , , , , , , ... ?..
. . -
. -, !
, ...

11

: , , .
Silverlight . Silverlight, ,
. ( Silverlight,
.)
. .


3,
Silverlight-. ,
( );
, ( , ),
, ""
.
9 .
.
 , ,

. , ; .

 , .

11.

201

, .

Silverlight- , ,
, . , ( 13) , ,
.


, .
, Silverlight .
, , - , , , (), , (
).
.

"" . !
? ,
. .
Silverlight , , , .
( , , ), .

List
List. , "" . .

List
, List,
. :
List<< >> < >;

202

III. C#

,
< >.
List, .
new. :
new List<< >>()

.
List<string> lstCol;
lstCol = new List<string>();

lstCol, ( String).


List Count, . .



,
. (, ,
).
Add . .
lstCol.Add("HTML + CSS + JavaScript");
lstCol.Add("Flash");
lstCol.Add("Silverlight");


-.
Insert .
:
Insert(< >, < >)

(),
. , -

11.

203

. . Insert .
lstCol.Insert(2, "Java");

Java!
Remove , . true, ,
false (,
).
lstCol.Remove("Flash");

Silverlight.
RemoveAt ,
. .
lstCol.RemoveAt(0);

...
Clear , .
.
lstCol.Clear();


, Item.
.
string st;
st = lstCol.Item[3];

st
"Silverlight".
Item List . , .
string st;
st = lstCol[3];

Item ,
.
lstCol[3] = "Silverlight 3";

lstCol
"Silverlight" "Silverlight 3".

204

III. C#


?
List .
Contains , ,
. true,
, false .
bool flag1, flag2;
flag1 = lstCol.Contains("Flash");
flag2 = lstCol.Contains("Delphi");

flag1 true
( lstCol "Flash"), flag2
false ( "Delphi" ).
IndexOf .
:
IndexOf(< >[, < >
[, < >]])

.
.
, .
, .
,
, ,
. , .
int indx;
indx = lstCol.IndexOf("Java");

indx 2.
LastIndexOf IndexOf ,
.
.
,
9 .
string output = "";
foreach (string str in lstCol)
{
output += str + "\r\n";
}

11.

205


Silverlight .
List
.
,
-. , , , .

Person. F (), N1 ()
N2 () Platforms List<string> ( ;
, List).
class Person
{
public string F { get; set; }
public string N1 { get; set; }
public string N2 { get; set; }
public List<string> Platforms { get; set; }
public Person()
{
Platforms = new List<string>();
}
}

List<string>
Platforms .
.
List<Person> lstPersons;
lstPersons = new List<Person>();

.
Person prs;
prs = new Person();
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.Platforms.Add("Flash");
lstPersons.Add(prs);
prs = new Person();
prs.F = "";

206

III. C#

prs.N1 = "";
prs.N2 = "";
prs.Platforms.Add("HTML + CSS + JavaScript");
prs.Platforms.Add("Flash");
prs.Platforms.Add("Java");
lstPersons.Add(prs);
prs = new Person();
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.Platforms.Add("Silverlight");
lstPersons.Add(prs);

: Person,
prs, lstPersons.
,
"" Platforms Person,
, , , , .
, Person
prs .
, .
prs = lstPersons[2];
bool flag = prs.Platforms.Contains("Java");

,
Java.
prs = lstPersons[0];
prs.F = "";

.
prs = lstPersons[1];
prs.Platforms.Add("Silverlight");

Silverlight.
, List . , . ,
, .
!

11.

207

Dictionary
Dictionary.
,
. , List, . ,
Dictionary (, ).
, ,
Silverlight .

Dictionary
Dictionary, , :
. :
Dictionary<< >, < >>
< >;

, .
new
:
new Dictionary<< >, < >>()
Dictionary<string, string> dicDict;
dicDict = new Dictionary<string, string>();

dicDict, .


Dictionary Count, .
.


Add :
Add(<>, <>)

208

III. C#

,
. .
dctDict.Add("hcjs", "HTML + CSS + JavaScript");
dctDict.Add("fl", "Flash");
dctDict.Add("j", "Java");
dctDict.Add("sl", "Silverlight");

Remove , . true,
, false (, ).
dctDict.Remove("fl");

Clear , . .


, Item.
.
string st;
st = dctDict.Item["sl"];

Item Dictionary . .
string st;
st = dctDict["sl"];

,
.
dctDict["sl"] = "Silverlight 3";

TryGetValue .
TryGetValue(< >, out <, >)

, , , , , . TryGetValue true,
, false .
string st;
if (dctDict.TryGetValue("fl", out st))
. . .

11.

209


ContainsKey ,
, . true, , false .
bool flag1, flag2;
flag1 = dctDict.ContainsKey("fl");
flag2 = dctDict.ContainsKey("d");

ContainsValue , , .
true, , false .
bool flag1, flag2;
flag1 = dctDict.ContainsValue("Flash");
flag2 = dctDict.ContainsValue("Delphi");


.
"-",
.
KeyValuePair,
.
foreach (KeyValuePair<string, string> kvp in dctDict)

, ,
.
KeyValuePair :
 Key ;
 Value .
string outputKeys, outputValues;
foreach (KeyValuePair<string, string> kvp in dctDict)
{
outputKeys += kvp.Keys + "\r\n";
outputValues += kvp.Values + "\r\n";
}


, .

210

III. C#

Queue
Queue , . " , ". ,
, ,
, .
, List.
Queue<string> queQ;
queQ = new Queue<string>();

Enqueue ,
. .
queQ.Enqueue("HTML + CSS + JavaScript");
queQ.Enqueue("Flash");
queQ.Enqueue("Java");
queQ.Enqueue("Silverlight");

Dequeue , (,
), ;
. .
string st1;
st = queQ.Dequeue();

st1 "HTML + CSS


+ JavaScript" queQ.
, "Flash".
Peek , .
.
string st2;
st = queQ.Peek();

st2 "Flash" queQ.


.
Queue Count Clear Contains.

Stack
Stack , -
. , " , ".

11.

211

.
Stack<string> stkStack;
stkStack = new Stack<string>();

Push ("") , . .
stkStack.Push("HTML + CSS + JavaScript");
stkStack.Push("Flash");
stkStack.Push("Java");
stkStack.Push("Silverlight");

Pop ("") ,
, ;
. .
string st1 = stkStack.Pop();
string st2 = stkStack.Pop();

st1 st2 "Silverlight" "Java" . stkStack .


Peek , .
.
string st;
st = stkStack.Peek();

st "Flash" stkStack
. .
Stack Count Clear Contains.

,

Silverlight,
.
, .
"" List.
, .
, , ListBox () Items.
6 , ItemCollection, FrameworkElement.
ListBoxItem, .

212

III. C#

lstList .
lstList.Item.Clear();
lstList.Item.Add(" ");
lstList.Item.Add(" ");

,
. , ,
.
! , ,
, Children.
UIElementCollection UIElement,
FrameworkElement
.
, , . .
Children
. , XAML- (,
, ).
,
, Silverlight.
.
, .
13,
.

?
C# . , Silverlight-.
.
, Silverlight
(,
). , ,
C#.

12

C#
Silverlight, . . "" , , , , ,
.
. ,
, . .


Silverlight-
.
, .
. -
, . , - "" ( ,
).
, .
?
, Silverlight ,
. , , . , - .

214

III. C#

, , , .
, Silverlight, , , , .
, . Silverlight- .


? , . . -
. ( " ",
.) ,
, , .

Silverlight. , , .
,
.
, ( ),
. .
, "" , , -
.
. , , . "" ,
, . , , .
, . : , ,
.
: ,
. , . , ,
.

12.

215

, ,
Silverlight, .
Silverlight "", .


Silverlight ,
. ( , ,
Silverlight.)
. 12.1.
12.1. Silverlight

Exception
SystemException

Exception

-
,

ArrayTypeMismatchException

SystemException

IndexOutOfRangeException

SystemException

InvalidCastException

SystemException

InvalidOperationException

SystemException

NullReferenceException

SystemException

OutOfMemoryException

SystemException

StackOverflowException

SystemException

ArgumentException

SystemException

-
,

216

III. C#
12.1 ()

ArgumentNullException

ArgumentException


null

ArgumentOutOfRangeException

ArgumentException

ArithmeticException

SystemException

-
,

DivideByZeroException

ArithmeticException

NotFiniteNumberException

ArithmeticException

,


(, + NaN)

OverflowException

ArithmeticException



,
(, int
short)

Message, . , .


Silverlight.


, . :
, .


,
.

12.

217

try
< , >
catch (< > < >)
< , >

, try catch. " try-catch".


, ( try), , , catch. , , ,
, . , ,
.
catch,
, , .
try
{
int
int
}
catch
{
. .
}

x = 1, y = 0;
z = x / y;
(DivideByZeroException e)
.

DivideByZeroException
.
catch .
try
{
. .
}
catch
{
. .
}
catch
{
. .
}

.
(DivideByZeroException e)
.
(ArithmeticException e)
.

218

III. C#

catch (SystemException e)
{
. . .
}

catch,
DivideByZeroException. , . , catch .
catch, ( ArithmeticException). , DivideByZeroException, catch .
catch, ,
(
SystemException). ,
DivideByZeroException, ArithmeticException,
catch .
, catch,
( DivideByZeroException),

(ArithmeticException SystemException).
"", "" "" .


,
, ,
try-catch .
< try-catch>
finally
< , >

- , finally, " finally".


, , , , try-catch .
try
{
btnDoCompute.IsEnabled = false;
. . .
}

12.

219

catch (ArithmeticException e)
{
. . .
}
finally
{
btnDoCompute.IsEnabled = true;
}

finally , , .


,
Silverlight ,
. ,
, .
, , .
ArgumentOutOfRangeException e = new ArgumentOutOfRangeException
(" .");

ArgumentOutOfRangeException. , ,
.
, . , , .

. throw,
.
throw e;

.
. ,
Silverlight.

220

III. C#

?
Silverlight,
, . C#.
Silverlight.
, .
, ...

IV


.
LINQ
13.

14.

LINQ

13



C# ,
. C#-. .

. ?
? .


, - ? , C#.
txtMillimetres.Text = cnvConvertor.Millimetres;

Text txtMillimetres
Millimetres cnvConvertor. (, .)
, C# .
, ? , ,
txtMillimetres ,
Millimetres cnvConvertor, C#-?
XAML-,
Text txtMillimetres
Millimetres cnvConvertor, Millimetres. Silverlight
.

224

IV. . LINQ

, Text
txtMillimetres Millimetres cnvConvertor.
, , .
, . ,
- HorizontalAlignment,
IsChecked, SelectedIndex . Silverlight .
, ,
.
, .


.
Convertor, ,
.
Silverlight-.
Visual Web Developer 2008
Convertor3.
, txtInches txtMillimetres btnConvert.
, Convertor.
Click btnConvert, . . .
XAML-
C#-. Convertor3 Convertor.
MainPage.
public class Convertor
{
private double fInches;
public string Inches
{
get
{
return fInches.ToString();
}
set
{
double dInches = 0;
double.TryParse(value, out dInches);

13.

225

if (dInches != fInches)
{
fInches = dInches;
}
}
}
public string Millimetres
{
get
{
double dMillimetres = fInches * 25.4;
return dMillimetres.ToString();
}
}
}

fInches
. .
Inches.
, , get- set. Get- ,
. Set-
fInches.
,
, ;
, .
Converter
Millimetres. , . .
get-. fInches,
, .
Converter ; ,
Object, (
-). Converter , .
, Converter . ,
.
C#-. XAML-.
.

226

IV. . LINQ

Silverlight-
.

,
Convertor. Load
, ,
InitializeComponent.
, . new.
. Silverlight, , , .
Convertor,
Convertor3. ( 4, Visual Web Developer 2008
,
.) Convertor3 XAML- , ,
<UserControl>, ,
xmlns.
. <UserControl> xmlns, , ,
. local (, Visual Basic, my). . Visual Web Developer 2008
, ;
Convertor3 in assembly Convertor3
Convertor3 , . . . :
xmlns:local="clr-namespace:Convertor3"

, Visual Web Developer 2008 xmlns , ,


. , , Build < > Build.

. .
Silverlight ,
-

13.

227

( , .), . , , .

,
, 8 . !

UserControl, , Resources, ResourceDictionary ( 11). ,


Object ,
. , . . .
, ,
.
<UserControl> , Resources. , , . , , , , .
x:Key, , .

, -, . Silverlight .

! XAML-
<UserControl>,
Convertor:
<UserControl.Resources>
<local:Convertor x:Key="cnvConvertor"></local:Convertor>
</UserControl.Resources>

, ,
Convertor local. cnvConvertor
x:Key .
. .
, Silverlight . , ,
App.xaml, ,
<Application>. ,

228

IV. . LINQ

, , , .


, .
 -, Text txtInches -

Inches Convertor,
. Text
Inches.
 -, Text txtMillimetres

Millimetres Convertor. Millimetres Text.


txtMillimetres, . . .
Text .
Binding. ( ),
XAML.
< >="{Binding < >}"

, (
Binding) , , .
, , Binding, ( ). <
>=< > .
, .
Binding, , .
 Path ,

. , .
 Source , .

Source . ,
, , . ?

13.

229

.
Source :
{StaticResource < Resources>}

, Text txtMillimetres
Millimetres - cnvConvertor, <TextBox> :
Text="{Binding Path=Millimetres, Source={StaticResource cnvConvertor}}"

txtMillimetres
Millimetres - cnvConvertor
Text.
txtInches. : , Text Inches - cnvConvertor.
, Mode Binding.
BindingMode :
 OneWay -

( ; );
 TwoWay -

,
( );
 OneTime

( ).
, <TextBox>,
txtInches:
Text="{Binding Path=Inches, Source={StaticResource cnvConvertor},
Mode=TwoWay}

, , , ... "" . ,
Inches Convertor ,
txtInches .
! .
Text TextBox,

230

IV. . LINQ

, . ,
, , .
. - . ... ... . ?


, ,
, , , . , .
.
Convertor INotifyPropertyChanged,
PropertyChanged.
Millimetres; , .
, , , , , INotifyPropertyChanged.
, , . PropertyChanged
, .
INotifyPropertyChanged PropertyChanged
System.ComponentModel. , , . ( . 4.)
Convertor INotifyPropertyChanged .
; .
public class Convertor : System.ComponentModel.INotifyPropertyChanged
{
private double fInches;
public event System.ComponentModel.PropertyChangedEventHandler
PropertyChanged;
public string Inches
{
get
{
return fInches.ToString();
}

13.

231

set
{
double dInches = 0;
double.TryParse(value, out dInches);
if (dInches != fInches)
{
fInches = dInches;
NotifyPropertyChanged("Millimetres");
}
}
}
. . .
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}

PropertyChanged

PropertyChangedEventHandler . ( ,
event.)
Silverlight,
INotifyPropertyChanged.
NotifyPropertyChanged, PropertyChanged.
, ,
.
,
PropertyChanged . (
, .) . PropertyChanged
null, , , .
, .
: ,
( this), , .
, ;
NotifyPropertyChanged .

232

IV. . LINQ

NotifyPropertyChanged get-
Inches.
Millimetres, . . txtMillimetres.
, fInches
PropertyChanged ,
, Inches, ? "" , .
,
.
, .


, ( Mode TwoWay), Silverlight .
,
, Silverlight
.
Binding ValidatesOnExceptions. true, Silverlight , . false.
,
. Text
txtInches ValidatesOnExceptions true.
Text="{Binding Path=Inches, Source={StaticResource cnvConvertor},
Mode=TwoWay, ValidatesOnExceptions=True}"

set- Inches Convertor ( ):


set
{
double dInches = 0;
double.TryParse(value, out dInches);
if (dInches < 0)
{
throw new Exception(" ");
}

13.

233

if (dInches != fInches)
{
fInches = dInches;
NotifyPropertyChanged("Millimetres");
}
}

, , .
, ,
.
,
; , " ".

Convertor
Silverlight . , ,
.
INotifyPropertyChanged...

Visual Web Developer 2008


Convertor4. , ,
txtInches txtMillimetres (
). , Convertor3, .
Text txtMillimetres txtInches.
, .
, ,
Path Binding.
ElementName.
, txtInches
<TextBox> :
Text="{Binding Path=Text, ElementName=txtInches}"

, -
.
. , , .

234

IV. . LINQ


. , -
.
Silverlight .
,
IValueConverter. :
 Convert , -

( );
 ConvertBack ,

( ).
IValueConvertr

System.Windows.Data. ,

, .
:
 Object;
 , .
Type

, ;

 Object;
 ,

. CultureInfo , . , CultureInfo
System.Globalization,
.

. .
Object.
,
IValueConverter. Convert;
ConvertBack .
Convertor.
Convertor4.
public class Convertor : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)

13.

235

{
string sInches;
double dInches, dMillimetres;
sInches = value as string;
if (double.TryParse(sInches, out dInches))
{
dMillimetres = dInches * 25.4;
}
else
{
dMillimetres = 0;
}
return dMillimetres.ToString();
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

.
ConvertBack NotEmplementedException, , ( ).
, . XAML-
Convertor4 ( , ),
Convertor,
cnvConvertor ( ). Converter Binding.
Text="{Binding Path=Text, ElementName=txtInches,
Converter={StaticResource cnvConvertor}}"

, , ,
,
, .
! , .

236

IV. . LINQ


,
, C#-. Silverlight .
, .


Visual Web Developer 2008 ListDemo.
lstDemo. - .
, . ,
. lstDemo.
? 11 .
, "" ,
- . ,
INotifyCollectionChanged.
, ,
Silverlight ObservableCollection.
, ,
, .
System.
,
, .
ObservableCollection
Collections.ObjectModel.

Load
, -, . : ObservableCollection ListDemoItemCollection,

. ListDemo .
public class ListDemoItemCollection :
System.Collections.ObjectModel.ObservableCollection<string>
{
public ListDemoItemCollection()

13.

237

{
Add(" ");
Add(" ");
Add(" ");
Add(" ");
}
}

ObservableCollection ,
.
ListDemoItemCollection, XAML-.
XAML- ListDemo ,
colItems.
ItemsSource, .
colItems, .
<ListBox . . . ItemsSource="{StaticResource colItems}" . . .>

. , , ...


... .
.
- , .
11 Person, -. .
public class Person
{
public string F { get; set; }
public string N1 { get; set; }
public string N2 { get; set; }
public int Age { get; set; }
public List<string> Platforms { get; set; }
public Person()
{
Platforms = new List<string>();
}
}

238

IV. . LINQ

, Age, . ListDemo.
ListDemoItemCollection ,
Person.
public class ListDemoItemCollection :
System.Collections.ObjectModel.ObservableCollection<Person>
{
public ListDemoItemCollection()
{
Person prs;
prs = new Person();
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.Age = 30;
prs.Platforms.Add("Flash");
Add(prs);
prs = new Person();
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.Age = 25;
prs.Platforms.Add("HTML + CSS + JavaScript");
prs.Platforms.Add("Flash");
prs.Platforms.Add("Java");
Add(prs);
prs = new Person();
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.Age = 38;
prs.Platforms.Add("Silverlight");
Add(prs);
}
}

lstDemo, Person
. DisplayMemberPath,
. .
<ListBox . . . ItemsSource="{StaticResource colItems}"
DisplayMemberPath="F" . . .>

13.

239

lstDemo,
F Person.
.
.


Person? . , .
, ,
. , . , ,
, , , . ,
, .
ItemTemplate,
.
DataTemplate , .
XAML-,
. , , , ItemTemplate, ,
DataTemplate, ,
.
<ListBox x:Name="lstDemo" ItemsSource="{StaticResource colItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel>
<TextBlock Text="{Binding Path=F}"></TextBlock>
<TextBlock Text="{Binding Path=N1}"></TextBlock>
<TextBlock Text="{Binding Path=N2}"></TextBlock>
</StackPanel>
<TextBlock Text="{Binding Path=Age}" FontSize="26"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

240

IV. . LINQ

"" . , -,
"", ,
, F, N1 N2. -,
, Age .
. :
, (. .
). . :
, , DisplayMemberPath,
, .
, , .
! !
,
SelectedItem. ,
, Object.
.


- "" Platforms Person. ,
, ?

lstDetail. . "" ,
Platforms , lstDemo. , SelectedItem ( !) Person.
C#-? DataContext, .
Object , .
, .
SelectionChanged lstDemo; , 6,
, .
.
private void lstDemo_SelectionChanged(object sender,
SelectionChangedEventArgs e)

13.

241

{
lstDetail.DataContext = (lstDemo.SelectedItem as Person).Platforms;
}

,
Person "" Platforms
DataContext lstDetail. ,
.
XAML- lstDetail
<ListBox> :
ItemsSource="{Binding}"

, ,
. , DataContext.
! .

DataGrid

.

DataGrid ( ""!).
DataGrid ( ) , , .
System.Windows.Controls,
System.Windows.Controls.Data.dll.
<data:DataGrid></data:DataGrid>

Visual Web Developer 2008 data. .

, , .
DataGrid ,
. .

, DataGrid ActualHeight,
ActualWidth, DataContext, FontFamily, FontSize, FontStretch, FontStyle,
FontWeight, Height, HorizontalAlignment, IsEnabled, IsTabStop, ItemsSource,

242

IV. . LINQ

Margin, MaxHeight, MaxWidth, MinHeight, MinWidth, Name, Padding, SelectedIndex,


SelectedItem, TabIndex, VerticalAlignment, Visibility Width, Focus
SelectionChanged.

IsReadOnly , true. false, , , ( ).


HorizontalScrollBarVisibility VerticalScrollBarVisibility , , ,
.
ScrollBarVisibility :
 Auto ;
 Hidden , -

-;
 Visible ;
 Disabled , -

-.
Auto.
HeadersVisibility ,
"" . DataGridHeadersVisibility;
Columns ("" ; ) None ("" ).
GridLinesVisibility ,
, .
DataGridGridLinesVisibility :
 None ;
 Horizontal , ;
 Vertical , ;
 All ( ).

, .
 CanUserReorderColumns

.
 CanUserResizeColumns -

, .

13.

243

 CanUserSortColumns

- , .
true , false . true.
AutoGenerateColumns . true , , , . false
, ,
Columns.
Columns , .
ObservableCollection<DataGridColumn>, DataGridColumn ,
.
Silverlight .
 DataGridTextColumn . -

.
 DataGridCheckBoxColumn .

.
 DataGridTemplateColumn , -

( ).
, - .
, . (
.)
, ActualWidth,
IsReadOnly, MaxWidth, MinWidth, Visibility Width.
Header .
DisplayIndex
.
.
 CanUserReorder

.
 CanUserResize -

, .
 CanUserSort -

, .

244

IV. . LINQ

true , false .
.
DataGridTextColumn DataGridCheckBoxColumn
Binding, .
.
DataGridTextColumn
FontStyle FontWeight.

FontFamily,

FontSize,

DataGridCheckBoxColumn IsThreeState. true , , , , , , false (


).
DataGridTemplateColumn
CellEditingTemplate.

CellTemplate
DataTemplate.
, ,
( ; ).
. Visual
Web Developer 2008 GridDemo. C#- Person
ListDemoItemCollection, ListDemo. XAML-
GridDemo
ListDemoItemCollection,
colItems.
grdDemo. ItemsSource,
colItems. - , , . .
!
,
Person. Platforms,
ObservableCollection, . (
DataGrid ,
Platforms . .) .
, .
: , , , , . ,
DataGridTextColumn.

13.

245

, AutoGenerateColumns false, . <data:DataGrid>,


, , :
<data:DataGrid.Columns>
<data:DataGridTextColumn Binding="{Binding Path=F}"
Header=""></data:DataGridTextColumn>
<data:DataGridTextColumn Binding="{Binding Path=N1}"
Header=""></data:DataGridTextColumn>
<data:DataGridTextColumn Binding="{Binding Path=N2}"
Header=""></data:DataGridTextColumn>
<data:DataGridTextColumn Binding="{Binding Path=Age}"
Header=""></data:DataGridTextColumn>
<data:DataGridTextColumn Binding="{Binding Path=Platforms.Count}"
Header="- "></data:DataGridTextColumn>
</data:DataGrid.Columns>

.
, Count ""
Platforms Person. -, " "!
. !
. <data:DataGrid> , ,
. ,
DataGridTemplateColumn, ,
.
<data:DataGridTemplateColumn Header="">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=F}"></TextBlock>
<TextBlock Text="{Binding Path=N1}"></TextBlock>
<TextBlock Text="{Binding Path=N2}"></TextBlock>
</StackPanel>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>

"" , , , ,
F, N1 N2.

246

IV. . LINQ

, , . ,
" ".


DataGrid
,
, DataGrid? !
DataGrid
, :
 , ;
 DataGridTemplateColumn;
 -

IsReadOnly true.
, , ,
true. .

IsReadOnly

, , - C#-,
, ,
INotifyPropertyChanged. ,
. , .

! - , ,
<Enter> . ,
<Esc>. <Esc> , .
, . . ,

, .

, .
. .

13.

247


DataGrid
,
. , .
, , DataGridTemplateColumn,
. , .
: ,
. , ,
(. .
DataGridTemplateColumn). ,
.
CellEditingTemplate
DataGridTemplateColumn. CellTemplate
.
1. ,
( , , , .).
2. , ,
Mode TwoWay.
<data:DataGridTemplateColumn>,
, :
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=F, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding Path=N1, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding Path=N2, Mode=TwoWay}"></TextBox>
</StackPanel>
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>

. .
. .
, .
Silverlight.

248

IV. . LINQ

?

Silverlight .
, ,
, ,
, .
, . , . ,
LINQ, .

14

LINQ
Silverlight .
.
. Silverlight . LINQ, . , , ...

LINQ
, , -. , , , Flash.
?
.
, , .
, "" Platforms
"Flash". , "" , . , , Flash.
, C#-,
, . , . - !
" "...

250

IV. . LINQ

Silverlight ! .
,
, ,
.
, LINQ (LanguageINtegrated Query , ), C#-
MSIL. C#
, .
LINQ, , . XAML, , , ,
. Silverlight:
" " , .

LINQ SQL
(Structured Query Language ). , SQL, LINQ.


, , ,
LINQ. .
Visual Web Developer 2008 LINQDemo.

lstDemo
LINQ-. ,
.
<ListBox x:Name="lstDemo" ItemsSource="{Binding}"></ListBox>

, , DataContext. ( . 13.)

C#- Person
ListDemoItemCollection, ListDemo.
ListDemoItemCollection LINQCollection, -

14. LINQ

251

, , .

, ,

LINQ-,

ObservableCollection -.
List.

, , , InitializeComponent. Load
.
, LINQ. ?
 , -

, . .

 .
 ,

.
 , ( ,
- ).
, LINQCollection colPersons:
LINQCollection colPersons = new LINQCollection();

, colPersons
:
IEnumerable<string> qryPersons = from person in colPersons
select person.F;

qryPersons.
, .
? , ,
,
.
, ,
.
IEnumerable, -

252

IV. . LINQ

. , .
.
IEnumerable ,
. (string),
colPersons ,
.
, , . (
) , .
 colPersons -

person (from person in colPersons).


.
 F ,
person (select person.F).
, . lstDemo .
lstDemo.DataContext = qryPersons;

.
. !
.
IEnumerable<string> qryPersons = from person in colPersons
select person.F + " " + person.N1 + " " + person.N2;

, , , .
.
IEnumerable<Person> qryPersons = from person in colPersons select person;

, ,
. , 13. ,
.
, , :
from < > in <>
select < ,
 >

14. LINQ

253

from, in select.
from select.

.

, colPersons? ?
. select
. .
var qryPersons = from person in colPersons
select new { F = person.F, N1 = person.N1, N2 = person.N2 };

colPersons select
, , , . ,
: new,
,
(F, N1 N2). ,
, .
, , ...
? !
Silverlight.
Visual Web Developer 2008 , . .
 .
 .
 , -

.
,
.
 ,

Object.
.
, ,

254

IV. . LINQ

var. ,
Visual Web Developer 2008 ( ).
,
, . ,
.
.
, lstDemo DisplayMemberPath
, , . ListBox
!
: , , ObservableCollection, - , , . .
System.Collections.ObjectModel.ObservableCollection<string> colTemp =
new System.Collections.ObjectModel.ObservableCollection<string>();
foreach (var prs in qryPersons)
{
colTemp.Add(prs.F + " " + prs.N1 + " " + prs.N2);
}
lstDemo.DataContext = colTemp;

, . , var, . . .

, , .
...


, . ,
, .
, , , .

14. LINQ

255

where, .
where < >

,
3. true, . ,
.

where from select.


IEnumerable<string> qryPersons = from person in colPersons
where person.Age > 30
select person.F + " " + person.N1 + " " + person.N2;

, ,
30 .
IEnumerable<string> qryPersons = from person in colPersons
where person.Age < 30 && person.Platforms.Contains("Flash")
select person.F + " " + person.N1 + " " + person.N2;

, ,
30 Flash. && Contains. (
. 11.)


.
orderby ,
.
orderby < ,
 ,
 >

, (). , . .

. ,

256

IV. . LINQ

() descending, .
orderby from select. where, orderby
select.
IEnumerable<string> qryPersons = from person in colPersons
where person.Age <= 30
orderby person.F
select person.F + " " + person.N1 + " " + person.N2;

, ,
30 , .
IEnumerable<string> qryPersons = from person in colPersons
orderby person.Platforms.Count() descending, person.F
select person.F + " " + person.N1 + " " + person.N2;

,
, ,
, . Count.


. - , , .
Dept.
: ID ( ) Name
( ).
public class Dept
{
public short ID { get; set; }
public string Name { get; set; }
}

DeptCollection, .
public class DeptCollection :
System.Collections.ObjectModel.ObservableCollection<Dept>
{
public DeptCollection()
{
Dept dpt;

14. LINQ

257

dpt = new Dept();


dpt.ID = 1;
dpt.Name = " ";
Add(dpt);
dpt = new Dept();
dpt.ID = 2;
dpt.Name = "--";
Add(dpt);
}
}

.
Person DeptID, .
public short DeptID { get; set; }

LINQCollection,
DeptID - .
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.DeptID = 1;
. . .
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.DeptID = 1;
. . .
prs.F = "";
prs.N1 = "";
prs.N2 = "";
prs.DeptID = 2;

.
DeptCollection colDepts = new DeptCollection();

, , . ?
-
LINQCollection DeptCollection,
DeptID ID. , , .

258

IV. . LINQ

LINQCollection , .
DeptCollection , .
.
join, join, in, on equals.
join < > in < >
on < >
equals < >

join , ,
in . on
,
equals
.
join from .
var qryPersons = from person in colPersons
join dept in colDepts on person.DeptID equals dept.ID
select new {Name = person.F + " " + person.N1 + " " + person.N2,
Dept = dept.Name};

LINQCollection DeptCollection
, , , ,
. .


,
.
() - (
). , .
group.
group by.
group <, > by < >

group from join.

14. LINQ

259


IEnumerable. IGrouping, .
 Key ,

.
 Value "" ,

,
.
IGrouping ,
, "" .
IEnumerable<IGrouping<string, string>> qryDepts = from dept in colDepts
join person in colPersons on dept.ID equals person.DeptID
group person.F + " " + person.N1 + " " + person.N2 by dept.Name;

, , , , . ,
, .
, .
IEnumerable. IGrouping,
.

, ,
var (.
).

,
. lstDemo , ,
lstDetail , .
, ,
, , .
,
ObservableCollection.
.
colKeys colValues
.

260

IV. . LINQ

private System.Collections.ObjectModel.ObservableCollection<string>
colKeys;
private System.Collections.ObjectModel.ObservableCollection
<System.Collections.ObjectModel.ObservableCollection<string>> colValues;

colKeys .
colValues . , .
:
colKeys = new
System.Collections.ObjectModel.ObservableCollection<string>();
colValues = new System.Collections.ObjectModel.ObservableCollection
<System.Collections.ObjectModel.ObservableCollection<string>>();
foreach (IGrouping<string, string> dpt in qryDepts)
{
colKeys.Add(dpt.Key);
System.Collections.ObjectModel.ObservableCollection<string>
lst = new
System.Collections.ObjectModel.ObservableCollection<string>();
foreach (string prs in dpt)
{
lst.Add(prs);
}
colValues.Add(lst);
}
lstDemo.DataContext = colKeys;


colKeys colValues. colKeys , colValues .
lstDemo colKeys.
lstDemo SelectionChanged.
:
lstDetail.DataContext = colValues[lstDemo.SelectedIndex];

lstDemo .
colKeys, colValues.
, colValues lstDetail.

, .

14. LINQ

261


IEnumerable ,
, , ,
. ,
, .
, ,
.
. 14.1. .
14.1. IEnumerable,

Average

.
,

Count

LongCount

Max

.
,

Min

. ,

Sum

. ,

IEnumerable<string> qryAggregate = from person in colPersons


where person.Platforms.Contains("Java")
select person.F;
lblAggregate.Text = qryAggregate.Count().ToString();

, , Java. Count
, . . Java, ToString lblAggregate.
:
lblAggregate.Text = (from person in colPersons
where person.Platforms.Contains("Java")
select person.F).Count().ToString();

262

IV. . LINQ

? Count ,
, ,
. , , .
lblAggregate.Text = (from person in colPersons
select person.Platforms.Count()).Max().ToString();

lblAggregate , .


.

LINQ . , ,
, ,
- .
, , , . from, select group.
, :
IEnumerable<string> qryPlatforms = from person in colPersons
from platform in person.Platforms
orderby platform
select platform;

colPersons, from
. "" Platforms colPersons, from platform in person.Platforms.
, ""
colPersons, .
,
,
. , .
person in colPersons,

IEnumerable<string> qryPlatforms = from person in colPersons


from platform in person.Platforms
orderby platform
group platform by platform into plGroup
select plGroup.Key;

14. LINQ

263

, , , "" , .
, .
.
-
, , .
into,
group select. into . :
 group IGrouping, ;
 select , ,

. ,
select, , .
group plGroup. . , select, ,
.
"" :
var qryPersons = from person in colPersons
select new { FNN = person.F + " " + person.N1 + " " + person.N2,
Age = person.Age } into pr
group pr by pr.Age;

colPersons ,
FNN (, ) Age () .
, .
var qryDepts = from dept in colDepts
select new { deptname = dept.Name,
cnt = (from person in colPersons
where person.DeptID == dept.ID &&
person.Platforms.Contains("Silverlight")
select person).Count() };

colDepts , deptname (-

264

IV. . LINQ

) cnt ( ,
Silverlight ).
deptname , cnt .
colPersons ,
DeptID ID colDepts, "" Platforms "Silverlight". Count.




, - . . , .
, ,
let .
let < > = <>

, let, ,
C#, .
; .
, , , , , .

,
.
IEnumerable<string> qryPersons = from person in colPersons
let FNN = person.F + " " + person.N1 + " " + person.N2
orderby FNN descending
select FNN;

FNN,
, ,

14. LINQ

265

colPersons
.
IEnumerable<string> qryDepts = from dept in colDepts
let persons = (from person in colPersons where person.DeptID == dept.ID
select person)
where persons.Count() > 1
select dept.Name;

colDepts
persons, , , . (, , .)
, .
, , persons.


, .
, .
, ... . - , .
,
, - . DeptCollection, , .
dpt = new Dept();
dpt.ID = 3;
dpt.Name = "--";
Add(dpt);

... .
!
, join
. ,
. , join .
- . !

266

IV. . LINQ

LINQ,
. , , - ...
, , ,
. , .
,
:
IEnumerable<IGrouping<string, string>> qryDepts = from dept in colDepts
join person in colPersons on dept.ID equals person.DeptID
group person.F + " " + person.N1 + " " + person.N2 by dept.Name;

:
IEnumerable<IGrouping<string, string>> qryDepts = from dept in colDepts
join person in colPersons on dept.ID equals person.DeptID into prGroup
from pr in prGroup.DefaultIfEmpty(new Person())
group pr.F + " " + pr.N1 + " " + pr.N2 by dept.Name;

, join into,
. , , , , group.
, ( ) prGroup,
. , , .

. ,
group, .
DefaultIfEmpty pr.
DefaultIfEmpty? ,
. , , .
, .
, prGroup, DefaultIfEmpty
.
"", prGroup , DefaultIfEmpty , ,
Person, "" . , , "" .

14. LINQ

267

,
, . ,
, pr, . . , .

, Silverlight
. , SQL, .

.
.
. , ...

?
LINQ
Silverlight.
Visual Web Developer 2008
LINQ-, .
, ,
Silverlight. , .

268

IV. . LINQ


Silverlight.

15.

16.

17.

18.

19.

15

Silverlight : LINQ
. C#-, , .
- . !
Silverlight. Silverlight-.
XAML. ...


, . Silverlight
( XAML-).
Line . :
 X1 ;
 Y1 ;
 X2 ;
 Y2 ;
 Stroke ;
 StrokeThickness .

272

V. Silverlight.

, (X)
, (Y) .
X1, Y1, X2, Y2 StrokeThickness double. Stroke, Brush, ; -, .
XAML- ,
Silverlight. , , Green (),
Red (), Blue (), Black (), White () Transparent
(, "" ). Silverlight .
<Line X1="100" Y1="100" X2="300" Y2="200" Stroke="Aqua"></Line>

- ( Aqua Stroke).
Line . "", "" ""
. ,
,
"".
Line , : ( ,
), ( ), . Silverlight.

Rectangle . ; .
 Width .
 Height .
 Stroke .
 StrokeThickness .
 Fill .
Transparent,

. . .
 Opacity 0
( ) 1 ( ). 1.
 RadiusX -

. 0.

15.

273

 RadiusY

. 0.
Width, Height, StrokeThickness, Opacity, RadiusX RadiusY
, Stroke
Fill Brush.
Rectangle "" (Canvas).
5 Canvas.Left Canvas.Top.
, ;
.
<Canvas>
<Rectangle Canvas.Left="10" Canvas.Top="10" Width="100" Height="200"
Stroke="Green" StrokeThickness="10" Fill="Transparent"></Rectangle>
</Canvas>

, , .
, Fill.
Rectangle .
,
.
<Grid>
<Rectangle Stroke="Blue" Fill="Blue"></Rectangle>
<TextBox Margin="5,5,5,5"></TextBox>
</Grid>

"" . (. .
XAML- , ),
.
.
Ellipse Rectangle,
. Width, Height, Stroke,
StrokeThickness, Fill Opacity.
<Ellipse Canvas.Left="200" Canvas.Top="150" Stroke="Yellow" Fill="Yellow"
Width="100" Height="75"></Ellipse>

(Yellow) "".
<Ellipse Canvas.Left="100" Canvas.Top="100" Stroke="Black"
Width="200" Height="200"></Ellipse>

274

V. Silverlight.

, . . . ,
, , .


, Silverlight , , .
.
Polygon , . ,
,
. , .
,
, Points.
PointCollection Point, , . XAML-
< >,
< >, ; .
Polygon Stroke,
StrokeThickness, Fill Opacity.
"" Line, Polygon . "", "", "" .
<Polygon Points="0,100 100,0 100,200" Stroke="Black"></Polygon>

. ,
.
, , Polygon,
, . , . .
<Polygon Points="10,10 10,60 30,60 10,10 10,100 100,100 10,10"
Stroke="Black" Fill="Blue"></Polygon>

, . 15.1. ,
.

15.

275

. 15.1. ,
( FillRule EvenOdd)

, Silverlight ,

(, , ).
FillRule, FillRule EvenOdd
NonZero.
FillRule EvenOdd (, , ), Silverlight ""
, .
, ,
.
NonZero FillRule Silverlight
. ,
, ,
. , , .
, , , .
FillRule , XAML-, NonZero , .
, . 15.2.

. 15.2. ,
( FillRule NonZero)

276

V. Silverlight.

Polyline , , .
, Polygon.
<Polyline Points="0,100 100,0 100,200" Stroke="Black"></Polyline>

, .

.
, , Silverlight- , .
(, ), , .
Path. -
.
Path Data Geometry. Geometry
, ;
.
Path Stroke, StrokeThickness, Fill
.

Opacity.



, .
LineGeometry. StartPoint
EndPoint, , ,
. Point; XAML-
< >,< >;

.
<Path Stroke="Black">
<Path.Data>
<LineGeometry StartPoint="0,150" EndPoint="400,150"></LineGeometry>
</Path.Data>
</Path>

. (
...)

15.

277

.
RectangleGeometry.
:
 Rect ;
 RadiusX -

;
 RadiusY

.
RadiusX RadiusY .
Rect Rect. XAML-
:
< >,
< >,<>,<>
<RectangleGeometry Rect="20,20,200,200"
RadiusX="5" RadiusY="5"></RectangleGeometry>

.
. EllipseGeometry, :
 Center ;
 RadiusX ;
 RadiusY .

RadiusX RadiusY .
Center Point.
<EllipseGeometry Center="200,150"
RadiusX="100" RadiusY="75"></EllipseGeometry>

.

,
, . ,
, Line, Rectangle
Ellipse. ,
, .
GeometryGroup.
Geometry,
Data Path.

278

V. Silverlight.

GeometryGroup Children. GeometryCollection, Geometry. , ,


Children, .
, , .
GeometryGroup FillRule.
, Silverlight ,
, , , ().
<Path Stroke="Black" Fill="Blue">
<Path.Data>
<GeometryGroup>
<GeometryGroup.Children>
<GeometryCollection>
<RectangleGeometry Rect="150,0,100,300"></RectangleGeometry>
<RectangleGeometry Rect="0,100,400,100"></RectangleGeometry>
</GeometryCollection>
</GeometryGroup.Children>
</GeometryGroup>
</Path.Data>
</Path>

. , ,
.
<Path Stroke="Black" Fill="Blue">
<Path.Data>
<GeometryGroup>
<GeometryGroup.Children>
<GeometryCollection>
<EllipseGeometry Center="200,150" RadiusX="100"
RadiusY="100"></EllipseGeometry>
<RectangleGeometry Rect="150,100,100,100"></RectangleGeometry>
</GeometryCollection>
</GeometryGroup.Children>
</GeometryGroup>
</Path.Data>
</Path>

"" .

15.

279


PathGeometry,
, ,
, ,
(). "" Silverlight.
, ,
Figures . PathFigureCollection,
PathFigure, .
PathGeometry FillRule.
PathFigure, , Segments,
PathSegmentCollection;
PathSegment, . -, .
, PathFigure , .
 StartPoint

Point.
 IsClosed , -

.
false ( ), ,
true .
 IsFilled , -

; Path.
true ( ),
, false .
, .
? "" , .
,
.
 StartPoint

PathFigure.
 .
 .

, . PathFigure IsClosed true, . -

280

V. Silverlight.

IsFilled false,
.
LineSegment.
Point
Point.
<Path Stroke="Black" Fill="Blue">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="0,150" IsClosed="True" IsFilled="False">
<PathFigure.Segments>
<LineSegment Point="150,0"></LineSegment>
<LineSegment Point="150,300"></LineSegment>
</PathFigure.Segments>
</PathFigure>
<PathFigure StartPoint="200,150">
<PathFigure.Segments>
<LineSegment Point="350,0"></LineSegment>
<LineSegment Point="350,300"></LineSegment>
<LineSegment Point="200,150"></LineSegment>
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>

, .
. , .
PolyLineSegment , . Points, , ; Polygon
Polyline.
<Path Stroke="Black" Fill="Blue">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="0,150" IsClosed="True" IsFilled="False">
<PathFigure.Segments>

15.

281

<PolyLineSegment Points="150,0 150,300"></PolyLineSegment>


</PathFigure.Segments>
</PathFigure>
<PathFigure StartPoint="200,150">
<PathFigure.Segments>
<PolyLineSegment Points="350,0 350,300
200,150"></PolyLineSegment>
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>

, .
ArcSegment . , .
Point
Point.
Size . Size, XAML-
< >,< >;
.
RotationAngle , ,
, .
0.
SweepDirection , , SweepDirection :
 Counterclockwise (

);
 Clockwise .

IsLargeArc , 180 (
true) ( false; ).
<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<ArcSegment Point="100,100" Size="100,50"></ArcSegment>
<ArcSegment Point="0,0" Size="50,100"></ArcSegment>
</PathFigure.Segments>
</PathFigure>

282

V. Silverlight.

. ...
<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<ArcSegment Point="100,100" Size="100,50"></ArcSegment>
<ArcSegment Point="0,0" Size="100,50"
RotationAngle="90"></ArcSegment>
</PathFigure.Segments>
</PathFigure>

, .
( , ), BezierSegment. ,
Point.
 Point1 . -

, , . , Silverlight, , "" .

 Point2 .
 Point3 .
<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<BezierSegment Point1="50,100" Point2="200,0"
Point3="100,300"></BezierSegment>
</PathFigure.Segments>
</PathFigure>

.

, PolyBezierSegment. Points
PointCollection
Point, . XAML-
< >,<
>,< >, ;
.
<PathFigure StartPoint="0,0">
<PathFigure.Segments>

15.

283

<PolyBezierSegment
Points="50,100,200,0,100,150,50,100,0,200,0,0"></PolyBezierSegment>
</PathFigure.Segments>
</PathFigure>

, , .

, Point1. Point2
.
Point.

QuadraticBezierSegment.

<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<QuadraticBezierSegment Point1="50,100"
Point2="200,0"></QuadraticBezierSegment>
</PathFigure.Segments>
</PathFigure>

, .
, PolyQuadraticBezierSegment.
Points PointCollection Point, . XAML-
< >,< >, ; .
<PathFigure StartPoint="0,0">
<PathFigure.Segments>
<PolyQuadraticBezierSegment
Points="50,100,50,50,0,200,0,0"></PolyQuadraticBezierSegment>
</PathFigure.Segments>
</PathFigure>

... , , .
<PathFigure StartPoint="100,100">
<PathFigure.Segments>
<LineSegment Point="50,100"></LineSegment>
<ArcSegment Point="50,0" Size="100,50"></ArcSegment>
<BezierSegment Point1="0,0" Point2="100,50"
Point3="200,300"></BezierSegment>

284

V. Silverlight.

<QuadraticBezierSegment Point1="400,0"
Point2="0,0"></QuadraticBezierSegment>
</PathFigure.Segments>
</PathFigure>


- .
(. ). ,
, PathGeometry .

Border
Silverlight . Border, - . .
Border Child. UIElement; , Silverlight.
Border .
 BorderBrush Brush.
 Background Brush.
 BorderThickness
Thickness. XAML- <
>,< >,< >,
< >,

,
.
.
 CornerRadius

CornerRadius. XAML-
< >,<
>,<
>,< >,

,

. .

15.

285

Border ActualHeight, ActualWidth, Height,


HorizontalAlignment, Margin, MaxHeight, MaxLength, MaxWidth, MinHeight,
MinWidth, Name, Padding, VerticalAlignment, Visibility Width.
<Border BorderBrush="Black" BorderThickness="3" CornerRadius="5"
Padding="10,10,10,10">
<data:DataGrid>
. . .
</data:DataGrid>
</Border>

DataGrid .


,
Silverlight . ,
, "" . ,
Foreground,
"" (, ), Background,
, BorderBrush, .
?
Silverlight . ,
,
. .
Silverlight ; "" .
Brush.


.
, . Silverlight !
<Line X1="100" Y1="100" X2="300" Y2="200" Stroke="Aqua"></Line>


Silverlight , .
.NET Framework Class
Library for Silverlight / System.Windows.Media Namespace / SolidColorBrush Class Silverlight.

286

V. Silverlight.

XAML- Silverlight,
,
SolidColorBrush, , .
, SolidColorBrush "", XAML-.
<Line X1="100" Y1="100" X2="300" Y2="200">
<Line.Stroke>
<SolidColorBrush Color="#984EA5"></SolidColorBrush>
</Line.Stroke>
</Line>

, Color
SolidColorBrush. Color XAML-
#[< >]< ><
>< >,
00 FF. , . , .
, SolidColorBrush Opacity, . 0 ( ) 1 ( ).
<SolidColorBrush Color="#984EA5" Opacity="0.5"></SolidColorBrush>


, , , . ,
.
Windows 2000
Windows XP Windows Vista.
. "" , , . . 15.3.

LinearGradientBrush. , .
,
, .

15.

287

StartPoint ( ) EndPoint ( ), Point. , XAML- < >,


< >,
.

. 15.3.

! ,
.
[0,0] , [1,1]
. , - , 0 1. , [0,5,0,5] .
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
</LinearGradientBrush>

[0,0]
[1,0]. , "" ( . 15.3) .
<LinearGradientBrush StartPoint="0,0" EndPoint="0,0.5">
</LinearGradientBrush>

288

V. Silverlight.

"" . ( , "" , .)
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
</LinearGradientBrush>

, ""
, .
, . GradientStops,
GradientStopCollection, GradientStop.
GradientStop ,
, .
GradientStop ,
.
 Color Color.
 Offset -

, "" ,
0 ( ) 1 ( ).
<Rectangle Width="400" Height="300">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Black" Offset="0"></GradientStop>
<GradientStop Color="White" Offset="0.5"></GradientStop>
<GradientStop Color="Blue" Offset="1"></GradientStop>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>

, . 15.3. "" .
( 0)
, ( 0,5)
, ( 1) .

15.

289

, ,
.
.
LinearGradientBrush Opacity, .
, ,
. ?
Silverlight SpreadMethod
LinearGradientBrush.
GradientSpreadMethod :
 Pad , -

( );
 Reflect , -

;
 Repeat

.
<LinearGradientBrush StartPoint="0,0" EndPoint="0,0.5"
SpreadMethod="Reflect">
. . .
</LinearGradientBrush>

, ""
(. 15.4). - , .

RadialGradientBrush.
, [0,0]
, [1,1] .
,
RadialGradientBrush, .
 GradientOrigin , -

, Point;
 Center , -

, Point;
 RadiusX ,

, ;
 RadiusY ,

, .

290

V. Silverlight.

. 15.4.

GradientStops.
RadialGradientBrush Opacity SpreadMethod.
<Rectangle Width="400" Height="300">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5"
RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Black" Offset="0"></GradientStop>
<GradientStop Color="White" Offset="0.5"></GradientStop>
<GradientStop Color="Blue" Offset="1"></GradientStop>
</GradientStopCollection>
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>

, . 15.4.

15.

291

<Rectangle Width="400" Height="300">


<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="0.75,0.25" Center="0.5,0.5"
RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="White" Offset="0"></GradientStop>
<GradientStop Color="Black" Offset="0.5"></GradientStop>
</GradientStopCollection>
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>

, (. 15.5). ,
.

. 15.5.


, . ( 8.)

292

V. Silverlight.

ImageBrush.
ImageSource , ;
.
<Rectangle Width="400" Height="300">
<Rectangle.Fill>
<ImageBrush ImageSource="image.jpg"></ImageBrush>
</Rectangle.Fill>
</Rectangle>

AlignmentX , , , , .
AlignmentX :
 Left ;
 Right ;
 Center ( ).

AlignmentY .
AlignmentY :
 Top ;
 Bottom ;
 Center ( ).

Stretch , .
Stretch :
 None .

;
 Fill , -

. ( );
 Uniform , -

, . ;
 UniformToFill ,

, .
.

15.

293

ImageBrush Opacity.
<Rectangle Width="600" Height="800" Stroke="Black">
<Rectangle.Fill>
<ImageBrush ImageSource="image.jpg" AlignmentX="Right"
Stretch="None"></ImageBrush>
</Rectangle.Fill>
</Rectangle>

, , Silverlight ().
.

, , ,
Visual Web Developer 2008,
, . , 7 8.

,
MediaElement, .
( MediaElement 7.)
, 0, .
<MediaElement x:Name="melFill" Source="video.wmv"
Opacity="0"></MediaElement>

VideoBrush. SourceName, MediaElement, .


<Rectangle Width="400" Height="300" Stroke="Black">
<Rectangle.Fill>
<VideoBrush SourceName="melFill"></VideoBrush>
</Rectangle.Fill>
</Rectangle>

VideoBrush AlignmentX, AlignmentY,


Opacity Stretch.

294

V. Silverlight.


,
.
XAML-, , . ?
,
, . (
13.) .
<UserControl.Resources>
<LinearGradientBrush x:Key="lgbFill" StartPoint="0,0" EndPoint="1,0">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Black" Offset="0"></GradientStop>
<GradientStop Color="White" Offset="0.5"></GradientStop>
<GradientStop Color="Blue" Offset="1"></GradientStop>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</UserControl.Resources>

, , . .
<Rectangle Width="400" Height="300" Stroke="{StaticResource lgbFill}"
Fill="{StaticResource lgbFill}"></Rectangle>

- ,
.
, , . .

?
Silverlight. , ,
. .
Silverlight .
, . -
.

16


Silverlight
. , .
Silverlight-. ,
.

Silverlight.

Silverlight ,
, .
, -.
Silverlight .
. - .


, .
Clip, .
Clip Geometry. ,
Geometry, . . ,
( LineGeometry), .
15.

296

V. Silverlight.

,
, . , , .
<Image Source="image.jpg">
<Image.Clip>
<EllipseGeometry Center="150,200" RadiusX="150"
RadiusY="200"></EllipseGeometry>
</Image.Clip>
</Image>

. ,
(. 16.1).

. 16.1.



. , , , . . .

16.

297

. ;
.
OpacityMask,
. Brush.
<Image Source="image.jpg">
<Image.OpacityMask>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5"
RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FF000000" Offset="0"></GradientStop>
<GradientStop Color="#99000000" Offset="0.75"></GradientStop>
<GradientStop Color="#00000000" Offset="1"></GradientStop>
</GradientStopCollection>
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Image.OpacityMask>
</Image>

(. 16.2).

. 16.2.

298

V. Silverlight.


. ,
: .
Effect,
. Effect; -, .
BlurEffect.
Radius,
; 5.
<TextBlock Text="!" FontSize="36">
<TextBlock.Effect>
<BlurEffect></BlurEffect>
</TextBlock.Effect>
</TextBlock>

(. 16.3).

. 16.3.

DropShadowEffect.
, .
 BlurRadius

. 5.

 Color Color.

#FF000000 ( ).

 Direction -

. 315.

 Opacity

0 ( ) 1 ( ; ).

 ShadowDepth

. 5.

16.

299

<TextBlock Text="!" FontSize="36">


<TextBlock.Effect>
<DropShadowEffect Color="#FFCCCCCC"
ShadowDepth="10"></DropShadowEffect>
</TextBlock.Effect>
</TextBlock>

(. 16.4).

. 16.4.

, , , , , . .
Silverlight , . : .


, , . .
, ( Geometry) ( Brush).

RenderTransform.

 Transform.
 Transform

RelativeTransform.
, , , , ( 15).

300

V. Silverlight.

Transform.
-, .
(, ) , . TranslateTransform. X Y, .

, .
<TextBlock Text="!" FontSize="36">
<TextBlock.RenderTransform>
<TranslateTransform X="2" Y="2"></TranslateTransform>
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock Text="!" FontSize="36"
Foreground="Aqua"></TextBlock>

- .
, , 2 . -
; ,
.
<Rectangle Width="400" Height="300">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5"
RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Black" Offset="0"></GradientStop>
<GradientStop Color="White" Offset="0.5"></GradientStop>
<GradientStop Color="Blue" Offset="1"></GradientStop>
</GradientStopCollection>
</RadialGradientBrush.GradientStops>
<RadialGradientBrush.RelativeTransform>
<TranslateTransform X="0.25" Y="-0.25"></TranslateTransform>
</RadialGradientBrush.RelativeTransform>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>

, ,
.

16.

301

, , . . RelativeTransform,
.
ScaleTransform . , .
 ScaleX ScaleY , ,

. 1 (. . ).

 CenterX CenterY

, "" "" .
. 0; , [0,0]
.

<TextBlock Text="!" FontSize="36">


<TextBlock.RenderTransform>
<ScaleTransform CenterX="200" ScaleX="0.5"
ScaleY="3"></ScaleTransform>
</TextBlock.RenderTransform>
</TextBlock>


. ,
[200,0]
; ""
"" .
, RotateTransform.
, .
 Angle -

. , . 0.

 CenterX CenterY

, .
. 0; , [0,0] .

302

V. Silverlight.

<TextBlock Text="!" FontSize="36">


<TextBlock.RenderTransform>
<RotateTransform Angle="45"></RotateTransform>
</TextBlock.RenderTransform>
</TextBlock>

- ...
SkewTransform , .
.
 AngleX AngleY

.
, . 0.

 CenterX CenterY

, .
. 0; , [0,0] .

<TextBlock Text="!" FontSize="36">


<TextBlock.RenderTransform>
<SkewTransform AngleX="-45"></SkewTransform>
</TextBlock.RenderTransform>
</TextBlock>

.
<Rectangle Width="400" Height="300">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5"
RadiusX="0.5" RadiusY="0.5">
<RadialGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="Black" Offset="0"></GradientStop>
<GradientStop Color="White" Offset="0.5"></GradientStop>
<GradientStop Color="Blue" Offset="1"></GradientStop>
</GradientStopCollection>
</RadialGradientBrush.GradientStops>
<RadialGradientBrush.RelativeTransform>
<SkewTransform CenterX="0.5" CenterY="0.5"
AngleX="45"></SkewTransform>
</RadialGradientBrush.RelativeTransform>
</RadialGradientBrush>

16.

303

</Rectangle.Fill>
</Rectangle>

, ,
.
- ...

.

- , . .
TransformGroup.
Transform,
, ,
.
TransformGroup Children. TransformCollection, Transform.
, , Children, .
, , , .
, , , XAML-.
<TextBlock Text="!" FontSize="36">
<TextBlock.RenderTransform>
<TransformGroup>
<TransformGroup.Children>
<TransformCollection>
<RotateTransform Angle="-90"></RotateTransform>
<TranslateTransform Y="400"></TranslateTransform>
</TransformCollection>
</TransformGroup.Children>
</TransformGroup>
</TextBlock.RenderTransform>
</TextBlock>

90 , 400 ( ).
, .

304

V. Silverlight.


Silverlight . , , Silverlight 3;
.
.
Projection, Projection.
-, .

PlaneProjection.

PlaneProjection ,
.
 RotationX, RotationY RotationZ

X, Y Z
. , . 0.

 CenterOfRotationX, CenterOfRotationY CenterOfRotationZ -

, ,
X, Y Z. 0 ( )
1 ( ). 0,5, 0,5 0 ; ,
[0,5,0,5,0] .

 LocalOffsetX, LocalOffsetY LocalOffsetZ -

X, Y Z. , ,
. 0.

 GlobalOffsetX, GlobalOffsetY GlobalOffsetZ

X, Y Z. , , . 0.

<Image Source="image.jpg">
<Image.Projection>
<PlaneProjection RotationY="-30" RotationZ="20"
GlobalOffsetZ="-30"></PlaneProjection>

16.

305

</Image.Projection>
</Image>

30 Y,
20 Z Z
"" 30 .
. 16.5. , ...

. 16.5.

, , .
.

?
Silverlight.
, ,
, .
" " , .
.

17


Silverlight, .
,
, . ,
?
Silverlight-, . -, Silverlight ""
""! XAML-!

Silverlight-
. , ,
, .
, Silverlight - . - , ,
, , - .
,
. .
.
,
( ), Silverlight .
, ()
,

17.

307

. , ( ), , , , , . ,
.
: , . , .
,
. ,
. , ,
Silverlight: "
".
Silverlight ,
,
(. ).
, . . ,
.
,
. , ,
, (. . ).
.
Silverlight . ; , .
, , .
- ,
.
.
( ).
. ,
. , ,
Loaded .

308

V. Silverlight.

.
.

, .
.


, . Silverlight .
(
Image) , , "
". Opacity
1 0. ,
10 .
, .
C#-.
Storyboard.
Children TimelineCollection,
Timeline . Children ,
.
<Storyboard x:Name="sbdDemo">
</Storyboard>


, Silverlight.
. ( 13.)
<UserControl.Resources>
<Storyboard x:Name="sbdDemo">
</Storyboard>
</UserControl.Resources>

,
. ,
( x:Name). Silverlight, -,
, , , C#-. , ...

17.

309

. Silverlight, .
 ColorAnimation Color,

. . .
 DoubleAnimation .

 PointAnimation Point,

. . .

Opacity , DoubleAnimation.
: .
, ;
.
From .
, . , DoubleAnimation
, ColorAnimation Color, PointAnimation Point.
From , (, XAML-, ).
To .
. ,
.
By ,
. , ,
. ,
,
.
Duration .
Duration. XAML-
[<>:]<>:<>:<>[.< >].
<DoubleAnimation . . . Duration="0:0:10" . . .>

, 10 .
Duration
Automatic. .
<DoubleAnimation . . . Duration="Automatic" . . .>

310

V. Silverlight.

?
:
 Storyboard.TargetName ;
 Storyboard.TargetProperty .

, XAML- :
<UserControl.Resources>
<Storyboard x:Name="sbdDemo">
<DoubleAnimation From="1" To="0" Duration="0:0:10"
Storyboard.TargetName="imgDemo"
Storyboard.TargetProperty="Opacity"></DoubleAnimation>
</Storyboard>
</UserControl.Resources>

imgDemo .
Loaded
, :
sbdDemo.Begin();

Begin .
. , " " 10 .
.
<Storyboard x:Name="sbdDemo">
<PointAnimation To="150,0" Duration="0:0:10"
Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="StartPoint"></PointAnimation>
</Storyboard>
. . .
<Path Stroke="Black">
<Path.Data>
<LineGeometry x:Name="lngDemo" StartPoint="0,150"
EndPoint="400,150"></LineGeometry>
</Path.Data>
</Path>

. PointAnimation , StartPoint . , -

17.

311

(150,0);
, XAML- (0,150).
<Storyboard x:Name="sbdDemo">
<ColorAnimation To="Green" Duration="0:0:10"
Storyboard.TargetName="gdsDemo"
Storyboard.TargetProperty="Color"></ColorAnimation>
</Storyboard>
. . .
<Rectangle Stroke="Black">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop x:Name="gdsDemo" Color="Blue"
Offset="0"></GradientStop>
<GradientStop Color="Red" Offset="1"></GradientStop>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>

,
ColorAnimation.
,
. .
FillBehavior , , .
FillBehavior :
 HoldEnd (-

);

 Stop .

AutoReverse . false (, ,
), . true, , ; . , ,
, .
: AutoReverse true,
FillBehavior .

312

V. Silverlight.

, FillBehavior
HoldEnd.
, . ,
,
, . RepeatBehavior.
RepeatBehavior XAML- :
 < >x -

 [<>:]<>:<>:<>[.< >]

 Forever .

BeginTime .
TimeSpan, 10. XAML- [<>:]<>:<>:<>[.< >]. 0,
.
BeginTime . , .
. ,
EasingFunction IEasingFunction. , , , .
Graphics, Animation, and Media / Animation / Animation Overview Silverlight.
<Storyboard x:Name="sbdDemo">
<PointAnimation To="150,0" Duration="0:0:10"
Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="StartPoint">
<PointAnimation.EasingFunction>
<BounceEase EasingMode="EaseInOut"></BounceEase>
</PointAnimation.EasingFunction>
</PointAnimation>
</Storyboard>
. . .

17.

313

<Path Stroke="Black">
<Path.Data>
<LineGeometry x:Name="lngDemo" StartPoint="0,150"
EndPoint="400,150"></LineGeometry>
</Path.Data>
</Path>

, BounceEase.
.


. , Silverlight,
.
, , .
.
 ColorAnimationUsingKeyFrames
Color.
 DoubleAnimationUsingKeyFrames
.
 PointAnimationUsingKeyFrames

Point.
.

AutoReverse, BeginTime, Duration,


FillBehavior RepeatBehavior. From, To By , , .
KeyFrames, . :
 ColorKeyFrameCollection, ColorKeyFrame,

ColorAnimationUsingKeyFrames;

 DoubleKeyFrameCollection, DoubleKeyFrame,

DoubleAnimationUsingKeyFrames;

 PointKeyFrameCollection, PointKeyFrame,

PointAnimationUsingKeyFrames.

ColorKeyFrame, DoubleKeyFrame PointKeyFrame


, .

314

V. Silverlight.

,
. .
 LinearColorKeyFrame, LinearDoubleKeyFrame LinearPointKeyFrame -

 DiscreteColorKeyFrame, DiscreteDoubleKeyFrame DiscretePointKeyFrame

 SplineColorKeyFrame, SplineDoubleKeyFrame SplinePointKeyFrame -

, .

, , , . .

KeyTime. KeyTime XAML-


[<>:]<>:<>:<>[.< >].

Value.
.
.
, .
, ,
, . ,
PointAnimationUsingKeyFrames,
LinearPointKeyFrame, DiscretePointKeyFrame SplinePointKeyFrame.

.
.
, , .
LinearPointKeyFrame.
<LinearPointKeyFrame KeyTime="0:0:4"
Value="10,10"></LinearPointKeyFrame>

. KeyTime , Value Point.

17.

315

"" DiscretePointKeyFrame.
<DiscretePointKeyFrame KeyTime="0:0:5"
Value="10,100"></DiscretePointKeyFrame>

, .

, .
SplinePointKeyFrame.

KeySpline KeySpline. XAML-
< >,

< > <


>,<
>.

0 1.

<SplinePointKeyFrame KeyTime="0:0:10" Value="150,0"


KeySpline="0,0.75 0.75,1"></SplinePointKeyFrame>

, , " ".
XAML- :
<Storyboard x:Name="sbdDemo">
<PointAnimationUsingKeyFrames Duration="0:0:10"
Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="StartPoint">
<PointAnimationUsingKeyFrames.KeyFrames>
<LinearPointKeyFrame KeyTime="0:0:4"
Value="10,10"></LinearPointKeyFrame>
<DiscretePointKeyFrame KeyTime="0:0:5"
Value="10,100"></DiscretePointKeyFrame>
<SplinePointKeyFrame KeyTime="0:0:10"
Value="150,0" KeySpline="0,0.75 0.75,1"></SplinePointKeyFrame>
</PointAnimationUsingKeyFrames.KeyFrames>
</PointAnimationUsingKeyFrames>
</Storyboard>
. . .
<Path Stroke="Black">
<Path.Data>
<LineGeometry x:Name="lngDemo" StartPoint="0,150"
EndPoint="400,150"></LineGeometry>
</Path.Data>
</Path>

316

V. Silverlight.

, , ObjectAnimationUsingKeyFrames. Object, . . .
KeyFrames ObjectAnimationUsingKeyFrames ObjectKeyFrameCollection, ObjectKeyFrame,
.
- DiscreteObjectKeyFrame. ,
ObjectAnimationUsingKeyFrames ""
.
. , XAML-:
<Storyboard x:Name="sbdDemo">
<ObjectAnimationUsingKeyFrames Duration="0:0:5"
Storyboard.TargetName="recDemo" Storyboard.TargetProperty="Fill">
<ObjectAnimationUsingKeyFrames.KeyFrames>
<DiscreteObjectKeyFrame KeyTime="0:0:1">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Yellow"></SolidColorBrush>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:2">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Red"></SolidColorBrush>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:3">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Green"></SolidColorBrush>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:4">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Blue"></SolidColorBrush>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:5">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Black"></SolidColorBrush>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames.KeyFrames>

17.

317

</ObjectAnimationUsingKeyFrames>
</Storyboard>
. . .
<Rectangle x:Name="recDemo" Stroke="Black" Fill="White"></Rectangle>

, ObjectAnimationUsingKeyFrames, Fill, Brush. !



? ?
. ?
! ,
, . .
, .
.
<Storyboard x:Name="sbdDemo">
<PointAnimation To="150,0" Duration="0:0:10"
Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="StartPoint"></PointAnimation>
<PointAnimation To="150,400" Duration="0:0:10"
Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="EndPoint"></PointAnimation>
</Storyboard>
. . .
<Path Stroke="Black">
<Path.Data>
<LineGeometry x:Name="lngDemo" StartPoint="0,150"
EndPoint="400,150"></LineGeometry>
</Path.Data>
</Path>

.
.
<Storyboard x:Name="sbdDemo">
<Storyboard>
<PointAnimation To="150,0" Duration="0:0:10"
Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="StartPoint"></PointAnimation>

318

V. Silverlight.

<PointAnimation To="150,400" Duration="0:0:10"


Storyboard.TargetName="lngDemo"
Storyboard.TargetProperty="EndPoint"></PointAnimation>
</Storyboard>
<ColorAnimation To="White" Duration="0:0:10"
Storyboard.TargetName="scbDemo"
Storyboard.TargetProperty="Color"></ColorAnimation>
</Storyboard>
. . .
<Path>
<Path.Stroke>
<SolidColorBrush x:Name="scbDemo" Color="Black"></SolidColorBrush>
</Path.Stroke>
<Path.Data>
<LineGeometry x:Name="lngDemo" StartPoint="0,150"
EndPoint="400,150"></LineGeometry>
</Path.Data>
</Path>

,
. , ,
.
. "".
, , . .
BeginTime.
TimeSpan (. 10)
. , . , 5 ,
3 ,
8 (5+3) .
SpeedRatio. , , .
; 1 ,
1 . 1 ( ).
<Storyboard x:Name="sbdDemo">
<Storyboard BeginTime="0:0:2" SpeedRatio="0.5">

17.

319

<PointAnimation To="150,0" Duration="0:0:10"


Storyboard.TargetName="lngDemo"
. . .

, , 2 2 ( SpeedRatio
0.5).


, C#-.
, Storyboard.
 Begin . .
 Stop .
 Pause .
 Resume Pause

.
Seek SeekAlignedToLastTick "" .
TimeSpan, , "" ;
"" , .
.
Seek "", ,
. SeekAlignedToLastTick ,
, .
sbdDemo.Seek(new TimeSpan(0, 0, 1));

"" .
SkipToFill ""
. . AutoReverse true, . RepeatBehavior Forever, "" ,
InvalidOperation.

320

V. Silverlight.

Storyboard,
Completed. .
private void sbdFirst_Completed(object sender, EventArgs e)
{
sbdSecond.Begin();
}

Completed sbdFirst, sbdSecond.

?
, Silverlight .
Flash, , .
. Silverlight-, . 2, .

18


Silverlight:
, , .
!..
, .
Silverlight-. , .
, .



. .
, - Silverlight 3.
.
, , . C#-, , new , Web-, .
- Silverlight . , ,
.
Silverlight 3
. . ,
. .

322

V. Silverlight.

, Silverligh