翼度科技»论坛 编程开发 .net 查看内容

基于Avalonia 11.0.0+ReactiveUI 的跨平台项目开发2-功能开发

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
基于Avalonia 11.0.0+ReactiveUI 的跨平台项目开发2-功能开发


项目简介:目标是开发一个跨平台的AI聊天和其他功能的客户端平台。目的来学习和了解Avalonia。将这个项目部署在openKylin 1.0 的系统上。
为什么使用Avalonia:之前已经了解了基于Avalonia的项目在国产麒麟系统中运行的案例。正是Avalonia在跨平台的出色表现,学习和了解Avalonia这个UI框架显得十分有必要。本项目采用的是最新稳定版本11.0.0-rc1.1。希望通过该项目了解和学习Avalonia开发的朋友可以在我的github上拉取代码,同时希望大家多多点点star。
https://github.com/raokun/TerraMours.Chat.Ava
项目的基础框架和通用功能在上一篇博客中介绍过了,想了解的同学跳转学习:
基于Avalonia 11.0.0+ReactiveUI 的跨平台项目开发1-通用框架
了解Avalonia创建模板项目-基础可跳转:
创建Avalonia 模板项目-基础
本次我主要分享的内容是项目中具体功能开发实现的过程和各技术的应用
1.功能介绍

1.界面交互

第一版的内容主要分为以下几个模块:

  • LoadView.axaml  加载界面:系统打开时候的加载界面,用于首页替换的技术实践。可改造成登陆界面。
  • MainWindow.axaml  首页
  • MainView.axaml  主界面
  • DataGridView.axaml  会话列表
  • ChatView.axaml  聊天界面
  • ApiSettingsView.axaml  API配置
2.功能实现

下面我会按照各个模块来介绍对应的功能和实现方法。
1.加载界面

加载界面 是系统的首个加载界面,界面样式如下:

1.作用和功能:

加载界面是系统在运行前的准备界面,目前并没有做什么操作,只是做了个进度条,到100%时跳转首页。不过这是一个可扩展的实践。
加载界面完成了首页的切换的实践,为后期登录页面做好了准备。同时,加载界面的内容,改写成蒙版,在需要长时间数据处理用于限制用户操作也是不错的选择。
2.设置加载界面为项目运行时首个加载界面

设置首个加载界面,需要在App.axaml.cs中的OnFrameworkInitializationCompleted方法中设置 desktop.MainWindow

OnFrameworkInitializationCompleted代码如下:
  1. public override void OnFrameworkInitializationCompleted() {
  2.     //添加共享资源
  3.     var VMLocator = new VMLocator();
  4.     Resources.Add("VMLocator", VMLocator);
  5.     if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
  6.         var load= new LoadView {
  7.             DataContext = new LoadViewModel(),
  8.         };
  9.         desktop.MainWindow = load;
  10.         VMLocator.LoadViewModel.ToMainAction = () =>
  11.         {
  12.             desktop.MainWindow = new MainWindow();
  13.             desktop.MainWindow.Show();
  14.             load.Close();
  15.         };
  16.     }
  17.     base.OnFrameworkInitializationCompleted();
  18. }
复制代码
3.隐藏窗口的关闭按钮,并设置窗口居中显示

加载界面不应该有关闭等按钮,我们用 SystemDecorations="None"。将 SystemDecorations 属性设置为 "None" 可以隐藏窗口的系统装饰。系统装饰包括标题栏、最小化、最大化和关闭按钮等。通过设置 SystemDecorations 为 "None",可以使窗口更加定制化和个性化,同时减少了不必要的系统装饰。
界面应该显示在屏幕正中间。我们用 WindowStartupLocation="CenterScreen"。设置 WindowStartupLocation 为 "CenterScreen" 可以使窗口在屏幕上居中显示。

4.实现进度条


代码如下:
  1. <StackPanel
  2.             HorizontalAlignment="Center"
  3.             VerticalAlignment="Center"
  4.             Orientation="Vertical"
  5.             Spacing="10">
  6.     <TextBlock
  7.                Text="Loading..."
  8.                HorizontalAlignment="Center"
  9.                VerticalAlignment="Center"
  10.                Foreground="White"
  11.                Background="Transparent"/>
  12.     <ProgressBar
  13.                  x:Name="progressBar"
  14.                  HorizontalAlignment="Center"
  15.                  Minimum="0"
  16.                  Maximum="100"
  17.                  Value="{Binding Progress}"
  18.                  Width="200"
  19.                  Height="20"
  20.                  Background="Transparent">
  21.         <ProgressBar.Foreground>
  22.             <SolidColorBrush Color="White"/>
  23.         </ProgressBar.Foreground>
  24.     </ProgressBar>
  25. </StackPanel>
复制代码
进度条用到了ProgressBar 的控件,对应的官方文档地址:ProgressBar
控件的属性:
PropertyDescriptionMinimum最大值Maximum最小值Value当前值Foreground进度条颜色ShowProgressText显示进度数值Value 值通过Binding绑定了ViewModel中的属性字段Progress。通过UpdateProgress()方法,让Progress的值由0变化到100,模拟加载过程。

代码如下:
  1. private async void UpdateProgress() {
  2.     // 模拟登录加载过程
  3.     for (int i = 0; i <= 100; i++) {
  4.         Progress = i;
  5.         await Task.Delay(100); // 延迟一段时间,以模拟加载过程
  6.     }
  7.     ToMainAction?.Invoke();
  8. }
复制代码
MainWindow的构造函数绑定了多个事件的实现方法:

  • this.Loaded 界面加载时触发MainWindow_Loaded 方法,作用是加载本地数据和本地配置文件。
  • this.Closing  程序关闭时触发SaveWindowSizeAndPosition方法,作用是保存当前的系统设置,包括用户调整后的界面的长宽和在屏幕的位置,用户下次打开时程序还会出现在之前的位置。比如,我把系统拉到桌面左上角,把窗口缩小到最小尺寸时候退出程序,下次打开,程序界面还会在之前退出的位置,在桌面左上角以最小尺寸出现。
  • this.KeyDown 监听键盘的输入事件,当按键按下时,会触发MainWindow_KeyDown方法,用于绑定自定义的快捷键。
MainWindow构造函数中,通过判断CultureInfo.CurrentCulture,获取当前 操作系统的语言系统,判断程序应该显示哪个国家的语言。从而确定程序显示的语言,通过Translate 修改语言相关配置。是系统国际化的实践。
4.系统配置-本地保存和加载

系统配置 对应AppSettings类,记录了应用程序设置ChatGPT API参数

系统设置参数通过保存到文件settings.json中实现配置的本地化和持久化。
MainWindow_Loaded 的方法实现系统配置加载:


代码如下:
  1. <Window xmlns="https://github.com/avaloniaui"
  2.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.         xmlns:vm="using:TerraMours.Chat.Ava.ViewModels"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  7.         x:
  8.                 x:DataType="vm:MainWindowViewModel"
  9.                 xmlns:dialogHost="clr-namespace:DialogHostAvalonia;assembly=DialogHost.Avalonia"
  10.                 RenderOptions.BitmapInterpolationMode="HighQuality"
  11.                 xmlns:sty="using:FluentAvalonia.Styling"
  12.                 xmlns:ui="using:FluentAvalonia.UI.Controls"
  13.                 xmlns:local="using:TerraMours.Chat.Ava.Views"
  14.         Icon="/Assets/terramours.ico"
  15.         Title="TerraMours.Chat.Ava">
  16.         <dialogHost:DialogHost IsOpen="{Binding        ApiSettingIsOpened}"
  17.                                                    DialogMargin="16"
  18.                                                         DisableOpeningAnimation="True"
  19.                                                    dialogHost:DialogHostStyle.CornerRadius="8"
  20.                                                    Background="rgb(52, 53, 65)">
  21.                 <dialogHost:DialogHost.DialogContent>
  22.                         <local:ApiSettingsView />
  23.                 </dialogHost:DialogHost.DialogContent>
  24.                 <Panel>
  25.                         <local:MainView />
  26.                 </Panel>
  27.         </dialogHost:DialogHost>
  28. </Window>
