XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.WinUIHelper

【DLL】WinUI的各种工具类,帮助开发者快速构建一个模块化的WinUI项目

公开
关注 0 Fork 0 Star 0
UTF-8
using XFEExtension.NetCore.WinUIHelper.Interface;
using XFEExtension.NetCore.WinUIHelper.Interface.Services;
using XFEExtension.NetCore.WinUIHelper.Model;
using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;

namespace XFEExtension.NetCore.WinUIHelper.Implements.Services;

class SettingService : GlobalServiceBase, ISettingService
{
    private readonly Dictionary<object, IProfileInfoEntry> settingControls = [];

    public Dictionary<object, IProfileInfoEntry> SettingControls => settingControls;

    public void AddComboBox(ComboBox comboBox, Func<string, object?> saveFunc, Func<List<object>, object?, object?> loadFunc)
    {
        if (comboBox.Tag is string profilePath)
            settingControls.Add(comboBox, new ComboBoxProfileInfoEntry(profilePath, saveFunc, loadFunc));
        else
            throw new NullReferenceException();
    }

    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (settingControls.TryGetValue(sender, out var profileInfo) && profileInfo is ComboBoxProfileInfoEntry comboBoxProfileInfo && e.AddedItems.Count > 0 && e.AddedItems[0] is ComboBoxItem comboBoxItem && comboBoxItem.Tag is string value)
            ProfileHelper.SetProfileValue(comboBoxProfileInfo.ProfilePath, comboBoxProfileInfo.ProfileSaveFunc(value));
    }

    public void Initialize()
    {
        foreach (var item in settingControls)
        {
            if (item.Key is ComboBox comboBox && item.Value is ComboBoxProfileInfoEntry comboBoxProfileInfo)
                comboBox.SelectedItem = comboBoxProfileInfo.ProfileLoadFunc([.. comboBox.Items], ProfileHelper.GetProfileValue(comboBoxProfileInfo.ProfilePath));
        }
    }

    public void RegisterEvents()
    {
        foreach (var comboBox in settingControls.Keys.OfType<ComboBox>())
            comboBox.SelectionChanged += ComboBox_SelectionChanged;
    }
}