添加时间格式转换器和命名空间引用

在 `App.xaml` 中添加了对 `chatclient.Data` 命名空间的引用。
在 `ChatDataModel.cs` 中移除不必要的 `using` 语句,添加对 `System.Windows` 和 `System.Windows.Media.Imaging` 的引用,并为 `ConvertBack` 方法添加文档注释。
在 `MainWindow.xaml` 中定义了 `TimeFormatConverter` 资源,以增强时间显示功能。
This commit is contained in:
绪山七寻 2025-06-22 00:55:33 +08:00
parent 1e80f28972
commit 015535c100
3 changed files with 14 additions and 13 deletions

View File

@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:chatclient" xmlns:local="clr-namespace:chatclient"
xmlns:data="clr-namespace:chatclient.Data"
StartupUri="MainWindow.xaml"> StartupUri="MainWindow.xaml">
<Application.Resources> <Application.Resources>
<ResourceDictionary> <ResourceDictionary>

View File

@ -1,13 +1,6 @@
using System; using System.Windows.Media;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows; using System.Windows;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Windows.Input;
using System.Globalization; using System.Globalization;
using System.Windows.Data; using System.Windows.Data;
@ -62,9 +55,6 @@ namespace chatclient.Data
public string? UserName { get; set; } public string? UserName { get; set; }
public string? UserPassword { get; set; } public string? UserPassword { get; set; }
} }
/// <summary>
/// 时间格式转换器类
/// </summary>
public class TimeFormatConverter : IValueConverter public class TimeFormatConverter : IValueConverter
{ {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
@ -81,7 +71,6 @@ namespace chatclient.Data
} }
else if (timestamp.Kind == DateTimeKind.Unspecified) else if (timestamp.Kind == DateTimeKind.Unspecified)
{ {
// 假设未指定时间是UTC时间常见实践
localTime = DateTime.SpecifyKind(timestamp, DateTimeKind.Utc).ToLocalTime(); localTime = DateTime.SpecifyKind(timestamp, DateTimeKind.Utc).ToLocalTime();
} }
else else
@ -127,7 +116,15 @@ namespace chatclient.Data
} }
return value; return value;
} }
/// <summary>
/// 将格式化的时间字符串转换回DateTime对象
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{ {
throw new NotImplementedException(); throw new NotImplementedException();

View File

@ -10,6 +10,9 @@
mc:Ignorable="d" mc:Ignorable="d"
Title="ChatWindow" Height="450" Width="800" MinHeight="240" MinWidth="380" Title="ChatWindow" Height="450" Width="800" MinHeight="240" MinWidth="380"
Style="{StaticResource MaterialDesignWindow}" Closed="MainWindow_Closed" Loaded="MainWindow_Loaded"> Style="{StaticResource MaterialDesignWindow}" Closed="MainWindow_Closed" Loaded="MainWindow_Loaded">
<Window.Resources>
<data:TimeFormatConverter x:Key="TimeConverter"/>
</Window.Resources>
<Grid> <Grid>
<materialDesign:Card> <materialDesign:Card>
<TabControl x:Name="TabControl" VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}"> <TabControl x:Name="TabControl" VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}">