![]() |
VOOZH | about |
dotnet add package ShadowObservableConfig --version 0.6.2
NuGet\Install-Package ShadowObservableConfig -Version 0.6.2
<PackageReference Include="ShadowObservableConfig" Version="0.6.2" />
<PackageVersion Include="ShadowObservableConfig" Version="0.6.2" />Directory.Packages.props
<PackageReference Include="ShadowObservableConfig" />Project file
paket add ShadowObservableConfig --version 0.6.2
#r "nuget: ShadowObservableConfig, 0.6.2"
#:package ShadowObservableConfig@0.6.2
#addin nuget:?package=ShadowObservableConfig&version=0.6.2Install as a Cake Addin
#tool nuget:?package=ShadowObservableConfig&version=0.6.2Install as a Cake Tool
👁 NuGet Version
👁 License: MIT
👁 .NET
一个为 WinUI 3 设计的响应式配置文件管理库,通过源代码生成器自动生成配置类,支持 YAML/JSON 格式配置文件,提供完整的 MVVM 数据绑定支持。
INotifyPropertyChanged 和 INotifyCollectionChanged<PackageReference Include="ShadowObservableConfig.Yaml" Version="0.6.0" />
// In App.xaml.cs
ShadowObservableConfig.GlobalSetting.Init(ApplicationData.Current.LocalFolder.Path,
[
new ShadowObservableConfig.Yaml.YamlConfigLoader()
]);
<PackageReference Include="ShadowObservableConfig.Json" Version="0.6.0" />
// In App.xaml.cs
ShadowObservableConfig.GlobalSetting.Init(ApplicationData.Current.LocalFolder.Path,
[
new ShadowObservableConfig.Json.JsonConfigLoader()
]);
FileExt 根据安装的库可选: .yaml 或 .json
using ShadowObservableConfig.Attributes;
using System.Collections.ObjectModel;
[ObservableConfig(FileName = "app_config", FileExt = ".yaml", DirPath = "config", Description = "应用程序配置", Version = "1.0.0")]
public partial class AppConfig
{
[ObservableConfigProperty(Name = "AppName", Description = "应用程序名称")]
private string _appName = "My App";
[ObservableConfigProperty(Name = "IsEnabled", Description = "是否启用")]
private bool _isEnabled = true;
[ObservableConfigProperty(Name = "MaxRetryCount", Description = "最大重试次数")]
private int _maxRetryCount = 3;
[ObservableConfigProperty(Name = "Settings", Description = "应用设置")]
private AppSettings _settings = new();
[ObservableConfigProperty(Name = "Features", Description = "功能列表")]
private ObservableCollection<string> _features = new();
}
[ObservableConfig(Description = "应用设置", Version = "1.0.0")]
public partial class AppSettings
{
[ObservableConfigProperty(Name = "Theme", Description = "主题")]
private string _theme = "Light";
[ObservableConfigProperty(Name = "Language", Description = "语言")]
private string _language = "zh-CN";
}
// App.xaml.cs
public App()
{
InitializeComponent();
ShadowObservableConfig.GlobalSetting.Init(ApplicationData.Current.LocalFolder.Path,
[
new ShadowObservableConfig.Yaml.YamlConfigLoader()
]);
}
public sealed partial class MainPage : Page
{
public AppConfig ViewModel { get; } = AppConfig.Load();
public MainPage()
{
this.InitializeComponent();
ViewModel.ConfigChanged += OnConfigChanged;
}
private void OnConfigChanged(object? sender, ConfigChangedEventArgs e)
{
Debug.WriteLine($"配置项 '{e.FullPropertyPath}' 已更改: {e.OldValue} -> {e.NewValue}");
}
}
<Page x:Class="MyApp.MainPage">
<StackPanel>
<TextBox Header="应用程序名称"
Text="{x:Bind ViewModel.AppName, Mode=TwoWay}" />
<CheckBox Content="启用应用程序"
IsChecked="{x:Bind ViewModel.IsEnabled, Mode=TwoWay}" />
<NumberBox Header="最大重试次数"
Value="{x:Bind ViewModel.MaxRetryCount, Mode=TwoWay}" />
<ComboBox Header="主题"
SelectedItem="{x:Bind ViewModel.Settings.Theme, Mode=TwoWay}">
<ComboBoxItem Content="Light" />
<ComboBoxItem Content="Dark" />
</ComboBox>
</StackPanel>
</Page>
FileName: 配置文件名(不含扩展名)不填该项说明当前类是内部类FileExt: 配置文件扩展名DirPath: 配置文件目录(默认为 "config")Description: 配置描述Version: 配置版本Name: 属性在配置文件中的名称Description: 属性描述Alias: 属性别名(只在yaml有效)AutoSave: 是否自动保存(默认为 true)string, int, double, bool, DateTime等enum 类型ObservableCollection<T>[ObservableConfig] 的类源代码生成器会自动为每个配置类生成:
Load() 静态方法Save() 方法AfterConfigInit() 部分方法(可重写)public class CustomConfigLoader : IConfigLoader
{
public T Load<T>(string filePath) where T : class
{
// 自定义加载逻辑
return JsonSerializer.Deserialize<T>(File.ReadAllText(filePath));
}
public void Save<T>(T config, string filePath) where T : class
{
// 自定义保存逻辑
File.WriteAllText(filePath, JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }));
}
}
自定义结束记得在ShadowObservableConfig.GlobalSetting.Init里设置
[ObservableConfig(FileName = "my_config")]
public partial class MyConfig
{
[ObservableConfigProperty(Name = "Value")]
private string _value = "default";
partial void AfterConfigInit()
{
// 配置加载完成后的初始化逻辑
Console.WriteLine($"配置已加载: {Value}");
}
}
ShadowObservableConfig/
├── ShadowObservableConfig/ # 核心库
│ ├── BaseConfig.cs # 基础配置类
│ ├── Attributes/ # 属性定义
│ └── Args/ # 事件参数
├── ShadowObservableConfig.SourceGenerator/ # 源代码生成器
│ └── Generators/ # 生成器实现
├── ShadowObservableConfig.Yaml/ # YAML 支持扩展
├── ShadowObservableConfig.Json/ # JSON 支持扩展
└── Config.WinUI/ # WinUI 3 示例应用
欢迎提交 Issue 和 Pull Request!
本项目采用 MIT 许可证 - 查看 文件了解详情。
Made with ❤️ by kitUIN
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 2 NuGet packages that depend on ShadowObservableConfig:
| Package | Downloads |
|---|---|
|
ShadowObservableConfig.Yaml
✨ ShadowObservableConfig.Yaml - 基于 YAML 的响应式配置文件扩展 ✨ |
|
|
ShadowObservableConfig.Json
✨ ShadowObservableConfig.Json - 基于 Json 的响应式配置文件扩展 ✨ |
This package is not used by any popular GitHub repositories.