using System; using System.Globalization; using System.Linq; using Avalonia; using Avalonia.Controls; using Avalonia.Data.Converters; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.VisualTree; using Project_Periodensystem.Persistence; using Project_Periodensystem.View.Converters; namespace Project_Periodensystem.View { // Converter to map element series to a color public class SeriesToColorConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return new SolidColorBrush(Colors.Gray); return value.ToString() switch { "Nichtmetall" => new SolidColorBrush(Color.Parse("#3e6418")), "Metall" => new SolidColorBrush(Color.Parse("#711019")), "Post-Übergangsmetall" => new SolidColorBrush(Color.Parse("#555555")), "Halbmetall" => new SolidColorBrush(Color.Parse("#015146")), "Edelgas" => new SolidColorBrush(Color.Parse("#3a2151")), "Halogen" => new SolidColorBrush(Color.Parse("#846011")), "Alkalimetall" => new SolidColorBrush(Color.Parse("#6c3b01")), "Erdalkalimetall" => new SolidColorBrush(Color.Parse("#846011")), "Lanthanoid" => new SolidColorBrush(Color.Parse("#402c17")), "Actinoid" => new SolidColorBrush(Color.Parse("#732e4c")), _ => new SolidColorBrush(Color.Parse("#222222")) }; } } public partial class PeriodicTablePage : UserControl { private readonly Grid? periodicGrid; public PeriodicTablePage() { InitializeComponent(); periodicGrid = this.Find("PeriodicGrid"); InitializePeriodicTable(); } private void InitializeComponent() { AvaloniaXamlLoader.Load(this); } private void InitializePeriodicTable() { try { Logger.Log("Initialisiere PeriodicTable..."); // Prüfen ob das Grid gefunden wurde if (periodicGrid == null) { Logger.Log("FEHLER: PeriodicGrid nicht gefunden!"); return; } // Prüfen ob Elemente vorhanden sind if (PeriodicTableData.Elements == null || !PeriodicTableData.Elements.Any()) { Logger.Log("FEHLER: Keine Elemente in PeriodicTableData!"); return; } var converter = new SeriesToColorConverter(); // Elemente hinzufügen foreach (var element in PeriodicTableData.Elements) { Logger.Log($"Füge Element hinzu: {element.Symbol} ({element.Row},{element.Column})"); var border = new Border { Width = 58, Height = 58, Margin = new Thickness(1), CornerRadius = new CornerRadius(4), BorderThickness = new Thickness(1), BorderBrush = new SolidColorBrush(Colors.Gray), Background = converter.Convert(element.Series, typeof(IBrush), null!, CultureInfo.CurrentCulture) as IBrush // Hier wird die Farbe gesetzt }; var stackPanel = new StackPanel(); // Symbol stackPanel.Children.Add(new TextBlock { Text = element.Symbol, FontSize = 20, Foreground = new SolidColorBrush(Colors.White), HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center }); // Atomic Number stackPanel.Children.Add(new TextBlock { Text = element.AtomicNumber.ToString(), FontSize = 12, Foreground = new SolidColorBrush(Colors.White), HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center }); border.Child = stackPanel; // Position setzen Grid.SetRow(border, Math.Max(0, element.Row - 1)); Grid.SetColumn(border, Math.Max(0, element.Column - 1)); periodicGrid.Children.Add(border); Logger.Log($"Element {element.Symbol} wurde hinzugefügt"); } Logger.Log("PeriodicTable wurde initialisiert"); } catch (Exception ex) { Logger.Log($"FEHLER beim Initialisieren des PeriodicTable: {ex.Message}"); Logger.Log($"StackTrace: {ex.StackTrace}"); } } private void AboutButton_Click(object? sender, RoutedEventArgs e) { try { Logger.Log("AboutButton wurde geklickt"); var mainWindow = this.FindAncestorOfType(); if (mainWindow != null) { Logger.Log("Navigation zu About Page"); mainWindow.ShowAbout(); } else { Logger.Log("FEHLER: MainWindow nicht gefunden"); } } catch (Exception ex) { Logger.Log($"FEHLER im AboutButton_Click: {ex.Message}"); } } } }