重写设置窗口

This commit is contained in:
yangwx
2025-03-13 01:06:32 +08:00
parent ca08aa615c
commit 296d450496
8 changed files with 160 additions and 95 deletions

View File

@@ -0,0 +1 @@


View File

@@ -6,6 +6,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:behaviors="clr-namespace:YwxApp.AiChat.Commands"
xmlns:ViewModels="clr-namespace:YwxApp.AiChat.ViewModels"
xmlns:ValueConverter="clr-namespace:YwxApp.AiChat.ValueConverter"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="ChatAI" Height="600" Width="800" MinHeight="600" MinWidth="800">
@@ -14,75 +15,74 @@
</Window.DataContext>
<Window.Resources>
<ResourceDictionary>
<ValueConverter:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Views/Style/ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<i:Interaction.Behaviors>
</Window.Resources>
<i:Interaction.Behaviors>
<behaviors:ClosingWindowBehavior Command="{Binding ClosingWindowCommand}" />
</i:Interaction.Behaviors>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
<Rectangle >
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#916CE5" Offset="0.5" />
<GradientStop Color="#FFFFFF" Offset="1.5" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</i:Interaction.Behaviors>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
<Rectangle >
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#916CE5" Offset="0.5" />
<GradientStop Color="#FFFFFF" Offset="1.5" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
<Grid Grid.Row="1" Grid.Column="0" >
<Rectangle Grid.Row="1" Grid.Column="0">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#9ABAFF" Offset="0.8" />
<GradientStop Color="#9ABFAF" Offset="0.3" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<StackPanel Margin="0 0 0 0" Grid.Row="1" Grid.Column="0" >
<Button Command="{Binding SwitchToViewCommand}" CommandParameter="ChatMdView" Style="{StaticResource IconButtonStyle}">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/chat64.png" Margin="5"/>
<TextBlock Text="会话" VerticalAlignment="Center"/>
</StackPanel>
</Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1" Grid.Column="1" Margin="0">
<ContentControl Content="{Binding CurrentView}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</Grid>
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#FAAFA9" Offset="0.1" />
<GradientStop Color="#A4D3A2" Offset="0.9" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle >
<WrapPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" VerticalAlignment="Center" HorizontalAlignment="Right">
<Label Content="{Binding CurrentModel}" Width="auto" FontSize="12" Margin="5 0 5 0"/>
<Label Content="{Binding CurrentTime}" Background="#00F0BD" Width="auto" FontSize="12" Margin="5 0 5 0"/>
<Button Content="设置" Command="{Binding OpenSettingWindowCommand}"/>
</WrapPanel>
</Grid>
</Grid>
<Grid Grid.Row="1" Grid.Column="0" >
<Rectangle Grid.Row="1" Grid.Column="0">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#9ABAFF" Offset="0.8" />
<GradientStop Color="#9ABFAF" Offset="0.3" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<StackPanel Margin="0 0 0 0" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center">
<Button Command="{Binding SwitchToViewCommand}" CommandParameter="SettingView" Style="{StaticResource IconButtonStyle}">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/setting64.png" Margin="5" />
<TextBlock Text="设置" VerticalAlignment="Center"/>
</StackPanel>
</Button>
<Button Command="{Binding SwitchToViewCommand}" CommandParameter="ChatMdView" Style="{StaticResource IconButtonStyle}">
<StackPanel Orientation="Horizontal">
<Image Source="/Resources/chat64.png" Margin="5"/>
<TextBlock Text="会话" VerticalAlignment="Center"/>
</StackPanel>
</Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1" Grid.Column="1" Margin="5">
<ContentControl Content="{Binding CurrentView}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"/>
</Grid>
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#FAAFA9" Offset="0.1" />
<GradientStop Color="#A4D3A2" Offset="0.9" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle >
<WrapPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" VerticalAlignment="Center" HorizontalAlignment="Right">
<Label Content="{Binding CurrentModel}" Width="auto" FontSize="12" Margin="5 0 5 0"/>
<Label Content="{Binding CurrentTime}" Background="#00F0BD" Width="auto" FontSize="12" Margin="5 0 5 0"/>
</WrapPanel>
</Grid>
</Grid>
</Window>