复制代码
MainWindow_Loaded 的方法中,通过解析settings.json,加载系统配置。
settings.json 的解析和保存

相关方法如下:

至此,系统配置的开发就基本完成了。对于这些不需要远程同步的基础配置,保存在本地文件中。
5.国际化

通过Translate方法,根据当前系统语言,改变控制文字显示的资源文件,实现语言的切换。

代码如下:
  1. public MainWindow() {
  2.             InitializeComponent();
  3.             this.Closing += (sender, e) => SaveWindowSizeAndPosition();
  4.             this.Loaded += MainWindow_Loaded;
  5.             MainWindowViewModel = new MainWindowViewModel();
  6.             VMLocator.MainWindowViewModel = MainWindowViewModel;
  7.             DataContext = MainWindowViewModel;
  8.             var cultureInfo = CultureInfo.CurrentCulture;
  9.             if (cultureInfo.Name == "zh-CN") {
  10.                 Translate("zh-CN");
  11.             }
  12.             this.KeyDown += MainWindow_KeyDown;
  13.         }
复制代码
关于国际化的资源文件的创建请看前篇内容:基于Avalonia 11.0.0+ReactiveUI 的跨平台项目开发1-通用框架
3.主界面

主界面 是项目的核心,包括了以下图片所有内容的布局,它勾勒出了整个程序。中间包括左上角的图标,会话列表,聊天区域,查询,配置等等。

1.作用和功能

主界面 的作用,是显示和完成整个业务功能的展示和交互。主界面 将会话列表和聊天窗口左右分开。控制了整个程序的排版和布局。

主要分三块:

  • 程序标题logo
  • 会话列表
  • 聊天窗口
2.界面设计


具体代码不贴出来了,需要了解的同学可以fork项目代码查看,功能区已经标注注释了,方便查看。
3.SplitView控制会话列表显示

会话列表聊天窗口 通过SplitView 实现,会话列表在窗口缩小时自动隐藏。通过IsPaneOpen属性控制。
隐藏效果:

实现方法为OnSizeChanged方法:

代码如下:
  1. private async void MainWindow_Loaded(object sender,RoutedEventArgs e) {
  2.     var settings = await LoadAppSettingsAsync();
  3.     if (File.Exists(Path.Combine(settings.AppDataPath, "settings.json"))) {
  4.         this.Width = settings.Width - 1;
  5.         this.Position = new PixelPoint(settings.X, settings.Y);
  6.         this.Height = settings.Height;
  7.         this.Width = settings.Width;
  8.         this.WindowState = settings.IsMaximized ? WindowState.Maximized : WindowState.Normal;
  9.     }
  10.     else {
  11.         var screen = Screens.Primary;
  12.         var workingArea = screen.WorkingArea;
  13.         double dpiScaling = screen.PixelDensity;
  14.         this.Width = 1300 * dpiScaling;
  15.         this.Height = 840 * dpiScaling;
  16.         this.Position = new PixelPoint(5, 0);
  17.     }
  18.     if (!File.Exists(settings.DbPath)) {
  19.         _dbProcess.CreateDatabase();
  20.     }
  21.     await _dbProcess.DbLoadToMemoryAsync();
  22.     await VMLocator.MainViewModel.LoadPhraseItemsAsync();
  23.     VMLocator.MainViewModel.SelectedPhraseItem = settings.PhrasePreset;
  24.     VMLocator.MainViewModel.SelectedLogPain = "Chat List";
  25.     await Dispatcher.UIThread.InvokeAsync(() => { VMLocator.MainViewModel.LogPainIsOpened = false; });
  26.     if (this.Width > 1295) {
  27.         //await Task.Delay(1000);
  28.         await Dispatcher.UIThread.InvokeAsync(() => { VMLocator.MainViewModel.LogPainIsOpened = true; });
  29.     }
  30.     this.GetObservable(ClientSizeProperty).Subscribe(size => OnSizeChanged(size));
  31.     _previousWidth = ClientSize.Width;
  32.     await _dbProcess.UpdateChatLogDatabaseAsync();
  33.     await _dbProcess.CleanUpEditorLogDatabaseAsync();
  34.     if (string.IsNullOrWhiteSpace(VMLocator.MainWindowViewModel.ApiKey)) {
  35.         var dialog = new ContentDialog() { Title = $"Please enter your API key.", PrimaryButtonText = "OK" };
  36.         await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);
  37.         VMLocator.ChatViewModel.OpenApiSettings();
  38.     }
  39. }
