浏览代码

changelog ui refactor

Veloe 2 年之前
父节点
当前提交
fa4b9074c2

+ 23 - 0
VeloeMinecraftLauncher/Utils/ChangelogDataTemplate.cs

@@ -0,0 +1,23 @@
+using Avalonia.Controls;
+using Avalonia.Controls.Templates;
+using Avalonia.Metadata;
+using System;
+using System.Collections.Generic;
+using VeloeMinecraftLauncher.Models.Entity;
+
+namespace VeloeMinecraftLauncher.Utils
+{
+    internal class ChangelogDataTemplate : IDataTemplate
+    {
+        [Content]
+        public Dictionary<bool, IDataTemplate> AvailableTemplates { get; } = new Dictionary<bool, IDataTemplate>();
+
+        public Control? Build(object? param)
+        {
+            ArgumentNullException.ThrowIfNull(param);
+            return AvailableTemplates[string.IsNullOrEmpty(((Changelog)param).Title)].Build(param);
+        }
+
+        public bool Match(object? data) => data is Changelog changelog && AvailableTemplates.ContainsKey(string.IsNullOrEmpty(changelog.Title));
+    }
+}

+ 5 - 30
VeloeMinecraftLauncher/ViewModels/MainWindowViewModel.cs

@@ -99,38 +99,11 @@ public class MainWindowViewModel : ViewModelBase
             {
                 OpenErrorWindow(ex);
             }
+            
             try
             {
-                var changelog = await Downloader.DownloadAndDeserializeJsonData<List<Changelog>>("https://files.veloe.link/launcher/changelog.json");
-
-                if (changelog is not null &&  Avalonia.Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop && desktop.MainWindow is not null)
-                {
-#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-                    Dispatcher.UIThread.InvokeAsync(() =>
-                    {
-                        var stackpanel = desktop.MainWindow.GetControl<StackPanel>("ChangeLogStackPanel");
-                        foreach (var version in changelog)
-                        {
-                            if (version.Title is not null)
-                            {
-                                stackpanel.Children.Add(new TextBlock()
-                                {
-                                    Text = version.Title,
-                                    VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
-                                    TextWrapping = TextWrapping.Wrap,
-                                    FontSize = 16
-                                });
-                            }
-                            stackpanel.Children.Add(new TextBlock()
-                            {
-                                Text = version.Text,
-                                VerticalAlignment = Avalonia.Layout.VerticalAlignment.Stretch,
-                                TextWrapping = TextWrapping.Wrap
-                            });
-                        }
-                    });
-#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
-                }
+                Changelogs = new ObservableCollection<Changelog>(await Downloader.DownloadAndDeserializeJsonData<List<Changelog>>("https://files.veloe.link/launcher/changelog.json"));
+                this.RaisePropertyChanged(nameof(Changelogs));
             }
             catch (Exception ex)
             {
@@ -279,6 +252,8 @@ public class MainWindowViewModel : ViewModelBase
         set => this.RaiseAndSetIfChanged(ref _serverPanels, value);
     }
 
+    public ObservableCollection<Changelog>? Changelogs { get; private set; }
+
     public DownloadedVersion DownloadedVersion
     {
         get => _downloadedVersion;

+ 34 - 5
VeloeMinecraftLauncher/Views/MainWindow.axaml

@@ -2,6 +2,8 @@
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="using:VeloeMinecraftLauncher.ViewModels"
 		xmlns:m="using:VeloeMinecraftLauncher.Models"
+		xmlns:entity="using:VeloeMinecraftLauncher.Models.Entity"
+		xmlns:utils="using:VeloeMinecraftLauncher.Utils"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 		xmlns:titlebars="using:VeloeMinecraftLauncher.Views.TitleBar"
@@ -158,11 +160,38 @@
 								HorizontalScrollBarVisibility="Auto"
 								VerticalAlignment="Stretch"
 								HorizontalAlignment="Stretch">
-								<StackPanel
-									Name="ChangeLogStackPanel"
-									Width="760"
-									Margin="5,5,5,5">								
-								</StackPanel>
+								<ItemsControl ItemsSource="{Binding Changelogs}">
+									<ItemsControl.ItemsPanel>
+										<ItemsPanelTemplate>
+											<StackPanel
+												Width="760"
+												Margin="5,5,5,5"/>
+										</ItemsPanelTemplate>
+									</ItemsControl.ItemsPanel>
+									<ItemsControl.ItemTemplate>
+										<utils:ChangelogDataTemplate>
+											<DataTemplate DataType="entity:Changelog" x:Key="True">
+												<TextBlock 
+													Text="{Binding Text}"
+													VerticalAlignment="Stretch"
+													TextWrapping="Wrap"/>
+											</DataTemplate>
+											<DataTemplate DataType="entity:Changelog" x:Key="False">
+												<StackPanel>
+													<TextBlock
+														Text="{Binding Title}"
+														VerticalAlignment="Stretch"
+														TextWrapping="Wrap"
+														FontSize="16"/>
+													<TextBlock 
+														Text="{Binding Text}"
+														VerticalAlignment="Stretch"
+														TextWrapping="Wrap"/>
+												</StackPanel>
+											</DataTemplate>
+										</utils:ChangelogDataTemplate>
+									</ItemsControl.ItemTemplate>
+								</ItemsControl>
 							</ScrollViewer>
 						</Panel>
 					</TabItem>