< TextBlock >
< TextBlock.Text > Hello, World! </ TextBlock.Text >
</ TextBlock >
* This source code was highlighted with Source Code Highlighter .
< ResourceDictionary
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my ="clr-namespace:MyClassLib;assembly=MyClassLib" >
< Style TargetType ="my:Clocks" >
< Setter Property ="Template" >
< Setter.Value >
< ControlTemplate TargetType ="my:Clocks" >
< Canvas x:Name ="RootElement" />
</ ControlTemplate >
</ Setter.Value >
</ Setter >
</ Style >
</ ResourceDictionary >
* This source code was highlighted with Source Code Highlighter .
namespace MyClassLib
{
[TemplatePart(Name=Clocks.RootElement,Type= typeof (Canvas))]
public class Clocks : Control
{
private const string RootElement = "RootElement" ;
private Canvas rootElement;
public Clocks()
{
this .DefaultStyleKey = typeof (Clocks);
}
public override void OnApplyTemplate()
{
base .OnApplyTemplate();
this .rootElement = this .GetTemplateChild(RootElement) as Canvas;
}
}
}
* This source code was highlighted with Source Code Highlighter .
this .SizeChanged += new SizeChangedEventHandler(Clocks_SizeChanged);
* This source code was highlighted with Source Code Highlighter .
private Rectangle rect;
private void CreateContent()
{
this .rect = new Rectangle();
this .rect.Fill = new SolidColorBrush(Color.FromArgb(0xFF, 0xCC, 0xCC, 0xCC));
this .rootElement.Children.Add( this .rect);
}
private void Render()
{
this .rect.Width = this .ActualWidth;
this .rect.Height = this .ActualHeight;
}
* This source code was highlighted with Source Code Highlighter .
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace MyClassLib
{
[TemplatePart(Name=Clocks.RootElement,Type= typeof (Canvas))]
public class Clocks : Control
{
private const string RootElement = "RootElement" ;
private Canvas rootElement;
public Clocks()
{
this .DefaultStyleKey = typeof (Clocks);
this .SizeChanged += new SizeChangedEventHandler(Clocks_SizeChanged);
}
private void Clocks_SizeChanged( object sender, SizeChangedEventArgs e)
{
this .Render();
}
private TextBlock textBlock;
private DispatcherTimer timer;
private void CreateContent()
{
this .textBlock = new TextBlock();
this .textBlock.TextAlignment = TextAlignment.Center;
this .timer = new DispatcherTimer();
this .timer.Interval = new TimeSpan (0, 0, 0);
this .timer.Tick += new EventHandler(timer_Tick);
this .timer.Start();
this .UpdateClocks();
this .rootElement.Background = new SolidColorBrush(Color.FromArgb(0xFF,0xDD,0xDD,0xDD));
this .rootElement.Children.Add( this .textBlock);
}
private void timer_Tick( object sender, EventArgs e)
{
this .UpdateClocks();
}
private void UpdateClocks()
{
this .textBlock.Text = DateTime .Now.ToLongTimeString();
}
private void Render()
{
this .textBlock.Width = ActualWidth;
this .textBlock.Height = ActualHeight;
}
public override void OnApplyTemplate()
{
base .OnApplyTemplate();
this .rootElement = this .GetTemplateChild(RootElement) as Canvas;
this .CreateContent();
}
}
}
* This source code was highlighted with Source Code Highlighter .
the book was published (in the electronic version) “Introduction to Silverlight 2” - fully in Russian
download link: msdb.ru/Downloads/expression/resources/IntroducingMicrosoftSilverlight2.pdf
PS: thank you MSDN Lightning Bulletin)
Source: https://habr.com/ru/post/47692/
All Articles