给设置界面添加数据库的配置
This commit is contained in:
@@ -48,11 +48,20 @@ public partial class App : Application
|
||||
{
|
||||
base.OnStartup(e);
|
||||
await Host.StartAsync();
|
||||
|
||||
try
|
||||
{
|
||||
InitializeDataBase();
|
||||
InitializeMenu()
|
||||
.Await((e) => { NotificationHelper.ShowMessage($"初始化主菜单失败:{e.Message}"); },
|
||||
() => { MessageHelper.SendLoadMessage(LoadTypes.Menu); });
|
||||
Host.Services.GetRequiredService<GrowlNotificationService>();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
NotificationHelper.ShowMessage("加载数据时发生错误,如果是连接字符串不正确,可以在设置界面更改:{exception.Message}",NotificationType.Error);
|
||||
}
|
||||
|
||||
MainWindow = Host.Services.GetRequiredService<MainView>();
|
||||
MainWindow.Show();
|
||||
}
|
||||
|
||||
40
Config/ConnectionSettings.cs
Normal file
40
Config/ConnectionSettings.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace PMSWPF.Config
|
||||
{
|
||||
public class ConnectionSettings
|
||||
{
|
||||
public string DbType { get; set; } = "MySql";
|
||||
public string Server { get; set; } = "127.0.0.1";
|
||||
public int Port { get; set; } = 3306;
|
||||
public string UserId { get; set; } = "root";
|
||||
public string Password { get; set; } = "Pgw15221236646";
|
||||
public string Database { get; set; } = "pmswpf";
|
||||
|
||||
private static readonly string SettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "connectionSettings.json");
|
||||
|
||||
public static ConnectionSettings Load()
|
||||
{
|
||||
if (File.Exists(SettingsFilePath))
|
||||
{
|
||||
string json = File.ReadAllText(SettingsFilePath);
|
||||
return JsonConvert.DeserializeObject<ConnectionSettings>(json);
|
||||
}
|
||||
return new ConnectionSettings(); // Return default settings if file doesn't exist
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
File.WriteAllText(SettingsFilePath, json);
|
||||
}
|
||||
|
||||
public string ToConnectionString()
|
||||
{
|
||||
// This example is for MySQL. You'll need to adjust for other database types.
|
||||
return $"server={Server};port={Port};user={UserId};password={Password};database={Database};";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,14 @@ public class DbContext
|
||||
{
|
||||
public static SqlSugarClient GetInstance()
|
||||
{
|
||||
var connectionString = "server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=pmswpf;";
|
||||
var settings = PMSWPF.Config.ConnectionSettings.Load();
|
||||
var connectionString = settings.ToConnectionString();
|
||||
var dbType = (SqlSugar.DbType)Enum.Parse(typeof(SqlSugar.DbType), settings.DbType);
|
||||
|
||||
var _db = new SqlSugarClient(new ConnectionConfig
|
||||
{
|
||||
ConnectionString = connectionString,
|
||||
DbType = DbType.MySql, // 根据实际数据库类型修改,如DbType.MySql等
|
||||
DbType = dbType, // 根据实际数据库类型修改,如DbType.MySql等
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute
|
||||
});
|
||||
|
||||
@@ -21,10 +21,12 @@
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
|
||||
<PackageReference Include="MQTTnet" Version="3.0.16" />
|
||||
<PackageReference Include="MySql.Data" Version="9.3.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="NLog" Version="6.0.0" />
|
||||
<PackageReference Include="NLog.Database" Version="6.0.0" />
|
||||
<PackageReference Include="NPOI" Version="2.7.4" />
|
||||
<PackageReference Include="S7netplus" Version="0.20.0" />
|
||||
<PackageReference Include="SqlSugarCore" Version="5.1.4.197" />
|
||||
<PackageReference Include="SqlSugarCore.MySql" Version="5.1.4.178" />
|
||||
<PackageReference Include="SqlSugarCoreNoDrive" Version="5.1.4.193" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,6 +1,125 @@
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using PMSWPF.Config;
|
||||
using PMSWPF.Data;
|
||||
using PMSWPF.Helper;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PMSWPF.ViewModels;
|
||||
|
||||
public class SettingViewModel : ViewModelBase
|
||||
public partial class SettingViewModel : ViewModelBase
|
||||
{
|
||||
private ConnectionSettings _connectionSettings;
|
||||
|
||||
public SettingViewModel()
|
||||
{
|
||||
_connectionSettings = ConnectionSettings.Load();
|
||||
AvailableDbTypes = Enum.GetNames(typeof(SqlSugar.DbType)).ToList();
|
||||
}
|
||||
|
||||
public List<string> AvailableDbTypes { get; set; }
|
||||
|
||||
public string SelectedDbType
|
||||
{
|
||||
get => _connectionSettings.DbType;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.DbType != value)
|
||||
{
|
||||
_connectionSettings.DbType = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Server
|
||||
{
|
||||
get => _connectionSettings.Server;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.Server != value)
|
||||
{
|
||||
_connectionSettings.Server = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Port
|
||||
{
|
||||
get => _connectionSettings.Port;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.Port != value)
|
||||
{
|
||||
_connectionSettings.Port = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string UserId
|
||||
{
|
||||
get => _connectionSettings.UserId;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.UserId != value)
|
||||
{
|
||||
_connectionSettings.UserId = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Password
|
||||
{
|
||||
get => _connectionSettings.Password;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.Password != value)
|
||||
{
|
||||
_connectionSettings.Password = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Database
|
||||
{
|
||||
get => _connectionSettings.Database;
|
||||
set
|
||||
{
|
||||
if (_connectionSettings.Database != value)
|
||||
{
|
||||
_connectionSettings.Database = value;
|
||||
OnPropertyChanged();
|
||||
_connectionSettings.Save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task TestConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var db = DbContext.GetInstance())
|
||||
{
|
||||
await db.Ado.OpenAsync();
|
||||
NotificationHelper.ShowMessage("连接成功!");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
NotificationHelper.ShowMessage($"连接失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,133 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
<Grid />
|
||||
d:DesignHeight="450"
|
||||
d:DesignWidth="800">
|
||||
<StackPanel>
|
||||
|
||||
<ui:SettingsExpander x:Name="dbSettingCard"
|
||||
Header="设置数据库"
|
||||
Description="设置要连接的数据库,数据全部在数据库中存放">
|
||||
<ui:SettingsExpander.HeaderIcon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.DataSense}"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,8,0"
|
||||
FontSize="14" />
|
||||
</ui:SettingsExpander.HeaderIcon>
|
||||
|
||||
<Button Content="测试连接"
|
||||
Command="{Binding TestConnectionCommand}"
|
||||
Style="{StaticResource AccentButtonStyle}"
|
||||
Margin="0,10,0,0"
|
||||
HorizontalAlignment="Left" />
|
||||
|
||||
<ui:SettingsExpander.Items>
|
||||
<ui:SettingsCard HorizontalAlignment="Left"
|
||||
Width="{Binding ElementName=dbSettingCard ,Path=Width}">
|
||||
<ikw:SimpleStackPanel Orientation="Horizontal"
|
||||
Spacing="10">
|
||||
<TextBlock Text="数据库类型:"
|
||||
FontSize="14"
|
||||
VerticalAlignment="Center"
|
||||
Margin="30,0,10,0" />
|
||||
<ComboBox Grid.Column="1"
|
||||
MinWidth="200"
|
||||
ItemsSource="{Binding AvailableDbTypes}"
|
||||
SelectedItem="{Binding SelectedDbType}" />
|
||||
</ikw:SimpleStackPanel>
|
||||
</ui:SettingsCard>
|
||||
|
||||
|
||||
<ui:SettingsCard HorizontalAlignment="Left"
|
||||
Width="{Binding ElementName=dbSettingCard ,Path=Width}">
|
||||
<ikw:SimpleStackPanel Orientation="Horizontal"
|
||||
Spacing="10">
|
||||
<TextBlock Text="服务器地址:"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,10,0" />
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding Server, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ikw:SimpleStackPanel>
|
||||
</ui:SettingsCard>
|
||||
<ui:SettingsCard HorizontalAlignment="Left"
|
||||
Width="{Binding ElementName=dbSettingCard ,Path=Width}">
|
||||
<ikw:SimpleStackPanel Orientation="Horizontal"
|
||||
Spacing="10">
|
||||
<TextBlock Text="服务器端口:"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,10,0" />
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding Port, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ikw:SimpleStackPanel>
|
||||
</ui:SettingsCard>
|
||||
|
||||
|
||||
<ui:SettingsCard HorizontalAlignment="Left"
|
||||
Width="{Binding ElementName=dbSettingCard ,Path=Width}">
|
||||
<ikw:SimpleStackPanel Orientation="Horizontal"
|
||||
Spacing="10">
|
||||
<TextBlock Text="用户名:"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,10,0" />
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding UserId, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ikw:SimpleStackPanel>
|
||||
</ui:SettingsCard>
|
||||
|
||||
<ui:SettingsCard HorizontalAlignment="Left"
|
||||
Width="{Binding ElementName=dbSettingCard ,Path=Width}">
|
||||
<ikw:SimpleStackPanel Orientation="Horizontal"
|
||||
Spacing="10">
|
||||
<TextBlock Text="密码:"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,10,0" />
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding Password, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ikw:SimpleStackPanel>
|
||||
</ui:SettingsCard>
|
||||
|
||||
<ui:SettingsCard HorizontalAlignment="Left"
|
||||
Width="{Binding ElementName=dbSettingCard ,Path=Width}">
|
||||
<ikw:SimpleStackPanel Orientation="Horizontal"
|
||||
Spacing="10">
|
||||
<TextBlock Text="数据库名称:"
|
||||
VerticalAlignment="Center"
|
||||
Margin="0,0,10,0" />
|
||||
<TextBox Grid.Column="1"
|
||||
Text="{Binding Database, UpdateSourceTrigger=PropertyChanged}" />
|
||||
</ikw:SimpleStackPanel>
|
||||
</ui:SettingsCard>
|
||||
|
||||
|
||||
</ui:SettingsExpander.Items>
|
||||
|
||||
</ui:SettingsExpander>
|
||||
|
||||
<ui:SettingsExpander Header="SettingsExpander"
|
||||
Description="The SettingsExpander has the same properties as a SettingsCard">
|
||||
<ui:SettingsExpander.HeaderIcon>
|
||||
<ui:FontIcon Glyph="" />
|
||||
</ui:SettingsExpander.HeaderIcon>
|
||||
|
||||
<Button Content="Content"
|
||||
Style="{StaticResource AccentButtonStyle}" />
|
||||
|
||||
<ui:SettingsExpander.Items>
|
||||
<ui:SettingsCard Header="A basic SettingsCard within an SettingsExpander">
|
||||
<Button Content="Button" />
|
||||
</ui:SettingsCard>
|
||||
<ui:SettingsCard Description="SettingsCard within an Expander can be made clickable too!"
|
||||
Header="This item can be clicked"
|
||||
IsClickEnabled="True" />
|
||||
<ui:SettingsCard ContentAlignment="Left">
|
||||
<CheckBox
|
||||
Content="Here the ContentAlignment is set to Left. This is great for e.g. CheckBoxes or RadioButtons" />
|
||||
</ui:SettingsCard>
|
||||
</ui:SettingsExpander.Items>
|
||||
</ui:SettingsExpander>
|
||||
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
@@ -1,3 +1,4 @@
|
||||
using PMSWPF.ViewModels;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace PMSWPF.Views;
|
||||
@@ -7,5 +8,6 @@ public partial class SettingView : UserControl
|
||||
public SettingView()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = new SettingViewModel();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user