瀏覽代碼

Use enum items array instead of strings in control catalog settings

Max Katz 4 年之前
父節點
當前提交
ec23173a0a

+ 13 - 8
samples/ControlCatalog/MainView.xaml

@@ -2,7 +2,8 @@
              xmlns="https://github.com/avaloniaui"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:controls="clr-namespace:ControlSamples;assembly=ControlSamples"
-             xmlns:pages="clr-namespace:ControlCatalog.Pages">
+             xmlns:pages="clr-namespace:ControlCatalog.Pages"
+             xmlns:models="clr-namespace:ControlCatalog.Models">
   <Grid>
     <Grid.Styles>
       <Style Selector="TextBlock.h2">
@@ -156,17 +157,21 @@
             <ComboBox x:Name="Decorations"
                       HorizontalAlignment="Stretch"
                       SelectedIndex="0">
-              <ComboBoxItem>No Decorations</ComboBoxItem>
-              <ComboBoxItem>Border Only</ComboBoxItem>
-              <ComboBoxItem>Full Decorations</ComboBoxItem>
+              <ComboBox.Items>
+                <SystemDecorations>None</SystemDecorations>
+                <SystemDecorations>BorderOnly</SystemDecorations>
+                <SystemDecorations>Full</SystemDecorations>
+              </ComboBox.Items>
             </ComboBox>
             <ComboBox x:Name="Themes"
                       HorizontalAlignment="Stretch"
                       SelectedIndex="0">
-              <ComboBoxItem>Fluent - Light</ComboBoxItem>
-              <ComboBoxItem>Fluent - Dark</ComboBoxItem>
-              <ComboBoxItem>Simple - Light</ComboBoxItem>
-              <ComboBoxItem>Simple - Dark</ComboBoxItem>
+              <ComboBox.Items>
+                <models:CatalogTheme>FluentLight</models:CatalogTheme>
+                <models:CatalogTheme>FluentDark</models:CatalogTheme>
+                <models:CatalogTheme>DefaultLight</models:CatalogTheme>
+                <models:CatalogTheme>DefaultDark</models:CatalogTheme>
+              </ComboBox.Items>
             </ComboBox>
             <ComboBox x:Name="TransparencyLevels"
                       HorizontalAlignment="Stretch"

+ 16 - 16
samples/ControlCatalog/MainView.xaml.cs

@@ -10,6 +10,7 @@ using Avalonia.Media;
 using Avalonia.Media.Immutable;
 using Avalonia.Platform;
 using ControlCatalog.Pages;
+using ControlCatalog.Models;
 
 namespace ControlCatalog
 {
@@ -40,28 +41,27 @@ namespace ControlCatalog
             var themes = this.Find<ComboBox>("Themes");
             themes.SelectionChanged += (sender, e) =>
             {
-                switch (themes.SelectedIndex)
+                if (themes.SelectedItem is CatalogTheme theme)
                 {
-                    case 0:
-                        Application.Current.Styles[0] = App.FluentLight;
-                        break;
-                    case 1:
-                        Application.Current.Styles[0] = App.FluentDark;
-                        break;
-                    case 2:
-                        Application.Current.Styles[0] = App.DefaultLight;
-                        break;
-                    case 3:
-                        Application.Current.Styles[0] = App.DefaultDark;
-                        break;
+                    Application.Current.Styles[0] = theme switch
+                    {
+                        CatalogTheme.FluentLight => App.FluentLight,
+                        CatalogTheme.FluentDark => App.FluentDark,
+                        CatalogTheme.DefaultLight => App.DefaultLight,
+                        CatalogTheme.DefaultDark => App.DefaultDark,
+                        _ => Application.Current.Styles[0]
+                    };
                 }
-            };            
+            };
 
             var decorations = this.Find<ComboBox>("Decorations");
             decorations.SelectionChanged += (sender, e) =>
             {
-                if (VisualRoot is Window window)
-                    window.SystemDecorations = (SystemDecorations)decorations.SelectedIndex;
+                if (VisualRoot is Window window
+                    && decorations.SelectedItem is SystemDecorations systemDecorations)
+                {
+                    window.SystemDecorations = systemDecorations;
+                }
             };
 
             var transparencyLevels = this.Find<ComboBox>("TransparencyLevels");

+ 14 - 0
samples/ControlCatalog/Models/CatalogTheme.cs

@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace ControlCatalog.Models
+{
+    public enum CatalogTheme
+    {
+        FluentLight,
+        FluentDark,
+        DefaultLight,
+        DefaultDark
+    }
+}