using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace WxOrder.Framework.Commons { public static class DebugVisibility { // 定义一个附加属性 public static readonly DependencyProperty EnableProperty = DependencyProperty.RegisterAttached( "Enable", typeof(bool), typeof(DebugVisibility), new FrameworkPropertyMetadata( true, FrameworkPropertyMetadataOptions.AffectsArrange, OnEnableChanged, OnEnableCoerceCall)); private static object OnEnableCoerceCall(DependencyObject d, object baseValue) { if (d is UIElement element) { #if DEBUG // Debug 模式下,根据属性值决定可见性 element.Visibility = (bool)baseValue ? Visibility.Visible : Visibility.Collapsed; #else // Release 模式下,隐藏所有标记了 IsDebug 的元素 element.Visibility = Visibility.Collapsed; #endif } return baseValue; } private static void OnEnableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is UIElement element) { #if DEBUG // Debug 模式下,根据属性值决定可见性 element.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed; #else // Release 模式下,隐藏所有标记了 IsDebug 的元素 element.Visibility = Visibility.Collapsed; #endif } } public static bool GetEnable(DependencyObject obj) => (bool)obj.GetValue(EnableProperty); public static void SetEnable(DependencyObject obj, bool value) => obj.SetValue(EnableProperty, value); } }