Skip to content

Instantly share code, notes, and snippets.

@kyle-seongwoo-jun
Created May 5, 2018 11:43
Show Gist options
  • Save kyle-seongwoo-jun/43984a8adee8c880f4851d65a168ea6e to your computer and use it in GitHub Desktop.
Save kyle-seongwoo-jun/43984a8adee8c880f4851d65a168ea6e to your computer and use it in GitHub Desktop.
Xamarin.Forms Watch Example
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Watch"
x:Class="Watch.WatchPage">
<Grid>
<Grid x:Name="hourBox"
HorizontalOptions="Center"
VerticalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="2*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<BoxView Color="Black"
Grid.Row="1" />
</Grid>
<Grid x:Name="minBox"
HorizontalOptions="Center"
VerticalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<BoxView Color="Black" />
</Grid>
<Grid x:Name="secBox"
HorizontalOptions="Center"
VerticalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<BoxView Color="Red" />
</Grid>
</Grid>
</ContentPage>
using System;
using Xamarin.Forms;
namespace Watch
{
public partial class WatchPage : ContentPage
{
public WatchPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
UpdateUI(DateTime.Now);
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
UpdateUI(DateTime.Now);
return true;
});
}
void UpdateUI(DateTime time)
{
double hour = (time.Hour % 12) / 12.0;
double min = time.Minute / 60.0;
double sec = time.Second / 60.0;
hourBox.Rotation = hour * 360;
minBox.Rotation = min * 360;
secBox.Rotation = sec * 360;
Title = time.ToString("hh:mm:ss");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment