Hi Forum,
hab eben mal ne Binäruhr mit C# und WPF gezaubert.
Wollte hier nur meinen Code bereitstellen, falls jemand Interesse daran hat
Ich bin dankbar für Kritik und Verbesserungsvorschläge.
Edit:
Converter.cs
ZeitSpeicher.cs
UhrModel.cs
Uhr.xaml
Screenshot:
Danke an alle, die hier ihre Kritik abgegeben haben.
Besonderen Dank an @ErfinderDesRades
Viel Spaß mit dem Code, die Projektmappe findet ihr im Anhang.
Alt
Fenster (natürlich XAML, nicht XML ) :
Spoiler anzeigen
Spoiler anzeigen
Und so wie sich's gehört, ein Bildschirmschuss :
Mit freundlichen Grüßen
nxtman
hab eben mal ne Binäruhr mit C# und WPF gezaubert.
Wollte hier nur meinen Code bereitstellen, falls jemand Interesse daran hat
Ich bin dankbar für Kritik und Verbesserungsvorschläge.
Edit:
C#-Quellcode
- using System;
- using System.Globalization;
- using System.Windows.Data;
- using System.Windows.Media;
- namespace BinaerUhrMVVM.Converters
- {
- [ValueConversion(typeof(bool),typeof(Brush))]
- public class Converter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if ((bool)value)
- return Brushes.Red;
- return Brushes.LightGray;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- }
C#-Quellcode
- using System;
- using System.ComponentModel;
- using System.Collections.ObjectModel;
- namespace BinaerUhrMVVM
- {
- public class ZeitSpeicher
- {
- public ObservableCollection<bool> H { get; private set; }
- public ObservableCollection<bool> M { get; private set; }
- public ObservableCollection<bool> S { get; private set; }
- public ZeitSpeicher()
- {
- H = new ObservableCollection<bool>(new bool[5]);
- M = new ObservableCollection<bool>(new bool[6]);
- S = new ObservableCollection<bool>(new bool[6]);
- }
- public void setTime(bool[] h, bool[] m, bool[] s)
- {
- for (int i = 0; i < 6; i++)
- {
- if(i < 5)
- this.H[i] = h[i];
- this.M[i] = m[i];
- this.S[i] = s[i];
- }
- }
- public bool[] toBinary(int time)
- {
- int tempTime = time;
- bool[] bits = new bool[6];
- for (int i = 0; i < 6; i++)
- {
- if ((tempTime / (1 << (5 - i))) > 0)
- {
- bits[i] = true;
- tempTime -= (1 << (5 - i));
- }
- else
- bits[i] = false;
- }
- Array.Reverse(bits);
- return bits;
- }
- public void act(DateTime time)
- {
- setTime(toBinary(time.Hour), toBinary(time.Minute), toBinary(time.Second));
- }
- }
- }
C#-Quellcode
- using System;
- using System.Windows;
- using System.Windows.Threading;
- namespace BinaerUhrMVVM.ViewModel
- {
- internal class UhrModel
- {
- public UhrModel()
- {
- _time = new ZeitSpeicher();
- timer = new DispatcherTimer();
- timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
- timer.Tick += timer_Tick;
- timer.Start();
- }
- private ZeitSpeicher _time;
- public ZeitSpeicher time
- {
- get { return _time; }
- }
- private DispatcherTimer timer;
- void timer_Tick(object sender, EventArgs e)
- {
- _time.act(DateTime.Now);
- }
- }
- }
XML-Quellcode
- <Window x:Class="BinaerUhrMVVM.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:vm="clr-namespace:BinaerUhrMVVM.ViewModel"
- xmlns:cnv="clr-namespace:BinaerUhrMVVM.Converters"
- Title="Binär-Uhr" Height="220" Width="140" KeyboardNavigation.TabNavigation="None" MinWidth="140" MinHeight="220" WindowStyle="ToolWindow" ResizeMode="NoResize" >
- <Window.Resources>
- <vm:UhrModel x:Key="uhr" />
- <cnv:Converter x:Key="boolToBrushConverter" />
- </Window.Resources>
- <StackPanel Orientation="Vertical" DataContext="{StaticResource uhr}" Width="110">
- <Grid Height="25">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="25" />
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <TextBlock Grid.Column="1" Text="h" TextAlignment="Center" VerticalAlignment="Bottom"/>
- <TextBlock Grid.Column="2" Text="m" TextAlignment="Center" VerticalAlignment="Bottom"/>
- <TextBlock Grid.Column="3" Text="s" TextAlignment="Center" VerticalAlignment="Bottom"/>
- </Grid>
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="25" />
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <ItemsControl Background="{x:Null}" BorderBrush="{x:Null}">
- <Label Content="1" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Height="25"/>
- <Label Content="2" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Height="25"/>
- <Label Content="4" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Height="25"/>
- <Label Content="8" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Height="25"/>
- <Label Content="16" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Height="25"/>
- <Label Content="32" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Height="25"/>
- </ItemsControl>
- <ItemsControl Grid.Column="1" ItemsSource="{Binding time.H}" Background="{x:Null}" BorderBrush="{x:Null}">
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border Height="{Binding Width, RelativeSource={RelativeSource Self}}" Width="25" HorizontalAlignment="Stretch" Padding="4">
- <Border Background="{Binding BindsDirectlyToSource=True, Converter={StaticResource boolToBrushConverter}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
- </Border>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- <ItemsControl Grid.Column="2" ItemsSource="{Binding time.M}" Background="{x:Null}" BorderBrush="{x:Null}">
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border Height="{Binding Width, RelativeSource={RelativeSource Self}}" Width="25" HorizontalAlignment="Stretch" Padding="4">
- <Border Background="{Binding BindsDirectlyToSource=True, Converter={StaticResource boolToBrushConverter}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
- </Border>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- <ItemsControl Grid.Column="3" ItemsSource="{Binding time.S}" Background="{x:Null}" BorderBrush="{x:Null}">
- <ItemsControl.ItemTemplate>
- <DataTemplate>
- <Border Height="{Binding Width, RelativeSource={RelativeSource Self}}" Width="25" HorizontalAlignment="Stretch" Padding="4">
- <Border Background="{Binding BindsDirectlyToSource=True, Converter={StaticResource boolToBrushConverter}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
- </Border>
- </DataTemplate>
- </ItemsControl.ItemTemplate>
- </ItemsControl>
- </Grid>
- </StackPanel>
- </Window>
Screenshot:
Danke an alle, die hier ihre Kritik abgegeben haben.
Besonderen Dank an @ErfinderDesRades
Viel Spaß mit dem Code, die Projektmappe findet ihr im Anhang.
Fenster (natürlich XAML, nicht XML ) :
XML-Quellcode
- <Window x:Class="Binäruhr.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Binär Uhr" Height="350" Width="192" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
- <Grid>
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="*" />
- <ColumnDefinition Width="30" />
- <ColumnDefinition Width="10" />
- <ColumnDefinition Width="30" />
- <ColumnDefinition Width="10" />
- <ColumnDefinition Width="30" />
- <ColumnDefinition Width="*" />
- </Grid.ColumnDefinitions>
- <Grid.RowDefinitions>
- <RowDefinition Height="*" />
- <RowDefinition Height="30" />
- <RowDefinition Height="10" />
- <RowDefinition Height="30" />
- <RowDefinition Height="10" />
- <RowDefinition Height="30" />
- <RowDefinition Height="10" />
- <RowDefinition Height="30" />
- <RowDefinition Height="10" />
- <RowDefinition Height="30" />
- <RowDefinition Height="10" />
- <RowDefinition Height="30" />
- <RowDefinition Height="10" />
- <RowDefinition Height="30" />
- <RowDefinition Height="2*" />
- </Grid.RowDefinitions>
- <TextBlock Text="h:" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
- <TextBlock Text="m:" Grid.Column="3" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
- <TextBlock Name="s_txt" Text="s:" Grid.Column="5" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
- <TextBlock Text="1:" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="3" />
- <TextBlock Text="2:" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="5" />
- <TextBlock Text="4:" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="7" />
- <TextBlock Text="8:" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="9" />
- <TextBlock Text="16:" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="11" />
- <TextBlock Text="32:" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="13" />
- <Border Background="Blue" Margin="5" Grid.Column="1" Grid.Row="3" Name="h1" />
- <Border Background="Blue" Margin="5" Grid.Column="1" Grid.Row="5" Name="h2" />
- <Border Background="Blue" Margin="5" Grid.Column="1" Grid.Row="7" Name="h4" />
- <Border Background="Blue" Margin="5" Grid.Column="1" Grid.Row="9" Name="h8" />
- <Border Background="Blue" Margin="5" Grid.Column="1" Grid.Row="11" Name="h16" />
- <Border Background="Blue" Margin="5" Grid.Column="3" Grid.Row="3" Name="m1" />
- <Border Background="Blue" Margin="5" Grid.Column="3" Grid.Row="5" Name="m2" />
- <Border Background="Blue" Margin="5" Grid.Column="3" Grid.Row="7" Name="m4" />
- <Border Background="Blue" Margin="5" Grid.Column="3" Grid.Row="9" Name="m8" />
- <Border Background="Blue" Margin="5" Grid.Column="3" Grid.Row="11" Name="m16" />
- <Border Background="Blue" Margin="5" Grid.Column="3" Grid.Row="13" Name="m32" />
- <Border Background="Blue" Margin="5" Grid.Column="5" Grid.Row="3" Name="s1" />
- <Border Background="Blue" Margin="5" Grid.Column="5" Grid.Row="5" Name="s2" />
- <Border Background="Blue" Margin="5" Grid.Column="5" Grid.Row="7" Name="s4" />
- <Border Background="Blue" Margin="5" Grid.Column="5" Grid.Row="9" Name="s8" />
- <Border Background="Blue" Margin="5" Grid.Column="5" Grid.Row="11" Name="s16" />
- <Border Background="Blue" Margin="5" Grid.Column="5" Grid.Row="13" Name="s32" />
- </Grid>
- </Window>
C#-Quellcode
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Windows.Threading;
- namespace Binäruhr
- {
- public partial class MainWindow : Window
- {
- DispatcherTimer t;
- Color colorOn = Colors.Red;
- Color colorOff = Colors.LightGray;
- public MainWindow()
- {
- InitializeComponent();
- t = new DispatcherTimer();
- t.Interval = new TimeSpan(0, 0, 0, 0, 100);
- t.Tick += t_Tick;
- t.Start();
- initField();
- }
- public void initField()
- {
- for(int x = 0; x < 3; x++)
- {
- String temp = "";
- if (x == 0)
- temp = "s";
- if (x == 1)
- temp = "m";
- if (x == 2)
- temp = "h";
- for(int y = 0; y < 32; y++)
- {
- if(temp != "h")
- {
- setBit(temp + (y+1).ToString(), false);
- }
- else
- {
- if(y < 30)
- setBit(temp + (y+1).ToString(), false);
- }
- }
- }
- }
- public void setBit(String bit, bool status)
- {
- char c = bit[0];
- String temp = bit.Substring(1, bit.Length - 1);
- int i = Int32.Parse(temp);
- SolidColorBrush brusch = new SolidColorBrush(status == true ? colorOn : colorOff);
- switch(c)
- {
- case 's':
- {
- switch(i)
- {
- case 1:
- {
- s1.Background = brusch;
- break;
- }
- case 2:
- {
- s2.Background = brusch;
- break;
- }
- case 4:
- {
- s4.Background = brusch;
- break;
- }
- case 8:
- {
- s8.Background = brusch;
- break;
- }
- case 16:
- {
- s16.Background = brusch;
- break;
- }
- case 32:
- {
- s32.Background = brusch;
- break;
- }
- }
- break;
- }
- case 'm':
- {
- switch (i)
- {
- case 1:
- {
- m1.Background = brusch;
- break;
- }
- case 2:
- {
- m2.Background = brusch;
- break;
- }
- case 4:
- {
- m4.Background = brusch;
- break;
- }
- case 8:
- {
- m8.Background = brusch;
- break;
- }
- case 16:
- {
- m16.Background = brusch;
- break;
- }
- case 32:
- {
- m32.Background = brusch;
- break;
- }
- }
- break;
- }
- case 'h':
- {
- switch (i)
- {
- case 1:
- {
- h1.Background = brusch;
- break;
- }
- case 2:
- {
- h2.Background = brusch;
- break;
- }
- case 4:
- {
- h4.Background = brusch;
- break;
- }
- case 8:
- {
- h8.Background = brusch;
- break;
- }
- case 16:
- {
- h16.Background = brusch;
- break;
- }
- }
- break;
- }
- }
- }
- public void setTime()
- {
- int temp2 = DateTime.Now.Second;
- String tempS = Convert.ToString(DateTime.Now.Second, 2);
- String tempM = Convert.ToString(DateTime.Now.Minute, 2);
- String tempH = Convert.ToString(DateTime.Now.Hour, 2);
- String s = "";
- String m = "";
- String h = "";
- int x = 0;
- for (int i = (tempS.Length - 1); i >= 0; i--)
- {
- s += tempS[i].ToString();
- }
- x = 6 - (int)s.Length;
- for (int i = 0; i < x; i++)
- {
- s += "0";
- }
- for(int i= (tempM.Length -1); i >= 0; i--)
- {
- m += tempM[i].ToString();
- }
- x = 6 - (int)m.Length;
- for (int i = 0; i < x; i++)
- {
- m += "0";
- }
- for (int i = (tempH.Length - 1); i >= 0; i--)
- {
- h += tempH[i].ToString();
- }
- x = 5 - h.Length;
- for (int i = 0; i < x; i++)
- {
- h += "0";
- }
- setBit("s1", s[0] == '1' ? true : false);
- setBit("s2", s[1] == '1' ? true : false);
- setBit("s4", s[2] == '1' ? true : false);
- setBit("s8", s[3] == '1' ? true : false);
- setBit("s16", s[4] == '1' ? true : false);
- setBit("s32", s[5] == '1' ? true : false);
- setBit("m1", m[0] == '1' ? true : false);
- setBit("m2", m[1] == '1' ? true : false);
- setBit("m4", m[2] == '1' ? true : false);
- setBit("m8", m[3] == '1' ? true : false);
- setBit("m16", m[4] == '1' ? true : false);
- setBit("m32", m[5] == '1' ? true : false);
- setBit("h1", h[0] == '1' ? true : false);
- setBit("h2", h[1] == '1' ? true : false);
- setBit("h4", h[2] == '1' ? true : false);
- setBit("h8", h[3] == '1' ? true : false);
- setBit("h16", h[4] == '1' ? true : false);
- }
- void t_Tick(object sender, EventArgs e)
- {
- setTime();
- }
- }
- }
Und so wie sich's gehört, ein Bildschirmschuss :
Mit freundlichen Grüßen
nxtman
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „nxtman“ () aus folgendem Grund: "Finalen" Code eingefügt