View File

@@ -1,4 +1,5 @@
using System.Windows;
using YwxApp.AiChat.ViewModels;
namespace YwxApp.AiChat;
@@ -7,11 +8,15 @@ namespace YwxApp.AiChat;
/// </summary>
public partial class MainWindow : Window
{
MainViewModel viewModel;
public MainWindow()
{
InitializeComponent();
this.viewModel = this.DataContext as MainViewModel;
//OllamaService ollamaService = new();
//ollamaService.Configure("http://192.168.1.3:11434", "deepseek-r1:1.5b");
//this.DataContext = new ChatViewModel(new OllamaService());
}
}

View File

@@ -0,0 +1,20 @@
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace YwxApp.AiChat.ValueConverter
{
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (Visibility)value == Visibility.Visible;
}
}
}

View File

@@ -7,8 +7,7 @@ using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using YwxApp.AiChat.Commands;
using YwxApp.AiChat.Views;
using YwxApp.AiChat.Views;
namespace YwxApp.AiChat.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
@@ -24,6 +23,18 @@ namespace YwxApp.AiChat.ViewModels
#endregion
#region Property
private Visibility _isMenuOpen = Visibility.Visible;
public Visibility IsMenuOpen
{
get => _isMenuOpen;
set
{
_isMenuOpen = value;
OnPropertyChanged();
}
}
public object CurrentView
{
get => _currentView;
@@ -69,6 +80,7 @@ namespace YwxApp.AiChat.ViewModels
#region Command
public ICommand SwitchToViewCommand { get; }
public ICommand ClosingWindowCommand { get; }
public ICommand OpenSettingWindowCommand { get; }
#endregion
@@ -83,10 +95,10 @@ namespace YwxApp.AiChat.ViewModels
//bind command method
SwitchToViewCommand = new ObjectPassingCommand(OnSwitchToView);
ClosingWindowCommand = new EventsCommand<CancelEventArgs>(OnClosingWindow);
OpenSettingWindowCommand = new ObjectPassingCommand(OnOpenSettingWindowCommand);
//create view
_viewList = new ObservableCollection<UserControl>();
ViewList.Add(new SettingView(_ollamaObject));
ViewList.Add(new ChatMdView(_ollamaObject));
//Set the default display of subview 1.
@@ -95,6 +107,8 @@ namespace YwxApp.AiChat.ViewModels
CurrentView = ViewList[0];
}
#region The window close event
/// <summary>
///trigger close event
@@ -137,9 +151,17 @@ namespace YwxApp.AiChat.ViewModels
#region Command method
#region View switch
private void OnOpenSettingWindowCommand(object obj)
{
var setting = new SettingView(_ollamaObject);
setting.ShowDialog();
}
//set the view
public void OnSwitchToView(object operationItem)
{
var viewObj = ViewList.FirstOrDefault(viewObj => viewObj.GetType().Name.Equals(operationItem));
if (viewObj == null)
{
@@ -148,10 +170,7 @@ namespace YwxApp.AiChat.ViewModels
{
case "ChatMdView":
newViewObj = new ChatMdView(_ollamaObject);
break;
case "SettingView":
newViewObj = new SettingView(_ollamaObject);
break;
break;
default:
break;
}
@@ -173,6 +192,19 @@ namespace YwxApp.AiChat.ViewModels
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
{
if (!Equals(field, newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
return true;
}
return false;
}
#endregion
}

View File

@@ -18,8 +18,7 @@
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="120" />
<RowDefinition Height="50" />
<RowDefinition Height="120" />
</Grid.RowDefinitions>
<!--First line: Display output text to "Markdown" container-->
@@ -37,6 +36,10 @@
</Grid>
<!-- the second line -->
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<TextBox x:Name="InputBox"
Text="{Binding InputText , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.Row="1" Margin="5" AcceptsReturn="True"
@@ -46,10 +49,13 @@
<KeyBinding Command="{Binding SubmitQuestionCommand}" Key="Enter"/>
</TextBox.InputBindings>
</TextBox>
<Button x:Name="BtnSubmit" Content="提交" FontSize="18" Margin="5"
Grid.Column="1" Grid.ColumnSpan="1" Grid.RowSpan="1"
Style="{StaticResource RoundCornerButtonStyle}" Command="{Binding SubmitQuestionCommand}" />
</Grid>
<!-- The third line submit button -->
<Grid Grid.Row="2">
<!--<Grid Grid.Row="2">
<WrapPanel Grid.Row="2" HorizontalAlignment="Right">
<Button x:Name="BtnNewChat" Content="新建会话"
@@ -65,7 +71,7 @@
Width="100"
Height="30"/>
</WrapPanel>
</Grid>
</Grid>-->
</Grid>
</UserControl>

View File

@@ -1,10 +1,12 @@
<UserControl x:Class="YwxApp.AiChat.Views.SettingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<UserControl.Resources>
<Window x:Class="YwxApp.AiChat.SettingView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:YwxApp.AiChat"
mc:Ignorable="d" WindowStartupLocation="CenterScreen" WindowStyle="SingleBorderWindow"
Title="AiChatSetting" Height="360" Width="360">
<Window.Resources>
<ResourceDictionary>
<!--Resource dictionary : add the control style-->
<ResourceDictionary.MergedDictionaries>
@@ -14,7 +16,7 @@
<ResourceDictionary Source="Style/ComboBoxStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
</Window.Resources>
<Grid Background="#FFFFFF" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
@@ -29,8 +31,8 @@
<WrapPanel Margin="5,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" >
<Label Content="Ollama路径:" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox x:Name="Tbx_OllamaAppPath" FontSize="12" Text="{Binding OllamaAppPath}" Style="{StaticResource SearchBoxStyle}" Margin="5"
MinWidth="180" VerticalAlignment="Center" VerticalContentAlignment="Center" />
MinWidth="180" VerticalAlignment="Center" VerticalContentAlignment="Center" />
<Label Name="Label_State" Style="{StaticResource RoundLabelStyle}" />
<Button Content="打开" Style="{StaticResource RoundCornerButtonStyle}" Command="{Binding StartOllamaServerCommand}"/>
</WrapPanel>
@@ -47,11 +49,11 @@
<WrapPanel Grid.Row="2" Margin="5,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.ColumnSpan="2">
<Label Content="模型:" VerticalAlignment="Center" Margin="5" />
<ComboBox x:Name="Cbx_ModelList" Style="{StaticResource RoundComboBoxStyle}" ItemsSource="{Binding ModelList}" SelectedItem="{Binding SelectedModel}"
MinWidth="180" VerticalAlignment="Center" VerticalContentAlignment="Center" />
MinWidth="180" VerticalAlignment="Center" VerticalContentAlignment="Center" />
<Button Content="刷新" Margin="5" Grid.Row="1" Style="{StaticResource RoundCornerButtonStyle}" Command="{Binding ModelListUpdateCommand}"/>
</WrapPanel>
<TextBox x:Name="ModelDesciption" Grid.Row="3" IsReadOnly="True" TextWrapping="WrapWithOverflow" Text="{Binding ModelInformation,Mode=OneWay}" Grid.ColumnSpan="2"/>
<ListBox x:Name="ModelList" Grid.Row="3" ItemsSource="{Binding ModelList}" SelectedItem="{Binding SelectedModel}"/>
</Grid>
</UserControl>
</Window>

View File

@@ -1,13 +1,12 @@
using System.Windows.Controls;
using System.Windows;
using YwxApp.AiChat.ViewModels;
namespace YwxApp.AiChat.Views
namespace YwxApp.AiChat
{
/// <summary>
/// SettingView.xaml 的交互逻辑
/// </summary>
public partial class SettingView : UserControl
public partial class SettingView : Window
{
SettingViewModel _viewModel;
public SettingView(ShareOllamaObject ollama)