A:项目介绍
This commit is contained in:
1 parent
956169dbb4
commit
9ffa70acde
1 file changed
+111
@@ -152,6 +152,117 @@ dotnet test
|
||||
|
||||
核心用例位于 `test/Common.Test`,覆盖 Result 与 Global 的主要功能与边界场景。
|
||||
|
||||
## WPF 工具包(Sharp.Wpf)
|
||||
|
||||
提供即插即用的 WPF 常用基础能力:
|
||||
|
||||
- 全局异常处理:统一捕获 UI/后台/未观察任务异常,支持弹窗与自定义日志
|
||||
- MVVM 基础:`ViewModelBase`(INPC/IsBusy)、`RelayCommand`、`AsyncCommand`
|
||||
- Dispatcher 封装:`IDispatcher`/`DispatcherService`,提供 UI 线程调用统一入口
|
||||
- 对话与文件对话:`IDialogService`、`IFileDialogService`
|
||||
- 常用转换器:Bool/NullOrEmpty 可见性、布尔取反、枚举描述
|
||||
- 附加行为:EventToCommand(事件转 ICommand)
|
||||
- 窗口工具:`WindowPlacement` 窗口位置/大小/状态持久化
|
||||
|
||||
### 引用
|
||||
|
||||
在你的 WPF 项目中添加项目引用:
|
||||
|
||||
```xml
|
||||
<!-- Client/Client.csproj -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Wpf\Wpf.csproj" />
|
||||
<!-- 或通过 NuGet: <PackageReference Include="Sharp.Wpf" Version="x.y.z" /> -->
|
||||
|
||||
</ItemGroup>
|
||||
```
|
||||
|
||||
### 全局异常处理(App 启动)
|
||||
|
||||
```csharp
|
||||
// App.Startup.cs
|
||||
using System.Windows;
|
||||
using Sharp.Wpf.Exceptions;
|
||||
|
||||
public partial class App : Application
|
||||
{
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
new UiExceptionHandler().Register(new ExceptionHandlingOptions
|
||||
{
|
||||
ShowDialog = true,
|
||||
DialogTitle = "应用程序错误"
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### XAML 集成(窗口位置、行为、转换器)
|
||||
|
||||
```xml
|
||||
<Window x:Class="Client.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:b="clr-namespace:Sharp.Wpf.Behaviors;assembly=Sharp.Wpf"
|
||||
xmlns:w="clr-namespace:Sharp.Wpf.Windowing;assembly=Sharp.Wpf"
|
||||
xmlns:conv="clr-namespace:Sharp.Wpf.Converters;assembly=Sharp.Wpf"
|
||||
Title="MainWindow"
|
||||
w:WindowPlacement.RememberPlacement="True"
|
||||
w:WindowPlacement.PlacementId="MainWindow">
|
||||
<Window.Resources>
|
||||
<conv:BoolToVisibilityConverter x:Key="Bool2Vis" />
|
||||
</Window.Resources>
|
||||
<StackPanel Margin="16">
|
||||
<Button Content="点击弹窗"
|
||||
b:EventToCommand.EventName="Click"
|
||||
b:EventToCommand.Command="{Binding ShowDialogCommand}" />
|
||||
<Button Content="触发异常"
|
||||
b:EventToCommand.EventName="Click"
|
||||
b:EventToCommand.Command="{Binding ThrowCommand}" />
|
||||
<TextBlock Text="正在处理..."
|
||||
Visibility="{Binding IsBusy, Converter={StaticResource Bool2Vis}}"/>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
```
|
||||
|
||||
### ViewModel 与命令
|
||||
|
||||
```csharp
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Sharp.Wpf.Commands;
|
||||
using Sharp.Wpf.Mvvm;
|
||||
using Sharp.Wpf.Services;
|
||||
|
||||
public sealed class MainViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IDialogService _dialog = new DialogService();
|
||||
|
||||
public string Greeting { get; set; } = "欢迎使用 Sharp.Wpf";
|
||||
public IAsyncCommand ShowDialogCommand { get; }
|
||||
public RelayCommand ThrowCommand { get; }
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
ShowDialogCommand = new AsyncCommand(async () =>
|
||||
{
|
||||
try { IsBusy = true; await Task.Delay(300); await _dialog.ShowInfoAsync("Hello!"); }
|
||||
finally { IsBusy = false; }
|
||||
});
|
||||
|
||||
ThrowCommand = new RelayCommand(() => throw new InvalidOperationException("演示未处理异常"));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 文件对话
|
||||
|
||||
```csharp
|
||||
var files = await new FileDialogService().OpenFilesAsync("Images|*.png;*.jpg");
|
||||
var folder = await new FileDialogService().SelectFolderAsync();
|
||||
```
|
||||
|
||||
## 路线图(Roadmap)
|
||||
|
||||
- P0(最优先)
|
||||
|
||||
Reference in new issue
Block a user