复制代码
当窗口宽度小于1295,会修改VMLocator.MainViewModel.LogPainButtonIsVisible为false,实现会话列表隐藏的效果。
4.初始化

MainViewModel控制了程序大部分的按键的事件实现,MainViewModel的构造函数如下:

代码如下:
  1. public void Translate(string targetLanguage) {
  2.     var translations = App.Current.Resources.MergedDictionaries.OfType<ResourceInclude>().FirstOrDefault(x => x.Source?.OriginalString?.Contains("/Lang/") ?? false);
  3.     if (translations != null)
  4.         App.Current.Resources.MergedDictionaries.Remove(translations);
  5.     App.Current.Resources.MergedDictionaries.Add(
  6.         (ResourceDictionary)AvaloniaXamlLoader.Load(
  7.             new Uri($"avares://TerraMours.Chat.Ava/Assets/lang/{targetLanguage}.axaml")
  8.         )
  9.     );
  10. }
复制代码
其中,绑定了会话、配置、聊天等功能的按钮事件。实现业务的交互。
5.调用ChatGpt接口

通过Betalgo.OpenAI 完成接口调用,是一个开源的nuget包,集成了OpenAI的接口,简化了调用逻辑。
本来更倾向于Senmantic Kernel的,是微软开发的LLM训练框架,但是代理方面我还没有很好的解决办法,后面再替换。
接口调用方法写在PostChatAsync方法里,通过post按钮发起调用:

代码如下:
  1. /// /// OpenAI 调用方法/// /// private async Task PostChatAsync(){    try    {        string message = PostMessage;        int conversationId = 1;        //创建会话        if(VMLocator.DataGridViewModel.ChatList == null)        {            VMLocator.DataGridViewModel.ChatList=new ObservableCollection ();            VMLocator.DataGridViewModel.ChatList.Add(new ChatList() { Id=1,Title=(message.Length< 5?message:$"{message.Substring(0,5)}..."), Category = (message.Length < 5 ? message : $"{message.Substring(0, 5)}...") ,Date=DateTime.Now});        }        if (VMLocator.ChatViewModel.ChatHistory == null)            VMLocator.ChatViewModel.ChatHistory = new ObservableCollection();        VMLocator.ChatViewModel.ChatHistory.Add(new Models.ChatMessage() { ChatRecordId = 1, ConversationId = conversationId, Message = message, Role = "User", CreateDate = DateTime.Now });        //根据配置中的CONTEXT_COUNT 查询上下文        var messages = new List();        messages.Add(OpenAI.ObjectModels.RequestModels.ChatMessage.FromUser(message));        var openAiOpetions = new OpenAI.OpenAiOptions()        {            ApiKey = AppSettings.Instance.ApiKey,            BaseDomain = AppSettings.Instance.ApiUrl        };        var openAiService = new OpenAIService(openAiOpetions);        //调用SDK        var response = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest<StackPanel
  2.             HorizontalAlignment="Center"
  3.             VerticalAlignment="Center"
  4.             Orientation="Vertical"
  5.             Spacing="10">
  6.     <TextBlock
  7.                Text="Loading..."
  8.                HorizontalAlignment="Center"
  9.                VerticalAlignment="Center"
  10.                Foreground="White"
  11.                Background="Transparent"/>
  12.     <ProgressBar
  13.                  x:Name="progressBar"
  14.                  HorizontalAlignment="Center"
  15.                  Minimum="0"
  16.                  Maximum="100"
  17.                  Value="{Binding Progress}"
  18.                  Width="200"
  19.                  Height="20"
  20.                  Background="Transparent">
  21.         <ProgressBar.Foreground>
  22.             <SolidColorBrush Color="White"/>
  23.         </ProgressBar.Foreground>
  24.     </ProgressBar>
  25. </StackPanel>                                   {<StackPanel
  26.             HorizontalAlignment="Center"
  27.             VerticalAlignment="Center"
  28.             Orientation="Vertical"
  29.             Spacing="10">
  30.     <TextBlock
  31.                Text="Loading..."
  32.                HorizontalAlignment="Center"
  33.                VerticalAlignment="Center"
  34.                Foreground="White"
  35.                Background="Transparent"/>
  36.     <ProgressBar
  37.                  x:Name="progressBar"
  38.                  HorizontalAlignment="Center"
  39.                  Minimum="0"
  40.                  Maximum="100"
  41.                  Value="{Binding Progress}"
  42.                  Width="200"
  43.                  Height="20"
  44.                  Background="Transparent">
  45.         <ProgressBar.Foreground>
  46.             <SolidColorBrush Color="White"/>
  47.         </ProgressBar.Foreground>
  48.     </ProgressBar>
  49. </StackPanel>                                       Messages = messages,<StackPanel
  50.             HorizontalAlignment="Center"
  51.             VerticalAlignment="Center"
  52.             Orientation="Vertical"
  53.             Spacing="10">
  54.     <TextBlock
  55.                Text="Loading..."
  56.                HorizontalAlignment="Center"
  57.                VerticalAlignment="Center"
  58.                Foreground="White"
  59.                Background="Transparent"/>
  60.     <ProgressBar
  61.                  x:Name="progressBar"
  62.                  HorizontalAlignment="Center"
  63.                  Minimum="0"
  64.                  Maximum="100"
  65.                  Value="{Binding Progress}"
  66.                  Width="200"
  67.                  Height="20"
  68.                  Background="Transparent">
  69.         <ProgressBar.Foreground>
  70.             <SolidColorBrush Color="White"/>
  71.         </ProgressBar.Foreground>
  72.     </ProgressBar>
  73. </StackPanel>                                       Model = AppSettings.Instance.ApiModel,<StackPanel
  74.             HorizontalAlignment="Center"
  75.             VerticalAlignment="Center"
  76.             Orientation="Vertical"
  77.             Spacing="10">
  78.     <TextBlock
  79.                Text="Loading..."
  80.                HorizontalAlignment="Center"
  81.                VerticalAlignment="Center"
  82.                Foreground="White"
  83.                Background="Transparent"/>
  84.     <ProgressBar
  85.                  x:Name="progressBar"
  86.                  HorizontalAlignment="Center"
  87.                  Minimum="0"
  88.                  Maximum="100"
  89.                  Value="{Binding Progress}"
  90.                  Width="200"
  91.                  Height="20"
  92.                  Background="Transparent">
  93.         <ProgressBar.Foreground>
  94.             <SolidColorBrush Color="White"/>
  95.         </ProgressBar.Foreground>
  96.     </ProgressBar>
  97. </StackPanel>                                       MaxTokens = AppSettings.Instance.ApiMaxTokens,<StackPanel
  98.             HorizontalAlignment="Center"
  99.             VerticalAlignment="Center"
  100.             Orientation="Vertical"
  101.             Spacing="10">
  102.     <TextBlock
  103.                Text="Loading..."
  104.                HorizontalAlignment="Center"
  105.                VerticalAlignment="Center"
  106.                Foreground="White"
  107.                Background="Transparent"/>
  108.     <ProgressBar
  109.                  x:Name="progressBar"
  110.                  HorizontalAlignment="Center"
  111.                  Minimum="0"
  112.                  Maximum="100"
  113.                  Value="{Binding Progress}"
  114.                  Width="200"
  115.                  Height="20"
  116.                  Background="Transparent">
  117.         <ProgressBar.Foreground>
  118.             <SolidColorBrush Color="White"/>
  119.         </ProgressBar.Foreground>
  120.     </ProgressBar>
  121. </StackPanel>                                   });        if (response == null)        {            var dialog = new ContentDialog()            {                Title = "接口调用失败",                PrimaryButtonText = "Ok"            };            await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);        }        if (!response.Successful)        {            var dialog = new ContentDialog()            {                Title = $"接口调用失败,报错内容: {response.Error.Message}",                PrimaryButtonText = "Ok"            };            await VMLocator.MainViewModel.ContentDialogShowAsync(dialog);        }        VMLocator.ChatViewModel.ChatHistory.Add(new Models.ChatMessage() { ChatRecordId = 2, ConversationId = conversationId, Message = response.Choices.FirstOrDefault().Message.Content, Role = "Assistant", CreateDate = DateTime.Now });        VMLocator.MainViewModel.PostMessage = "";    }    catch (Exception e)    {    }}
复制代码
通过创建OpenAIService初始化,Completion接口调用时使用openAiService.ChatCompletion.CreateCompletion方法。
ChatMessage是上下文的模型,通过创建messages完成上下文的创建,请求参数都写在ChatCompletionCreateRequest之中。
目前的第一版使用的CreateCompletion是直接返回的结果。后面我会优化调用,使用Stream流式输出。
4.会话列表

会话列表是模拟chatgpt官网的样式,将聊天按会话的形式归类。chatgpt官网截图如下:

1.作用和功能

会话列表将聊天按会话的形式归类,更好的管理聊天内容。
2.界面设计

因为考虑到后面会有其他类型的AI 类型,决定通过DataGrid实现会话列表,DataGrid的表格类型也能更多的展示数据。

代码如下:
  1. public MainViewModel() {
  2.      PostButtonText = "Post";
  3.      LoadChatListCommand = ReactiveCommand.CreateFromTask<string>(async (keyword) => await LoadChatListAsync(keyword));
  4.      PhrasePresetsItems = new ObservableCollection<string>();
  5.      //会话
  6.      ImportChatLogCommand = ReactiveCommand.CreateFromTask(ImportChatLogAsync);
  7.      ExportChatLogCommand = ReactiveCommand.CreateFromTask(ExportChatLogAsync);
  8.      DeleteChatLogCommand = ReactiveCommand.CreateFromTask(DeleteChatLogAsync);
  9.      //配置
  10.      SystemMessageCommand = ReactiveCommand.Create(InsertSystemMessage);
  11.      HotKeyDisplayCommand = ReactiveCommand.CreateFromTask(HotKeyDisplayAsync);
  12.      OpenApiSettingsCommand = ReactiveCommand.Create(OpenApiSettings);
  13.      ShowDatabaseSettingsCommand = ReactiveCommand.CreateFromTask(ShowDatabaseSettingsAsync);
  14.      //聊天
  15.      PostCommand = ReactiveCommand.CreateFromTask(PostChatAsync);
  16. }
复制代码
会话列表的数据共有三个:Id,创建时间,会话标题。通过DataGridTextColumn 通过绑定的形式实现。
其中,使用了CustomDateConverter 时间转换器,将Date的显示格式做转换。
3.数据交互

会话列表目前还在优化,第一版是通过第一次调用PostChatAsync时创建的。目前的数据存在本地SQLite数据库中。
5.聊天窗口

聊天窗口是程序的工作重心,是展示聊天成果的重要界面。其中用到Markdown.Avalonia的扩展包实现Markdown内容的展示。

1.作用和功能

数据是核心,聊天窗口是数据的展示平台,作用不容小嘘。通过编写数据模板DataTemplate来控制内容的展示。呈现chat问答式的结果。
2.Markdown 风格样式

通过DataTemplate来设置Markdown 风格样式。代码如下:
  1. <StackPanel
  2.             HorizontalAlignment="Center"
  3.             VerticalAlignment="Center"
  4.             Orientation="Vertical"
  5.             Spacing="10">
  6.     <TextBlock
  7.                Text="Loading..."
  8.                HorizontalAlignment="Center"
  9.                VerticalAlignment="Center"
  10.                Foreground="White"
  11.                Background="Transparent"/>
  12.     <ProgressBar
  13.                  x:Name="progressBar"
  14.                  HorizontalAlignment="Center"
  15.                  Minimum="0"
  16.                  Maximum="100"
  17.                  Value="{Binding Progress}"
  18.                  Width="200"
  19.                  Height="20"
  20.                  Background="Transparent">
  21.         <ProgressBar.Foreground>
  22.             <SolidColorBrush Color="White"/>
  23.         </ProgressBar.Foreground>
  24.     </ProgressBar>
  25. </StackPanel><StackPanel
  26.             HorizontalAlignment="Center"
  27.             VerticalAlignment="Center"
  28.             Orientation="Vertical"
  29.             Spacing="10">
  30.     <TextBlock
  31.                Text="Loading..."
  32.                HorizontalAlignment="Center"
  33.                VerticalAlignment="Center"
  34.                Foreground="White"
  35.                Background="Transparent"/>
  36.     <ProgressBar
  37.                  x:Name="progressBar"
  38.                  HorizontalAlignment="Center"
  39.                  Minimum="0"
  40.                  Maximum="100"
  41.                  Value="{Binding Progress}"
  42.                  Width="200"
  43.                  Height="20"
  44.                  Background="Transparent">
  45.         <ProgressBar.Foreground>
  46.             <SolidColorBrush Color="White"/>
  47.         </ProgressBar.Foreground>
  48.     </ProgressBar>
  49. </StackPanel><StackPanel
  50.             HorizontalAlignment="Center"
  51.             VerticalAlignment="Center"
  52.             Orientation="Vertical"
  53.             Spacing="10">
  54.     <TextBlock
  55.                Text="Loading..."
  56.                HorizontalAlignment="Center"
  57.                VerticalAlignment="Center"
  58.                Foreground="White"
  59.                Background="Transparent"/>
  60.     <ProgressBar
  61.                  x:Name="progressBar"
  62.                  HorizontalAlignment="Center"
  63.                  Minimum="0"
  64.                  Maximum="100"
  65.                  Value="{Binding Progress}"
  66.                  Width="200"
  67.                  Height="20"
  68.                  Background="Transparent">
  69.         <ProgressBar.Foreground>
  70.             <SolidColorBrush Color="White"/>
  71.         </ProgressBar.Foreground>
  72.     </ProgressBar>
  73. </StackPanel><StackPanel
  74.             HorizontalAlignment="Center"
  75.             VerticalAlignment="Center"
  76.             Orientation="Vertical"
  77.             Spacing="10">
  78.     <TextBlock
  79.                Text="Loading..."
  80.                HorizontalAlignment="Center"
  81.                VerticalAlignment="Center"
  82.                Foreground="White"
  83.                Background="Transparent"/>
  84.     <ProgressBar
  85.                  x:Name="progressBar"
  86.                  HorizontalAlignment="Center"
  87.                  Minimum="0"
  88.                  Maximum="100"
  89.                  Value="{Binding Progress}"
  90.                  Width="200"
  91.                  Height="20"
  92.                  Background="Transparent">
  93.         <ProgressBar.Foreground>
  94.             <SolidColorBrush Color="White"/>
  95.         </ProgressBar.Foreground>
  96.     </ProgressBar>
  97. </StackPanel>            编辑<StackPanel
  98.             HorizontalAlignment="Center"
  99.             VerticalAlignment="Center"
  100.             Orientation="Vertical"
  101.             Spacing="10">
  102.     <TextBlock
  103.                Text="Loading..."
  104.                HorizontalAlignment="Center"
  105.                VerticalAlignment="Center"
  106.                Foreground="White"
  107.                Background="Transparent"/>
  108.     <ProgressBar
  109.                  x:Name="progressBar"
  110.                  HorizontalAlignment="Center"
  111.                  Minimum="0"
  112.                  Maximum="100"
  113.                  Value="{Binding Progress}"
  114.                  Width="200"
  115.                  Height="20"
  116.                  Background="Transparent">
  117.         <ProgressBar.Foreground>
  118.             <SolidColorBrush Color="White"/>
  119.         </ProgressBar.Foreground>
  120.     </ProgressBar>
  121. </StackPanel><StackPanel
  122.             HorizontalAlignment="Center"
  123.             VerticalAlignment="Center"
  124.             Orientation="Vertical"
  125.             Spacing="10">
  126.     <TextBlock
  127.                Text="Loading..."
  128.                HorizontalAlignment="Center"
  129.                VerticalAlignment="Center"
  130.                Foreground="White"
  131.                Background="Transparent"/>
  132.     <ProgressBar
  133.                  x:Name="progressBar"
  134.                  HorizontalAlignment="Center"
  135.                  Minimum="0"
  136.                  Maximum="100"
  137.                  Value="{Binding Progress}"
  138.                  Width="200"
  139.                  Height="20"
  140.                  Background="Transparent">
  141.         <ProgressBar.Foreground>
  142.             <SolidColorBrush Color="White"/>
  143.         </ProgressBar.Foreground>
  144.     </ProgressBar>
  145. </StackPanel><StackPanel
  146.             HorizontalAlignment="Center"
  147.             VerticalAlignment="Center"
  148.             Orientation="Vertical"
  149.             Spacing="10">
  150.     <TextBlock
  151.                Text="Loading..."
  152.                HorizontalAlignment="Center"
  153.                VerticalAlignment="Center"
  154.                Foreground="White"
  155.                Background="Transparent"/>
  156.     <ProgressBar
  157.                  x:Name="progressBar"
  158.                  HorizontalAlignment="Center"
  159.                  Minimum="0"
  160.                  Maximum="100"
  161.                  Value="{Binding Progress}"
  162.                  Width="200"
  163.                  Height="20"
  164.                  Background="Transparent">
  165.         <ProgressBar.Foreground>
  166.             <SolidColorBrush Color="White"/>
  167.         </ProgressBar.Foreground>
  168.     </ProgressBar>
  169. </StackPanel><StackPanel
  170.             HorizontalAlignment="Center"
  171.             VerticalAlignment="Center"
  172.             Orientation="Vertical"
  173.             Spacing="10">
  174.     <TextBlock
  175.                Text="Loading..."
  176.                HorizontalAlignment="Center"
  177.                VerticalAlignment="Center"
  178.                Foreground="White"
  179.                Background="Transparent"/>
  180.     <ProgressBar
  181.                  x:Name="progressBar"
  182.                  HorizontalAlignment="Center"
  183.                  Minimum="0"
  184.                  Maximum="100"
  185.                  Value="{Binding Progress}"
  186.                  Width="200"
  187.                  Height="20"
  188.                  Background="Transparent">
  189.         <ProgressBar.Foreground>
  190.             <SolidColorBrush Color="White"/>
  191.         </ProgressBar.Foreground>
  192.     </ProgressBar>
  193. </StackPanel>                    
复制代码
MarkdownScrollViewer.Styles 根据不同的内容设置不同的样式。
MarkdownScrollViewer.ContextMenu设置右键菜单。
其中通过ChatBackgroundConverter转换器根据角色控制背景,ChatBackgroundConverter代码如下:

3.总结和待办事项

avalonia开发目前网上,特别是国内的网站的教程和文章很少,希望能给大家一点学习使用avalonia开发客户端项目的朋友一点帮助。写的不对的地方也恳请大家多多留言,我会及时更正,多多交流心得体会。
Todo:

  • 项目发布,在多平台下的运行
  • 搭建国产系统虚拟机测试avalonia项目
  • 程序改造成云同步版本,跟我做的web项目互通。
  • 优化UI界面
  • 优化语言国际化内容
**目前程序还没有完全开发完成。后续的开发我会及时跟进。阅读如遇样式问题,请前往个人博客浏览:https://www.raokun.top
目前web端ChatGPT:https://ai.terramours.site
当前开源项目地址:https://github.com/raokun/TerraMours.Chat.Ava

来源:https://www.cnblogs.com/raok/archive/2023/07/19/17566752.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具