Skip to content

Instantly share code, notes, and snippets.

@ghost1372
Last active November 11, 2023 16:46
Show Gist options
  • Save ghost1372/06a3f0c75d7985da7198cd8dd9006279 to your computer and use it in GitHub Desktop.
Save ghost1372/06a3f0c75d7985da7198cd8dd9006279 to your computer and use it in GitHub Desktop.
Create AnalogClock
// مرحله اول
public class AnalogClock : Control
{
}
// مرحله دوم
[TemplatePart(Name = PART_HOUR, Type = typeof(Line))]
[TemplatePart(Name = PART_MINUTE, Type = typeof(Line))]
[TemplatePart(Name = PART_SECOND, Type = typeof(Line))]
public class AnalogClock : Control
{
const string PART_HOUR = "PART_Hour"
const string PART_MINUTE = "PART_Minute"
const string PART_SECOND = "PART_Second"
}
// مرحله سوم
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
// مرحله چهارم
DispatcherTimer timer = new DispatcherTimer
{
Interval = new TimeSpan(0, 0, 1)
};
timer.Tick += (s, e) => UpdateClock();
timer.Start();
public void UpdateClock()
{
_hour.RenderTransform = new RotateTransform((DateTime.Now.Hour / 12.0) * 360, 0.5, 0.5);
_minute.RenderTransform = new RotateTransform((DateTime.Now.Minute / 60.0) * 360, 0.5, 0.5);
_second.RenderTransform = new RotateTransform((DateTime.Now.Second / 60.0) * 360, 0.5, 0.5);
}
// مرحله آخر
[TemplatePart(Name = PART_HOUR, Type = typeof(Line))]
[TemplatePart(Name = PART_MINUTE, Type = typeof(Line))]
[TemplatePart(Name = PART_SECOND, Type = typeof(Line))]
public class AnalogClock : Control
{
Line _hour, _minute, _second;
const string PART_HOUR = "PART_Hour"
const string PART_MINUTE = "PART_Minute"
const string PART_SECOND = "PART_Second"
public override void OnApplyTemplate()
{
_hour = Template.FindName(PART_HOUR, this) as Line;
_minute = Template.FindName(PART_MINUTE, this) as Line;
_second = Template.FindName(PART_SECOND, this) as Line;
UpdateClock();
DispatcherTimer timer = new DispatcherTimer
{
Interval = new TimeSpan(0, 0, 1)
};
timer.Tick += (s, e) => UpdateClock();
timer.Start();
base.OnApplyTemplate();
}
public void UpdateClock()
{
_hour.RenderTransform = new RotateTransform((DateTime.Now.Hour / 12.0) * 360, 0.5, 0.5);
_minute.RenderTransform = new RotateTransform((DateTime.Now.Minute / 60.0) * 360, 0.5, 0.5);
_second.RenderTransform = new RotateTransform((DateTime.Now.Second / 60.0) * 360, 0.5, 0.5);
}
}
<!-- مرحله اول استایل -->
<Style TargetType="local:AnalogClock">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:AnalogClock">
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- مرحله دوم استایل -->
<Grid>
<Line x:Name="PART_Hour" StrokeThickness="2" Stroke="{DynamicResource VioletBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" X1="0" X2="-70"/>
<Line x:Name="PART_Minute" StrokeThickness="1.5" Stroke="{DynamicResource PrimaryBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" X1="0" X2="-100"/>
<Line x:Name="PART_Second" StrokeThickness="1" Stroke="{DynamicResource DangerBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" X1="0" X2="-100"/>
<Ellipse StrokeThickness="1" Width="210" Height="210"/>
</Grid>
<!-- مرحله سوم استایل -->
<Grid.RenderTransform>
<RotateTransform Angle="90"/>
</Grid.RenderTransform>
<!-- مرحله آخر استایل -->
<Grid RenderTransformOrigin="0.5, 0.5">
<Grid.RenderTransform>
<RotateTransform Angle="90"/>
</Grid.RenderTransform>
<Line x:Name="PART_Hour" StrokeThickness="2" Stroke="{DynamicResource VioletBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" X1="0" X2="-70"/>
<Line x:Name="PART_Minute" StrokeThickness="1.5" Stroke="{DynamicResource PrimaryBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" X1="0" X2="-100"/>
<Line x:Name="PART_Second" StrokeThickness="1" Stroke="{DynamicResource DangerBrush}" VerticalAlignment="Center" HorizontalAlignment="Center" X1="0" X2="-100"/>
<Ellipse StrokeThickness="1" Width="210" Height="210"/>
</Grid>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment