MainWindow.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using System.Text;
  5. using Perspex;
  6. using Perspex.Animation;
  7. using Perspex.Collections;
  8. using Perspex.Controls;
  9. using Perspex.Controls.Html;
  10. using Perspex.Controls.Primitives;
  11. using Perspex.Controls.Shapes;
  12. using Perspex.Controls.Templates;
  13. using Perspex.Diagnostics;
  14. using Perspex.Layout;
  15. using Perspex.Media;
  16. using Perspex.Media.Imaging;
  17. using TestApplication;
  18. namespace TestApplication
  19. {
  20. class MainWindow
  21. {
  22. private static readonly PerspexList<Node> s_treeData = new PerspexList<Node>
  23. {
  24. new Node
  25. {
  26. Name = "Root 1",
  27. Children = new PerspexList<Node>
  28. {
  29. new Node
  30. {
  31. Name = "Child 1",
  32. },
  33. new Node
  34. {
  35. Name = "Child 2",
  36. Children = new PerspexList<Node>
  37. {
  38. new Node
  39. {
  40. Name = "Grandchild 1",
  41. },
  42. new Node
  43. {
  44. Name = "Grandmaster Flash",
  45. },
  46. }
  47. },
  48. new Node
  49. {
  50. Name = "Child 3",
  51. },
  52. }
  53. },
  54. new Node
  55. {
  56. Name = "Root 2",
  57. },
  58. };
  59. private static readonly PerspexList<Item> s_listBoxData = new PerspexList<Item>
  60. {
  61. new Item { Name = "Item 1", Value = "Item 1 Value" },
  62. new Item { Name = "Item 2", Value = "Item 2 Value" },
  63. new Item { Name = "Item 3", Value = "Item 3 Value" },
  64. new Item { Name = "Item 4", Value = "Item 4 Value" },
  65. new Item { Name = "Item 5", Value = "Item 5 Value" },
  66. new Item { Name = "Item 6", Value = "Item 6 Value" },
  67. new Item { Name = "Item 7", Value = "Item 7 Value" },
  68. new Item { Name = "Item 8", Value = "Item 8 Value" },
  69. };
  70. public static Window Create()
  71. {
  72. TabControl container;
  73. Window window = new Window
  74. {
  75. Title = "Perspex Test Application",
  76. Width = 900,
  77. Height = 480,
  78. Content = new Grid
  79. {
  80. ColumnDefinitions = new ColumnDefinitions
  81. {
  82. new ColumnDefinition(1, GridUnitType.Star),
  83. new ColumnDefinition(1, GridUnitType.Star),
  84. },
  85. RowDefinitions = new RowDefinitions
  86. {
  87. new RowDefinition(GridLength.Auto),
  88. new RowDefinition(1, GridUnitType.Star),
  89. new RowDefinition(GridLength.Auto),
  90. },
  91. Children = new Controls
  92. {
  93. (container = new TabControl
  94. {
  95. Padding = new Thickness(5),
  96. Items = new[]
  97. {
  98. ButtonsTab(),
  99. TextTab(),
  100. HtmlTab(),
  101. ImagesTab(),
  102. ListsTab(),
  103. LayoutTab(),
  104. AnimationsTab(),
  105. },
  106. Transition = new CrossFade(TimeSpan.FromSeconds(0.25)),
  107. [Grid.RowProperty] = 1,
  108. [Grid.ColumnSpanProperty] = 2,
  109. })
  110. }
  111. },
  112. };
  113. container.Classes.Add(":container");
  114. window.Show();
  115. return window;
  116. }
  117. private static TabItem ButtonsTab()
  118. {
  119. var result = new TabItem
  120. {
  121. Header = "Button",
  122. Content = new ScrollViewer()
  123. {
  124. CanScrollHorizontally = false,
  125. Content = new StackPanel
  126. {
  127. Margin = new Thickness(10),
  128. Orientation = Orientation.Vertical,
  129. Gap = 4,
  130. Children = new Controls
  131. {
  132. new TextBlock
  133. {
  134. Text = "Button",
  135. FontWeight = FontWeight.Medium,
  136. FontSize = 20,
  137. Foreground = SolidColorBrush.Parse("#212121"),
  138. },
  139. new TextBlock
  140. {
  141. Text = "A button control",
  142. FontSize = 13,
  143. Foreground = SolidColorBrush.Parse("#727272"),
  144. Margin = new Thickness(0, 0, 0, 10)
  145. },
  146. new Button
  147. {
  148. Width = 150,
  149. Content = "Button"
  150. },
  151. new Button
  152. {
  153. Width = 150,
  154. Content = "Disabled",
  155. IsEnabled = false,
  156. },
  157. new TextBlock
  158. {
  159. Margin = new Thickness(0, 40, 0, 0),
  160. Text = "ToggleButton",
  161. FontWeight = FontWeight.Medium,
  162. FontSize = 20,
  163. Foreground = SolidColorBrush.Parse("#212121"),
  164. },
  165. new TextBlock
  166. {
  167. Text = "A toggle button control",
  168. FontSize = 13,
  169. Foreground = SolidColorBrush.Parse("#727272"),
  170. Margin = new Thickness(0, 0, 0, 10)
  171. },
  172. new ToggleButton
  173. {
  174. Width = 150,
  175. IsChecked = true,
  176. Content = "On"
  177. },
  178. new ToggleButton
  179. {
  180. Width = 150,
  181. IsChecked = false,
  182. Content = "Off"
  183. },
  184. }
  185. }
  186. },
  187. };
  188. return result;
  189. }
  190. private static TabItem HtmlTab()
  191. {
  192. return new TabItem
  193. {
  194. Header = "Text",
  195. Content = new ScrollViewer()
  196. {
  197. CanScrollHorizontally = false,
  198. Content = new StackPanel()
  199. {
  200. Margin = new Thickness(10),
  201. Orientation = Orientation.Vertical,
  202. Gap = 4,
  203. Children = new Controls
  204. {
  205. new TextBlock
  206. {
  207. Text = "TextBlock",
  208. FontWeight = FontWeight.Medium,
  209. FontSize = 20,
  210. Foreground = SolidColorBrush.Parse("#212121"),
  211. },
  212. new TextBlock
  213. {
  214. Text = "A control for displaying text.",
  215. FontSize = 13,
  216. Foreground = SolidColorBrush.Parse("#727272"),
  217. Margin = new Thickness(0, 0, 0, 10)
  218. },
  219. new TextBlock
  220. {
  221. Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
  222. FontSize = 11
  223. },
  224. new TextBlock
  225. {
  226. Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
  227. FontSize = 11,
  228. FontWeight = FontWeight.Medium
  229. },
  230. new TextBlock
  231. {
  232. Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
  233. FontSize = 11,
  234. FontWeight = FontWeight.Bold
  235. },
  236. new TextBlock
  237. {
  238. Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
  239. FontSize = 11,
  240. FontStyle = FontStyle.Italic,
  241. },
  242. new TextBlock
  243. {
  244. Margin = new Thickness(0, 40, 0, 0),
  245. Text = "HtmlLabel",
  246. FontWeight = FontWeight.Medium,
  247. FontSize = 20,
  248. Foreground = SolidColorBrush.Parse("#212121"),
  249. },
  250. new TextBlock
  251. {
  252. Text = "A label capable of displaying HTML content",
  253. FontSize = 13,
  254. Foreground = SolidColorBrush.Parse("#727272"),
  255. Margin = new Thickness(0, 0, 0, 10)
  256. },
  257. new HtmlLabel
  258. {
  259. Background = SolidColorBrush.Parse("#CCCCCC"),
  260. Padding = new Thickness(5),
  261. Text = @"<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href=""#"">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
  262. <h2>Header Level 2</h2>
  263. <ol>
  264. <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  265. <li>Aliquam tincidunt mauris eu risus.</li>
  266. </ol>
  267. <blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote>
  268. <h3>Header Level 3</h3>
  269. <ul>
  270. <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  271. <li>Aliquam tincidunt mauris eu risus.</li>
  272. </ul>"
  273. }
  274. }
  275. }
  276. }
  277. };
  278. }
  279. private static TabItem TextTab()
  280. {
  281. return new TabItem
  282. {
  283. Header = "Input",
  284. Content = new ScrollViewer()
  285. {
  286. Content = new StackPanel
  287. {
  288. Margin = new Thickness(10),
  289. Orientation = Orientation.Vertical,
  290. Gap = 4,
  291. Children = new Controls
  292. {
  293. new TextBlock
  294. {
  295. Text = "TextBox",
  296. FontWeight = FontWeight.Medium,
  297. FontSize = 20,
  298. Foreground = SolidColorBrush.Parse("#212121"),
  299. },
  300. new TextBlock
  301. {
  302. Text = "A text box control",
  303. FontSize = 13,
  304. Foreground = SolidColorBrush.Parse("#727272"),
  305. Margin = new Thickness(0, 0, 0, 10)
  306. },
  307. new TextBox { Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", Width = 200},
  308. new TextBox { Width = 200, Watermark="Watermark"},
  309. new TextBox { Width = 200, Watermark="Floating Watermark", UseFloatingWatermark = true },
  310. new TextBox { AcceptsReturn = true, TextWrapping = TextWrapping.Wrap, Width = 200, Height = 150, Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est." },
  311. new TextBlock
  312. {
  313. Margin = new Thickness(0, 40, 0, 0),
  314. Text = "CheckBox",
  315. FontWeight = FontWeight.Medium,
  316. FontSize = 20,
  317. Foreground = SolidColorBrush.Parse("#212121"),
  318. },
  319. new TextBlock
  320. {
  321. Text = "A check box control",
  322. FontSize = 13,
  323. Foreground = SolidColorBrush.Parse("#727272"),
  324. Margin = new Thickness(0, 0, 0, 10)
  325. },
  326. new CheckBox { IsChecked = true, Margin = new Thickness(0, 0, 0, 5), Content = "Checked" },
  327. new CheckBox { IsChecked = false, Content = "Unchecked" },
  328. new TextBlock
  329. {
  330. Margin = new Thickness(0, 40, 0, 0),
  331. Text = "RadioButton",
  332. FontWeight = FontWeight.Medium,
  333. FontSize = 20,
  334. Foreground = SolidColorBrush.Parse("#212121"),
  335. },
  336. new TextBlock
  337. {
  338. Text = "A radio button control",
  339. FontSize = 13,
  340. Foreground = SolidColorBrush.Parse("#727272"),
  341. Margin = new Thickness(0, 0, 0, 10)
  342. },
  343. new RadioButton { IsChecked = true, Content = "Option 1" },
  344. new RadioButton { IsChecked = false, Content = "Option 2" },
  345. new RadioButton { IsChecked = false, Content = "Option 3" },
  346. }
  347. }
  348. }
  349. };
  350. }
  351. private static TabItem ListsTab()
  352. {
  353. return new TabItem
  354. {
  355. Header = "Lists",
  356. Content = new ScrollViewer()
  357. {
  358. CanScrollHorizontally = false,
  359. Content = new StackPanel
  360. {
  361. HorizontalAlignment = HorizontalAlignment.Left,
  362. Orientation = Orientation.Vertical,
  363. VerticalAlignment = VerticalAlignment.Top,
  364. Gap = 4,
  365. Margin = new Thickness(10),
  366. DataTemplates = new DataTemplates
  367. {
  368. new FuncDataTemplate<Item>(x =>
  369. new StackPanel
  370. {
  371. Gap = 4,
  372. Orientation = Orientation.Horizontal,
  373. Children = new Controls
  374. {
  375. new Image { Width = 50, Height = 50, Source = new Bitmap("github_icon.png") },
  376. new TextBlock { Text = x.Name, FontSize = 18 }
  377. }
  378. })
  379. },
  380. Children = new Controls
  381. {
  382. new TextBlock
  383. {
  384. Text = "ListBox",
  385. FontWeight = FontWeight.Medium,
  386. FontSize = 20,
  387. Foreground = SolidColorBrush.Parse("#212121"),
  388. },
  389. new TextBlock
  390. {
  391. Text = "A list box control.",
  392. FontSize = 13,
  393. Foreground = SolidColorBrush.Parse("#727272"),
  394. Margin = new Thickness(0, 0, 0, 10)
  395. },
  396. new ListBox
  397. {
  398. BorderThickness = 2,
  399. Items = s_listBoxData,
  400. Height = 300,
  401. Width = 300,
  402. },
  403. new TextBlock
  404. {
  405. Margin = new Thickness(0, 40, 0, 0),
  406. Text = "TreeView",
  407. FontWeight = FontWeight.Medium,
  408. FontSize = 20,
  409. Foreground = SolidColorBrush.Parse("#212121"),
  410. },
  411. new TextBlock
  412. {
  413. Text = "A tree view control.",
  414. FontSize = 13,
  415. Foreground = SolidColorBrush.Parse("#727272"),
  416. Margin = new Thickness(0, 0, 0, 10)
  417. },
  418. new TreeView
  419. {
  420. Name = "treeView",
  421. Items = s_treeData,
  422. Height = 300,
  423. BorderThickness = 2,
  424. Width = 300,
  425. }
  426. }
  427. },
  428. }
  429. };
  430. }
  431. private static TabItem ImagesTab()
  432. {
  433. var imageCarousel = new Carousel
  434. {
  435. Width = 400,
  436. Height = 400,
  437. Transition = new PageSlide(TimeSpan.FromSeconds(0.25)),
  438. Items = new[]
  439. {
  440. new Image { Source = new Bitmap("github_icon.png"), Width = 400, Height = 400 },
  441. new Image { Source = new Bitmap("pattern.jpg"), Width = 400, Height = 400 },
  442. }
  443. };
  444. var next = new Button
  445. {
  446. VerticalAlignment = VerticalAlignment.Center,
  447. Padding = new Thickness(20),
  448. Content = new Perspex.Controls.Shapes.Path
  449. {
  450. Data = StreamGeometry.Parse("M4,11V13H16L10.5,18.5L11.92,19.92L19.84,12L11.92,4.08L10.5,5.5L16,11H4Z"),
  451. Fill = Brushes.Black
  452. }
  453. };
  454. var prev = new Button
  455. {
  456. VerticalAlignment = VerticalAlignment.Center,
  457. Padding = new Thickness(20),
  458. Content = new Perspex.Controls.Shapes.Path
  459. {
  460. Data = StreamGeometry.Parse("M20,11V13H8L13.5,18.5L12.08,19.92L4.16,12L12.08,4.08L13.5,5.5L8,11H20Z"),
  461. Fill = Brushes.Black
  462. }
  463. };
  464. prev.Click += (s, e) =>
  465. {
  466. if (imageCarousel.SelectedIndex == 0)
  467. imageCarousel.SelectedIndex = 1;
  468. else
  469. imageCarousel.SelectedIndex--;
  470. };
  471. next.Click += (s, e) =>
  472. {
  473. if (imageCarousel.SelectedIndex == 1)
  474. imageCarousel.SelectedIndex = 0;
  475. else
  476. imageCarousel.SelectedIndex++;
  477. };
  478. return new TabItem
  479. {
  480. Header = "Images",
  481. Content = new ScrollViewer
  482. {
  483. Content = new StackPanel
  484. {
  485. HorizontalAlignment = HorizontalAlignment.Left,
  486. Orientation = Orientation.Vertical,
  487. VerticalAlignment = VerticalAlignment.Top,
  488. Gap = 4,
  489. Margin = new Thickness(10),
  490. Children = new Controls
  491. {
  492. new TextBlock
  493. {
  494. Text = "Carousel",
  495. FontWeight = FontWeight.Medium,
  496. FontSize = 20,
  497. Foreground = SolidColorBrush.Parse("#212121"),
  498. },
  499. new TextBlock
  500. {
  501. Text = "An items control that displays its items as pages that fill the controls.",
  502. FontSize = 13,
  503. Foreground = SolidColorBrush.Parse("#727272"),
  504. Margin = new Thickness(0, 0, 0, 10)
  505. },
  506. new StackPanel
  507. {
  508. Name = "carouselVisual",
  509. Orientation = Orientation.Horizontal,
  510. Gap = 4,
  511. Children = new Controls
  512. {
  513. prev,
  514. imageCarousel,
  515. next
  516. }
  517. }
  518. }
  519. }
  520. }
  521. };
  522. }
  523. private static TabItem LayoutTab()
  524. {
  525. return new TabItem
  526. {
  527. Header = "Layout",
  528. Content = new ScrollViewer
  529. {
  530. Content = new StackPanel
  531. {
  532. HorizontalAlignment = HorizontalAlignment.Left,
  533. Orientation = Orientation.Vertical,
  534. VerticalAlignment = VerticalAlignment.Top,
  535. Gap = 4,
  536. Margin = new Thickness(10),
  537. Children = new Controls
  538. {
  539. new TextBlock
  540. {
  541. Text = "Grid",
  542. FontWeight = FontWeight.Medium,
  543. FontSize = 20,
  544. Foreground = SolidColorBrush.Parse("#212121"),
  545. },
  546. new TextBlock
  547. {
  548. Text = "Lays out child controls according to a grid.",
  549. FontSize = 13,
  550. Foreground = SolidColorBrush.Parse("#727272"),
  551. Margin = new Thickness(0, 0, 0, 10)
  552. },
  553. new Grid
  554. {
  555. Width = 600,
  556. ColumnDefinitions = new ColumnDefinitions
  557. {
  558. new ColumnDefinition(1, GridUnitType.Star),
  559. new ColumnDefinition(1, GridUnitType.Star),
  560. },
  561. RowDefinitions = new RowDefinitions
  562. {
  563. new RowDefinition(1, GridUnitType.Auto),
  564. new RowDefinition(1, GridUnitType.Auto)
  565. },
  566. Children = new Controls
  567. {
  568. new Rectangle
  569. {
  570. Fill = SolidColorBrush.Parse("#FF5722"),
  571. [Grid.ColumnSpanProperty] = 2,
  572. Height = 200,
  573. Margin = new Thickness(2.5)
  574. },
  575. new Rectangle
  576. {
  577. Fill = SolidColorBrush.Parse("#FF5722"),
  578. [Grid.RowProperty] = 1,
  579. Height = 100,
  580. Margin = new Thickness(2.5)
  581. },
  582. new Rectangle
  583. {
  584. Fill = SolidColorBrush.Parse("#FF5722"),
  585. [Grid.RowProperty] = 1,
  586. [Grid.ColumnProperty] = 1,
  587. Height = 100,
  588. Margin = new Thickness(2.5)
  589. },
  590. },
  591. },
  592. new TextBlock
  593. {
  594. Margin = new Thickness(0, 40, 0, 0),
  595. Text = "StackPanel",
  596. FontWeight = FontWeight.Medium,
  597. FontSize = 20,
  598. Foreground = SolidColorBrush.Parse("#212121"),
  599. },
  600. new TextBlock
  601. {
  602. Text = "A panel which lays out its children horizontally or vertically.",
  603. FontSize = 13,
  604. Foreground = SolidColorBrush.Parse("#727272"),
  605. Margin = new Thickness(0, 0, 0, 10)
  606. },
  607. new StackPanel
  608. {
  609. Orientation = Orientation.Vertical,
  610. Gap = 4,
  611. Width = 300,
  612. Children = new Controls
  613. {
  614. new Rectangle
  615. {
  616. Fill = SolidColorBrush.Parse("#FFC107"),
  617. Height = 50,
  618. },
  619. new Rectangle
  620. {
  621. Fill = SolidColorBrush.Parse("#FFC107"),
  622. Height = 50,
  623. },
  624. new Rectangle
  625. {
  626. Fill = SolidColorBrush.Parse("#FFC107"),
  627. Height = 50,
  628. },
  629. }
  630. },
  631. new TextBlock
  632. {
  633. Margin = new Thickness(0, 40, 0, 0),
  634. Text = "Canvas",
  635. FontWeight = FontWeight.Medium,
  636. FontSize = 20,
  637. Foreground = SolidColorBrush.Parse("#212121"),
  638. },
  639. new TextBlock
  640. {
  641. Text = "A panel which lays out its children by explicit coordinates.",
  642. FontSize = 13,
  643. Foreground = SolidColorBrush.Parse("#727272"),
  644. Margin = new Thickness(0, 0, 0, 10)
  645. },
  646. new Canvas
  647. {
  648. Background = Brushes.Yellow,
  649. Width = 300,
  650. Height = 400,
  651. Children = new Controls
  652. {
  653. new Rectangle
  654. {
  655. Fill = Brushes.Blue,
  656. Width = 63,
  657. Height = 41,
  658. [Canvas.LeftProperty] = 40,
  659. [Canvas.TopProperty] = 31,
  660. },
  661. new Ellipse
  662. {
  663. Fill = Brushes.Green,
  664. Width = 58,
  665. Height = 58,
  666. [Canvas.LeftProperty] = 130,
  667. [Canvas.TopProperty] = 79,
  668. },
  669. }
  670. },
  671. }
  672. }
  673. }
  674. };
  675. }
  676. private static TabItem AnimationsTab()
  677. {
  678. Border border1;
  679. Border border2;
  680. RotateTransform rotate;
  681. Button button1;
  682. var result = new TabItem
  683. {
  684. Header = "Animations",
  685. Content = new StackPanel
  686. {
  687. Orientation = Orientation.Vertical,
  688. Gap = 4,
  689. Margin = new Thickness(10),
  690. Children = new Controls
  691. {
  692. new TextBlock
  693. {
  694. Text = "Animations",
  695. FontWeight = FontWeight.Medium,
  696. FontSize = 20,
  697. Foreground = SolidColorBrush.Parse("#212121"),
  698. },
  699. new TextBlock
  700. {
  701. Text = "A few animations showcased below",
  702. FontSize = 13,
  703. Foreground = SolidColorBrush.Parse("#727272"),
  704. Margin = new Thickness(0, 0, 0, 10)
  705. },
  706. (button1 = new Button
  707. {
  708. Content = "Animate",
  709. Width = 120,
  710. [Grid.ColumnProperty] = 1,
  711. [Grid.RowProperty] = 1,
  712. }),
  713. new Canvas
  714. {
  715. ClipToBounds = false,
  716. Children = new Controls
  717. {
  718. (border1 = new Border
  719. {
  720. Width = 100,
  721. Height = 100,
  722. HorizontalAlignment = HorizontalAlignment.Center,
  723. VerticalAlignment = VerticalAlignment.Center,
  724. Background = Brushes.Crimson,
  725. RenderTransform = new RotateTransform(),
  726. Child = new TextBox
  727. {
  728. Background = Brushes.White,
  729. Text = "Hello!",
  730. HorizontalAlignment = HorizontalAlignment.Center,
  731. VerticalAlignment = VerticalAlignment.Center,
  732. },
  733. [Canvas.LeftProperty] = 100,
  734. [Canvas.TopProperty] = 100,
  735. }),
  736. (border2 = new Border
  737. {
  738. Width = 100,
  739. Height = 100,
  740. HorizontalAlignment = HorizontalAlignment.Center,
  741. VerticalAlignment = VerticalAlignment.Center,
  742. Background = Brushes.Coral,
  743. Child = new Image
  744. {
  745. Source = new Bitmap("github_icon.png"),
  746. HorizontalAlignment = HorizontalAlignment.Center,
  747. VerticalAlignment = VerticalAlignment.Center,
  748. },
  749. RenderTransform = (rotate = new RotateTransform
  750. {
  751. PropertyTransitions = new PropertyTransitions
  752. {
  753. RotateTransform.AngleProperty.Transition(500),
  754. }
  755. }),
  756. PropertyTransitions = new PropertyTransitions
  757. {
  758. Layoutable.WidthProperty.Transition(300),
  759. Layoutable.HeightProperty.Transition(1000),
  760. },
  761. [Canvas.LeftProperty] = 400,
  762. [Canvas.TopProperty] = 100,
  763. }),
  764. }
  765. }
  766. },
  767. },
  768. };
  769. button1.Click += (s, e) =>
  770. {
  771. if (border2.Width == 100)
  772. {
  773. border2.Width = border2.Height = 400;
  774. rotate.Angle = 180;
  775. }
  776. else
  777. {
  778. border2.Width = border2.Height = 100;
  779. rotate.Angle = 0;
  780. }
  781. };
  782. var start = Animate.Stopwatch.Elapsed;
  783. var degrees = Animate.Timer
  784. .Select(x =>
  785. {
  786. var elapsed = (x - start).TotalSeconds;
  787. var cycles = elapsed / 4;
  788. var progress = cycles % 1;
  789. return 360.0 * progress;
  790. });
  791. border1.RenderTransform.Bind(
  792. RotateTransform.AngleProperty,
  793. degrees,
  794. BindingPriority.Animation);
  795. return result;
  796. }
  797. }
  798. }