c# - 如何在 WPF 中的代码中复制资源引用?

标签 c# wpf xaml dynamicresource

在我的应用程序中,我有一个颜色资源。我有一个元素使用该颜色作为 xaml 中的动态资源。

  <Window x:Class="ResourcePlay.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="425">
     <Window.Resources>
        <Color x:Key="MyColor">Red</Color>
     </Window.Resources>
     <Grid>
        <Rectangle VerticalAlignment="Top" Width="80" Height="80" Margin="10">
           <Rectangle.Fill>
              <SolidColorBrush x:Name="TopBrush" Color="{DynamicResource MyColor}"/>
           </Rectangle.Fill>
        </Rectangle>
        <Rectangle VerticalAlignment="Bottom" Width="80" Height="80" Margin="10">
           <Rectangle.Fill>
              <SolidColorBrush x:Name="BottomBrush"/>
           </Rectangle.Fill>
        </Rectangle>
     </Grid>
  </Window>

在代码中,我想复制这个资源引用。
  using System.Windows;
  using System.Windows.Media;

  namespace ResourcePlay {
     public partial class MainWindow : Window {
        public MainWindow() {
           InitializeComponent();

           // I want to copy the resource reference, not the color.
           BottomBrush.Color = TopBrush.Color;

           // I'd really rather do something like this.
           var reference = TopBrush.GetResourceReference(SolidColorBrush.ColorProperty);
           BottomBrush.SetResourceReference(reference);

           // I want this to change the colors of both elements
           Resources["MyColor"] = Colors.Green;
        }
     }
  }

但是,SetResourceReference 仅适用于 FrameworkElements 或 FrameworkContentElements。 SolidColorBrush 只是一个 Freezable。另外,我不知道如何在后面的代码中获取资源引用。

有没有办法在 WPF 中做到这一点,以便两种颜色同时改变?在我的实际应用程序中,问题并不是那么简单,所以我不能只在 xaml 中添加第二个 DynamicResource。

最佳答案

Il Vic 建议使用反射。在此基础上进行扩展,我能够为 DependencyObject 构建一些扩展方法来满足我的需求。我真的不喜欢在代码中使用反射,如果其他人知道实现这一点的更好方法,我很乐意看到它。至少当我尝试从后面的代码调试 DynamicResources 时,这将很有帮助。

  public static class DependencyObjectExtensions
  {
     public static object GetDynamicResourceKey(this DependencyObject obj, DependencyProperty prop)
     {
        // get the value entry from the depencency object for the specified dependency property
        var dependencyObject = typeof(DependencyObject);
        var dependencyObject_LookupEntry = dependencyObject.GetMethod("LookupEntry", BindingFlags.NonPublic | BindingFlags.Instance);
        var entryIndex = dependencyObject_LookupEntry.Invoke(obj, new object[] { prop.GlobalIndex });
        var effectiveValueEntry_GetValueEntry = dependencyObject.GetMethod("GetValueEntry", BindingFlags.NonPublic | BindingFlags.Instance);
        var valueEntry = effectiveValueEntry_GetValueEntry.Invoke(obj, new object[] { entryIndex, prop, null, 0x10 });

        // look inside the value entry to find the ModifiedValue object
        var effectiveValueEntry = valueEntry.GetType();
        var effectiveValueEntry_Value = effectiveValueEntry.GetProperty("Value", BindingFlags.Instance | BindingFlags.NonPublic);
        var effectiveValueEntry_Value_Getter = effectiveValueEntry_Value.GetGetMethod(nonPublic: true);
        var rawEntry = effectiveValueEntry_Value_Getter.Invoke(valueEntry, new object[0]);

        // look inside the ModifiedValue object to find the ResourceReference
        var modifiedValue = rawEntry.GetType();
        var modifiedValue_BaseValue = modifiedValue.GetProperty("BaseValue", BindingFlags.Instance | BindingFlags.NonPublic);
        var modifiedValue_BaseValue_Getter = modifiedValue_BaseValue.GetGetMethod(nonPublic: true);
        var resourceReferenceValue = modifiedValue_BaseValue_Getter.Invoke(rawEntry, new object[0]);

        // check the ResourceReference for the original ResourceKey
        var resourceReference = resourceReferenceValue.GetType();
        var resourceReference_resourceKey = resourceReference.GetField("_resourceKey", BindingFlags.NonPublic | BindingFlags.Instance);
        var resourceKey = resourceReference_resourceKey.GetValue(resourceReferenceValue);

        return resourceKey;
     }

     public static void SetDynamicResourceKey(this DependencyObject obj, DependencyProperty prop, object resourceKey)
     {
        var dynamicResource = new DynamicResourceExtension(resourceKey);
        var resourceReferenceExpression = dynamicResource.ProvideValue(null);
        obj.SetValue(prop, resourceReferenceExpression);
     }
  }

第二种方法使用DynamicResourceExtension避免使用 Activator 带来一些麻烦,但第一种方法感觉非常脆弱。

我可以在原始示例中使用这些方法,如下所示:
  public MainWindow() {
     InitializeComponent();

     var key = TopBrush.GetDynamicResourceKey(SolidColorBrush.ColorProperty);
     BottomBrush.SetDynamicResourceKey(SolidColorBrush.ColorProperty, key);

     Resources["MyColor"] = Colors.Green;
  }

这适用于任何 DependencyProperty,只要在我们尝试获取资源键时将其设置为 DynamicResource。生产代码需要更多的技巧。

关于c# - 如何在 WPF 中的代码中复制资源引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28240528/

相关文章:

c# - 数据绑定(bind)/PropertyChanged 为空

c# - 如何在TextBox中书写时自动添加点

wpf - 为 RelativeSource 绑定(bind)定义混合表达式 ViewModel 定义

c# - 如何检查使用 SetThreadExecutionState 禁用 sleep 是否失败?

c# - 如何部署用sql server express 2012和 Crystal 报表开发的C#windows窗体应用

wpf - 获得焦点时如何设置组合框背景颜色

WPF DataTrigger 适用于图像,但不适用于路径

html - 网络布局系统

C#、Linq、.Net3.1——根据条件创建新集合

c# - Unity 测试设置强制排除我想要从项目中测试的所有代码