mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-19 02:23:01 +08:00
33
src/MaaWpfGui/Utilities/PropertyDependsOnAttribute.cs
Normal file
33
src/MaaWpfGui/Utilities/PropertyDependsOnAttribute.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
// <copyright file="PropertyDependsOnAttribute.cs" company="MaaAssistantArknights">
|
||||
// MaaWpfGui - A part of the MaaCoreArknights project
|
||||
// Copyright (C) 2021 MistEO and Contributors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License v3.0 only as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY
|
||||
// </copyright>
|
||||
|
||||
#nullable enable
|
||||
using System;
|
||||
|
||||
namespace MaaWpfGui.Utilities;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
|
||||
public class PropertyDependsOnAttribute : Attribute
|
||||
{
|
||||
public PropertyDependsOnAttribute(string propertyName)
|
||||
{
|
||||
PropertyNames = [propertyName];
|
||||
}
|
||||
|
||||
public PropertyDependsOnAttribute(string propertyName, params string[] propertyNames)
|
||||
{
|
||||
PropertyNames = [propertyName, .. propertyNames];
|
||||
}
|
||||
|
||||
public string[] PropertyNames { get; init; }
|
||||
}
|
||||
@@ -12,8 +12,13 @@
|
||||
// </copyright>
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using MaaWpfGui.Main;
|
||||
using MaaWpfGui.Services;
|
||||
using MaaWpfGui.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Stylet;
|
||||
|
||||
@@ -21,6 +26,60 @@ namespace MaaWpfGui.ViewModels;
|
||||
|
||||
public abstract class TaskViewModel : PropertyChangedBase
|
||||
{
|
||||
private readonly Dictionary<string, List<string>> _propertyDependencies = []; // 属性依赖关系, key为被订阅的属性名, value为依赖于该属性的属性名列表
|
||||
|
||||
protected TaskViewModel()
|
||||
{
|
||||
InitializePropertyDependencies();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化属性依赖关系
|
||||
/// </summary>
|
||||
private void InitializePropertyDependencies()
|
||||
{
|
||||
var type = GetType();
|
||||
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
|
||||
|
||||
foreach (var property in properties)
|
||||
{
|
||||
var dependsOnAttributes = property.GetCustomAttributes<PropertyDependsOnAttribute>(true);
|
||||
foreach (var attribute in dependsOnAttributes.Where(i => i.PropertyNames.Length > 0))
|
||||
{
|
||||
foreach (var key in attribute.PropertyNames)
|
||||
{
|
||||
if (!_propertyDependencies.TryGetValue(key, out var value))
|
||||
{
|
||||
value = [];
|
||||
_propertyDependencies[key] = value;
|
||||
}
|
||||
|
||||
if (_propertyDependencies.TryGetValue(property.Name, out var values) && values.Contains(key))
|
||||
{
|
||||
throw new ArgumentException($"属性 {key} 依赖于属性 {property.Name}, 但它们之间存在循环依赖关系");
|
||||
}
|
||||
|
||||
value.Add(property.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PropertyChanged += OnPropertyChanged;
|
||||
}
|
||||
|
||||
private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(e.PropertyName) || !_propertyDependencies.TryGetValue(e.PropertyName, out var value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var dependentProperty in value)
|
||||
{
|
||||
NotifyOfPropertyChange(dependentProperty);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void ProcSubTaskMsg(AsstMsg msg, JObject details)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ using MaaWpfGui.Helper;
|
||||
using MaaWpfGui.Models;
|
||||
using MaaWpfGui.Models.AsstTasks;
|
||||
using MaaWpfGui.Services;
|
||||
using MaaWpfGui.Utilities;
|
||||
using MaaWpfGui.Utilities.ValueType;
|
||||
using MaaWpfGui.ViewModels.UI;
|
||||
using Microsoft.Win32;
|
||||
@@ -317,11 +318,11 @@ public class InfrastSettingsUserControlModel : TaskViewModel
|
||||
CustomInfrastFile = @"resource\custom_infrast\" + value;
|
||||
}
|
||||
|
||||
NotifyOfPropertyChange(nameof(IsCustomInfrastFileReadOnly));
|
||||
ConfigurationHelper.SetValue(ConfigurationKeys.DefaultInfrast, value);
|
||||
}
|
||||
}
|
||||
|
||||
[PropertyDependsOn(nameof(DefaultInfrast))]
|
||||
public bool IsCustomInfrastFileReadOnly => _defaultInfrast != UserDefined;
|
||||
|
||||
private bool _dormFilterNotStationedEnabled = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.DormFilterNotStationedEnabled, bool.TrueString));
|
||||
|
||||
Reference in New Issue
Block a user