在 ResourceDictionary 中交换 DynamicResource
Posted
技术标签:
【中文标题】在 ResourceDictionary 中交换 DynamicResource【英文标题】:Swap DynamicResource in ResourceDictionary 【发布时间】:2022-01-07 22:21:01 【问题描述】:我有一个包含许多 ResourceDictionaries 的 MVVM 项目。我已经可以为颜色资源设置动画并动态更改画笔。更新作品。不幸的是,“重新链接”一个 DynamicResource 不起作用。 我的目标是通过代码将资源“IPC”中的 Brush="DynamicResource Process4" 交换为 Brush="DynamicResource Process6" 。解决方案必须是动态的。最好的方法是直接绑定到我的 ViewModel 的属性。
我已经在 IPC 中尝试过 Brush="DynamicResource Processes",但是更新 ProcessesMask 属性并没有更新 IPC。
public class ViewModel : ViewModelBase
private Brush _ProcessesMask = GetAppResObject<DrawingBrush>("Process9");
public Brush ProcessesMask
get
return _ProcessesMask;
set
_ProcessesMask = Value;
PropertyChanged("ProcessesMask");
private T GetAppResObject<T>(string resourceKey)
try
object value = Windows.Application.Current.FindResource(resourceKey);
if (value is T)
return (T)value;
else
return default(T);
catch (Exception ex)
Logger.Append($"typeof(T).Name not found in the resources: resourceKey");
return default(T);
<?xml version="1.0" encoding="utf-8"?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen00"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen01"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen02"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen10"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen11"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen12"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen20"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen21"></SolidColorBrush>
<SolidColorBrush Color="#00FFFFFF" x:Key="Screen22"></SolidColorBrush>
<DrawingBrush x:Key="Processes" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="Binding ViewModel.ProcessesMask, UpdateSourceTrigger=PropertyChanged" Geometry="F1 M 0.220913,0.220886L 164.221,0.220886L 164.221,104.221L 0.220913,104.221L 0.220913,0.220886 Z "/>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="Process4" Stretch="Uniform" >
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="DynamicResource Screen00" Geometry="F1 M 2.6668,-3.8147e-005L 75.3332,-3.8147e-005C 76.806,-3.8147e-005 78,1.19395 78,2.66677L 78,45.3332C 78,46.806 76.806,48 75.3332,48L 2.6668,48C 1.19397,48 -6.70045e-006,46.806 -6.70045e-006,45.3332L -6.70045e-006,2.66677C -6.70045e-006,1.19395 1.19397,-3.8147e-005 2.6668,-3.8147e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.206964" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen01" Geometry="F1 M 88.6668,7.62939e-006L 161.333,7.62939e-006C 162.806,7.62939e-006 164,1.19397 164,2.66679L 164,45.3332C 164,46.806 162.806,48 161.333,48L 88.6668,48C 87.194,48 86,46.806 86,45.3332L 86,2.66679C 86,1.19397 87.194,7.62939e-006 88.6668,7.62939e-006 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.206829" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen10" Geometry="F1 M 2.6668,56L 75.3332,56C 76.8061,56 78,57.194 78,58.6668L 78,101.333C 78,102.806 76.8061,104 75.3332,104L 2.6668,104C 1.19397,104 -5.2378e-006,102.806 -5.2378e-006,101.333L -5.2378e-006,58.6668C -5.2378e-006,57.194 1.19397,56 2.6668,56 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen11" Geometry="F1 M 88.6668,56L 161.333,56C 162.806,56 164,57.1939 164,58.6667L 164,101.333C 164,102.806 162.806,104 161.333,104L 88.6668,104C 87.194,104 86,102.806 86,101.333L 86,58.6667C 86,57.1939 87.194,56 88.6668,56 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.207383" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="Process6" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="DynamicResource Screen00" Geometry="F1 M 2.66679,-3.8147e-005L 47.3332,-3.8147e-005C 48.806,-3.8147e-005 50,1.19395 50,2.66677L 50,45.3332C 50,46.806 48.806,48 47.3332,48L 2.66679,48C 1.19397,48 -3.58676e-006,46.806 -3.58676e-006,45.3332L -3.58676e-006,2.66677C -3.58676e-006,1.19395 1.19397,-3.8147e-005 2.66679,-3.8147e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.206964" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen01" Geometry="F1 M 59.6668,9.15527e-005L 104.333,9.15527e-005C 105.806,9.15527e-005 107,1.19405 107,2.66688L 107,45.3333C 107,46.8062 105.806,48.0001 104.333,48.0001L 59.6668,48.0001C 58.194,48.0001 57,46.8062 57,45.3333L 57,2.66688C 57,1.19405 58.194,9.15527e-005 59.6668,9.15527e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.204468" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen02" Geometry="F1 M 2.66676,56.0001L 47.3332,56.0001C 48.806,56.0001 50,57.1941 50,58.6669L 50,101.333C 50,102.806 48.806,104 47.3332,104L 2.66676,104C 1.19393,104 -3.62374e-005,102.806 -3.62374e-005,101.333L -3.62374e-005,58.6669C -3.62374e-005,57.1941 1.19393,56.0001 2.66676,56.0001 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen10" Geometry="F1 M 59.6668,56L 104.333,56C 105.806,56 107,57.194 107,58.6668L 107,101.333C 107,102.806 105.806,104 104.333,104L 59.6668,104C 58.194,104 57,102.806 57,101.333L 57,58.6668C 57,57.194 58.194,56 59.6668,56 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.207383" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen11" Geometry="F1 M 116.667,5.34058e-005L 161.333,5.34058e-005C 162.806,5.34058e-005 164,1.19401 164,2.66684L 164,45.3333C 164,46.8061 162.806,48.0001 161.333,48.0001L 116.667,48.0001C 115.194,48.0001 114,46.8061 114,45.3333L 114,2.66684C 114,1.19401 115.194,5.34058e-005 116.667,5.34058e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.204468" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen12" Geometry="F1 M 116.667,56L 161.333,56C 162.806,56 164,57.194 164,58.6668L 164,101.333C 164,102.806 162.806,104 161.333,104L 116.667,104C 115.194,104 114,102.806 114,101.333L 114,58.6668C 114,57.194 115.194,56 116.667,56 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.205016" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="Process9" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="DynamicResource Screen00" Geometry="F1 M 2.66681,-5.34058e-005L 47.3332,-5.34058e-005C 48.806,-5.34058e-005 50,1.19392 50,2.66675L 50,29.3331C 50,30.806 48.806,32 47.3332,32L 2.66681,32C 1.19398,32 1.56308e-005,30.806 1.56308e-005,29.3331L 1.56308e-005,2.66675C 1.56308e-005,1.19392 1.19398,-5.34058e-005 2.66681,-5.34058e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.209617" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen01" Geometry="F1 M 2.66678,36L 47.3332,36C 48.806,36 50,37.1939 50,38.6668L 50,65.3331C 50,66.806 48.806,68 47.3332,68L 2.66678,68C 1.19395,68 -1.81472e-005,66.806 -1.81472e-005,65.3331L -1.81472e-005,38.6668C -1.81472e-005,37.1939 1.19395,36 2.66678,36 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen02" Geometry="F1 M 2.6668,72.0001L 47.3332,72.0001C 48.806,72.0001 50,73.194 50,74.6668L 50,101.333C 50,102.806 48.806,104 47.3332,104L 2.6668,104C 1.19398,104 8.08905e-006,102.806 8.08905e-006,101.333L 8.08905e-006,74.6668C 8.08905e-006,73.194 1.19398,72.0001 2.6668,72.0001 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen10" Geometry="F1 M 116.667,-9.15527e-005L 161.333,-9.15527e-005C 162.806,-9.15527e-005 164,1.19388 164,2.66671L 164,29.3331C 164,30.806 162.806,31.9999 161.333,31.9999L 116.667,31.9999C 115.194,31.9999 114,30.806 114,29.3331L 114,2.66671C 114,1.19388 115.194,-9.15527e-005 116.667,-9.15527e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.209617" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen11" Geometry="F1 M 116.667,35.9999L 161.333,35.9999C 162.806,35.9999 164,37.1939 164,38.6667L 164,65.3331C 164,66.806 162.806,67.9999 161.333,67.9999L 116.667,67.9999C 115.194,67.9999 114,66.806 114,65.3331L 114,38.6667C 114,37.1939 115.194,35.9999 116.667,35.9999 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen12" Geometry="F1 M 116.667,72.0001L 161.333,72.0001C 162.806,72.0001 164,73.194 164,74.6669L 164,101.333C 164,102.806 162.806,104 161.333,104L 116.667,104C 115.194,104 114,102.806 114,101.333L 114,74.6669C 114,73.194 115.194,72.0001 116.667,72.0001 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen20" Geometry="F1 M 62.6668,-0.0001297L 107.333,-0.0001297C 108.806,-0.0001297 110,1.19384 110,2.66666L 110,29.3331C 110,30.806 108.806,31.9999 107.333,31.9999L 62.6668,31.9999C 61.194,31.9999 60,30.806 60,29.3331L 60,2.66666C 60,1.19384 61.194,-0.0001297 62.6668,-0.0001297 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.209617" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen21" Geometry="F1 M 62.6668,35.9999L 107.333,35.9999C 108.806,35.9999 110,37.1939 110,38.6667L 110,65.3331C 110,66.8059 108.806,67.9999 107.333,67.9999L 62.6668,67.9999C 61.194,67.9999 60,66.8059 60,65.3331L 60,38.6667C 60,37.1939 61.194,35.9999 62.6668,35.9999 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="DynamicResource Screen22" Geometry="F1 M 62.6668,72.0001L 107.333,72.0001C 108.806,72.0001 110,73.1941 110,74.6669L 110,101.333C 110,102.806 108.806,104 107.333,104L 62.6668,104C 61.194,104 60,102.806 60,101.333L 60,74.6669C 60,73.1941 61.194,72.0001 62.6668,72.0001 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="#FFFF0000" Geometry="F1 M 54.5,-2.28882e-005L 55.5,-2.28882e-005C 56.0523,-2.28882e-005 56.5,0.447784 56.5,1L 56.5,103C 56.5,103.552 56.0523,104 55.5,104L 54.5,104C 53.9477,104 53.5,103.552 53.5,103L 53.5,1C 53.5,0.447784 53.9477,-2.28882e-005 54.5,-2.28882e-005 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.208717" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="IPC" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing Brush="#00000000" Geometry="F1 M 0,-0.000213623L 348,-0.000213623L 348,278L 0,278L 0,-0.000213623 Z "/>
<GeometryDrawing Geometry="F1 M 73.3334,64.0148L 274.667,64.0148C 275.403,64.0148 276,64.6117 276,65.3482L 276,212.667C 276,213.403 275.403,214 274.667,214L 73.3334,214C 72.597,214 72,213.403 72,212.667L 72,65.3482C 72,64.6117 72.597,64.0148 73.3334,64.0148 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="2.06475" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="#FFFFFFFF" Geometry="F1 M 73.6854,193.936L 274.238,193.936C 274.607,193.936 274.905,194.234 274.905,194.603L 274.905,212.134C 274.905,212.502 274.607,212.801 274.238,212.801L 73.6854,212.801C 73.3172,212.801 73.0187,212.502 73.0187,212.134L 73.0187,194.603C 73.0187,194.234 73.3172,193.936 73.6854,193.936 Z ">
<GeometryDrawing.Pen>
<Pen Thickness="0.198569" LineJoin="Round" Brush="#FF000000"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Brush="#FFA91830" Geometry="F1 M 168.893,206.67L 167.725,204.45L 167.871,204.379L 168.005,204.299L 168.127,204.212L 168.232,204.117L 168.325,204.015L 168.406,203.904L 168.475,203.785L 168.528,203.666L 168.573,203.544L 168.605,203.413L 168.629,203.274L 168.642,203.128L 168.646,202.977L 168.642,202.831L 168.629,202.689L 168.605,202.554L 168.573,202.423L 168.532,202.301L 168.483,202.186L 168.423,202.075L 168.345,201.96L 168.256,201.858L 168.155,201.759L 168.041,201.667L 167.916,201.584L 167.802,201.525L 167.676,201.47L 167.542,201.418L 167.4,201.375L 167.246,201.339L 167.084,201.307L 166.922,201.284L 166.751,201.264L 166.573,201.248L 166.386,201.24L 166.191,201.236L 163.482,201.236L 163.482,206.666L 165.044,206.666L 165.044,202.352L 166.325,202.352L 166.447,202.356L 166.561,202.364L 166.662,202.376L 166.755,202.396L 166.84,202.419L 166.914,202.451L 166.97,202.487L 167.019,202.53L 167.06,202.582L 167.092,202.641L 167.12,202.708L 167.141,202.788L 167.153,202.875L 167.157,202.974L 167.153,203.072L 167.141,203.159L 167.12,203.239L 167.092,203.306L 167.06,203.365L 167.019,203.413L 166.97,203.456L 166.914,203.492L 166.84,203.524L 166.755,203.547L 166.662,203.567L 166.561,203.579L 166.447,203.587L 166.325,203.591L 165.737,203.591L 165.737,203.745L 165.741,203.777L 165.745,203.809L 165.753,203.84L 165.761,203.868L 165.774,203.896L 165.786,203.927L 165.798,203.955L 165.81,203.983L 165.826,204.01L 167.23,206.674L 168.893,206.674M 185.071,206.67L 185.071,205.507L 182.24,205.507L 184.893,202.594L 184.929,202.55L 184.962,202.506L 184.99,202.459L 185.015,202.412L 185.035,202.36L 185.051,202.309L 185.063,202.257L 185.071,202.202L 185.075,202.15L 185.075,201.24L 178.354,201.24L 178.354,204.296L 175.806,201.466L 175.769,201.43L 175.729,201.394L 175.688,201.363L 175.644,201.335L 175.595,201.311L 175.546,201.288L 175.498,201.272L 175.445,201.256L 175.392,201.248L 175.339,201.24L 174.277,201.24L 174.277,205.55L 170.613,205.55L 170.613,204.485L 172.909,204.485L 172.909,203.365L 170.613,203.365L 170.613,202.356L 173.761,202.356L 173.761,201.236L 169.619,201.236L 169.579,201.24L 169.538,201.244L 169.498,201.248L 169.457,201.256L 169.408,201.268L 169.364,201.284L 169.323,201.303L 169.262,201.339L 169.206,201.383L 169.173,201.418L 169.145,201.458L 169.121,201.497L 169.1,201.537L 169.084,201.58L 169.072,201.628L 169.064,201.667L 169.06,201.707L 169.055,201.747L 169.051,201.786L 169.051,206.116L 169.055,206.156L 169.06,206.195L 169.064,206.235L 169.072,206.274L 169.084,206.322L 169.1,206.365L 169.121,206.405L 169.157,206.468L 169.202,206.524L 169.238,206.555L 169.279,206.583L 169.319,206.607L 169.36,206.623L 169.404,206.638L 169.453,206.65L 169.494,206.658L 169.534,206.666L 169.615,206.674L 175.652,206.674L 175.652,203.619L 178.199,206.449L 178.236,206.484L 178.276,206.52L 178.317,206.551L 178.362,206.579L 178.41,206.603L 178.508,206.642L 178.56,206.658L 178.613,206.666L 178.666,206.674L 179.729,206.674L 179.729,202.4L 183.076,202.4L 180.418,205.313L 180.382,205.352L 180.353,205.4L 180.325,205.447L 180.301,205.495L 180.28,205.546L 180.264,205.598L 180.252,205.649L 180.244,205.704L 180.244,206.67M 186.447,207.153L 186.447,207.295L 186.443,207.331L 186.438,207.367L 186.434,207.402L 186.426,207.434L 186.414,207.482L 186.402,207.525L 186.386,207.569L 186.366,207.612L 186.341,207.652L 186.313,207.687L 186.264,207.739L 186.207,207.782L 186.171,207.806L 186.13,207.83L 186.086,207.849L 186.049,207.865L 186.012,207.877L 185.972,207.889L 185.931,207.901L 185.891,207.909L 185.85,207.917L 185.81,207.925L 174.865,210.141L 174.828,210.149L 174.755,210.165L 174.719,210.173L 174.682,210.181L 174.646,210.185L 174.609,210.193L 174.5,210.209L 174.39,210.216L 174.281,210.22L 174.151,210.216L 174.025,210.204L 173.976,210.197L 173.932,210.189L 173.887,210.181L 173.838,210.173L 173.794,210.165L 173.745,210.153L 173.696,210.145L 162.751,207.929L 162.678,207.913L 162.642,207.905L 162.609,207.897L 162.573,207.885L 162.54,207.873L 162.508,207.861L 162.476,207.849L 162.431,207.83L 162.39,207.81L 162.35,207.786L 162.313,207.759L 162.277,207.727L 162.244,207.691L 162.204,207.636L 162.171,207.572L 162.155,207.529L 162.143,207.485L 162.131,207.438L 162.123,207.399L 162.118,207.359L 162.11,207.28L 162.11,200.611L 162.114,200.575L 162.118,200.543L 162.123,200.508L 162.131,200.472L 162.143,200.425L 162.155,200.381L 162.171,200.338L 162.192,200.294L 162.216,200.255L 162.244,200.219L 162.293,200.167L 162.35,200.124L 162.39,200.1L 162.431,200.08L 162.476,200.061L 162.512,200.045L 162.549,200.033L 162.589,200.021L 162.63,200.009L 162.67,200.001L 162.711,199.993L 162.751,199.985L 173.696,197.769L 173.733,197.761L 173.769,197.753L 173.806,197.745L 173.842,197.737L 173.879,197.729L 173.915,197.725L 173.952,197.717L 174.061,197.702L 174.171,197.694L 174.281,197.69L 174.41,197.694L 174.536,197.706L 174.585,197.714L 174.629,197.722L 174.674,197.729L 174.723,197.737L 174.767,197.745L 174.816,197.753L 174.865,197.761L 185.81,199.978L 185.883,199.993L 185.919,200.001L 185.956,200.009L 185.992,200.021L 186.025,200.033L 186.057,200.045L 186.09,200.057L 186.134,200.077L 186.175,200.096L 186.211,200.12L 186.248,200.148L 186.284,200.179L 186.317,200.215L 186.357,200.27L 186.39,200.334L 186.406,200.377L 186.418,200.421L 186.43,200.468L 186.438,200.508L 186.443,200.547L 186.451,200.626L 186.451,207.153"/>
<GeometryDrawing Brush="DynamicResource Process4" Geometry="F1 M 93.0001,76.0001L 255,76.0001L 255,181L 93.0001,181L 93.0001,76.0001 Z "/>
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</ResourceDictionary>
【问题讨论】:
【参考方案1】:您绝对不想在视图模型中管理视图资源。而且您不想更改引用键本身:这意味着您必须重新构建所有元素/XAML 以更新引用的资源键。
您想使用相同的资源密钥,例如DynamicBrushKey
并更改资源,例如它引用的 Color
。
如果您可以直接访问定义资源的ResourceDictionary
,例如,通过拥有元素或MergedDictionary
,您可以冻结它们以提高性能。也可以使用StaticResource
引用Color
资源,进一步提升性能。
由于您必须在代码隐藏中处理资源键,我建议创建静态资源键以避免魔术字符串。这也简化了重构。这些静态键可以在任何类中定义。
MainWindow.xaml.cs
partial class MainWindow : Window
public static ResourceKey DynamicBrushKey get; = new ComponentResourceKey(typeof(MainWindow), "DynamicBackgroundBrush");
public static ResourceKey BlueColorResourceKey get; = new ComponentResourceKey(typeof(MainWindow), "BlueColorResource");
public static ResourceKey RedColorResourceKey get; = new ComponentResourceKey(typeof(MainWindow), "RedColorResource");
private void AddResourcesDynamically()
// Get the ResourceDictionary or dictionaries that should define the resources
ResourceDictionary resources = Application.Current.Resources;
resources[<new_key>] = Colors.Green;
private void ChangeAndFreezeDynamicResourcesOfKnownResourceDictionay()
// Get the ResourceDictionary or dictionaries that define the resources
ResourceDictionary resources = Application.Current.Resources.Contains(MainWindow.DynamicBrushKey)
? Application.Current.Resources
: Application.Current.Resources.MergedDictionaries.First(resources => resources.Contains(MainWindow.DynamicBrushKey));
var dynamicBrush = resources[MainWindow.DynamicBrushKey] as SolidColorBrush;
var newColor = (Color)resources[MainWindow.BlueColorResourceKey];
if (dynamicBrush.IsFrozen)
dynamicBrush = dynamicBrush.Clone();
dynamicBrush.Color = newColor;
dynamicBrush.Freeze();
resources[MainWindow.DynamicBrushKey] = dynamicBrush;
// Resource should not be frozen i.e. referenced using DynamicResource
private void ChangeDynamicResourcesOfUnknownResourceDictionay()
var dynamicBrush = FindResource(MainWindow.DynamicBrushKey) as SolidColorBrush;
if (dynamicBrush.IsFrozen)
return;
var newColor = (Color)FindResource(MainWindow.BlueColorResourceKey);
dynamicBrush.Color = newColor;
MainWindow.xaml
<Window>
<Rectangle Fill="DynamicResource x:Static local:MainWindow.DynamicBrushKey"
Height="100" Width="100" />
</Window>
Colors.xaml
<ResourceDictionary>
<Color x:Key="x:Static local:MainWindow.RedColorResourceKey">Red</Color>
<Color x:Key="x:Static local:MainWindow.BlueColorResourceKey">Blue</Color>
<!-- The dynamic Brush resource -->
<SolidColorBrush x:Key="x:Static local:MainWindow.DynamicBrushKey" Color="StaticResource x:Static local:MainWindow.RedColorResourceKey" />
</ResourceDictionary>
App.xaml
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Colors.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
更具体的问题示例
public static ResourceKey IpcBrushKey get; = new ComponentResourceKey(typeof(MainWindow), "IpcBrush");
public static ResourceKey ProcessBrushKey get; = new ComponentResourceKey(typeof(MainWindow), "ProcessBrush");
public static ResourceKey Process4ResourceBrushKey get; = new ComponentResourceKey(typeof(MainWindow), "Process4Brush");
public static ResourceKey Process6ResourceBrushKey get; = new ComponentResourceKey(typeof(MainWindow), "Process6Brush");
App.xaml
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Colors.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Colors.xaml
强烈建议将ProcessX
画笔内的画笔引用为StaticResource
。它们不会更改并将它们引用为StaticResource
允许它们在添加到ResourceDictionary
时被冻结。这提高了性能。始终首选StaticResource
而不是DynamicResource
,除非资源在运行时发生变化或只能在运行时解决。
<DrawingBrush x:Key="x:Static local:MainWindow.IpcBrushKey" Stretch="Uniform">
<DrawingBrush.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing ... />
<GeometryDrawing ...>
<GeometryDrawing.Pen>
<Pen ... />
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing ...>
<GeometryDrawing.Pen>
<Pen ... />
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing ... />
<!-- Reference the dynamically changing resource -->
<GeometryDrawing Brush="DynamicResource x:Static local:MainWindow.ProcessBrushKey" ... />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<!--
The dynamic resources that will replace the dynamic placeholder ProcessBrushKey DynamicResource reference in the IPC Brush
-->
<DrawingBrush x:Key="x:Static local:MainWindow.Process4ResourceBrushKey" ...>
... Use StaticResource instead of DynamicResource
</DrawingBrush>
<DrawingBrush x:Key="x:Static local:MainWindow.Process6ResourceBrushKey" ...>
... Use StaticResource instead of DynamicResource
</DrawingBrush>
<!-- The placeholder Brush that is referenced by the IPC Brush
and itself references the Process4 Brush by default.
This refernce is later dynamically overridden (see below).
Alternatively remove this brush and initialize the resource key on
application startup -->
<DrawingBrush x:Key="x:Static local:MainWindow.ProcessBrushKey"
Drawing="Binding Source=StaticResource x:Static local:MainWindow.Process4ResourceBrushKey, Path=Drawing" />
实际的资源交换
// Sets the IPC Brush reference to Process6 Brush
private void ChangeAndFreezeDynamicResourcesOfKnownResourceDictionay()
// Get the ResourceDictionary or dictionaries that define the resources
ResourceDictionary resources = Application.Current.Resources.Contains(MainWindow.Process6ResourceBrushKey)
? Application.Current.Resources
: Application.Current.Resources.MergedDictionaries.First(resources => resources.Contains(MainWindow.Process6ResourceBrushKey));
var process6Brush = resources[MainWindow.Process6ResourceBrushKey] as Brush;
// Update the resources that is registered with the ProcessBrushKey resource key,
// which are referenced by the "IPC" Brush
resources[MainWindow.ProcessBrushKey] = process6Brush;
/*
Update IPC DrawingBrush which is the actual brush referenced by the view
*/
var ipcBrush = resources[IpcBrushKey] as Brush;
var ipcBrushClone = ipcBrushClone.Clone();
// Resources that contain DynamicResource references can't be frozen
if (ipcBrushClone.CanFreeze && !ipcBrushClone.IsFrozen)
ipcBrushClone.Freeze();
resources[IpcBrushKey] = ipcBrushClone;
【讨论】:
我在 MainWindow.xaml 中没有直接的矩形。矩形由 MainWindow.xaml 的 ContentPresenter 中的代码动态插入: myRectangle.SetResourceReference(Shape.FillProperty, myData.Type.ResourceReference) ,例如myData.Type.ResourceReference = "IPC"。 现在IPC中的图标个数可以是4个、6个或9个,应该用Process4、Process6或Process9来实现。到目前为止,一切都适用于 4 个图标。随着布局(4、6 或 9)的变化,问题开始了。我同意你的看法,并且不会在我的视图模型中管理我的视图资源。如果我将 Process4、Process6 和 Process9 移至 CodeBehind,IPC 现在会更新吗? PS:画笔通常不是简单的颜色而是复杂的绘图,其中又包含动态资源。 IPC 的细分必须通过视图模型触发,因为这取决于业务逻辑。 我一直在考虑是否应该将 IPC 实现为 UserControl 而不是资源。但这将导致重大重组,从而产生额外的工作。 “我在 MainWindow.xaml 中没有直接的矩形”:这没关系。这只是为了给你一个完整的背景来帮助你理解正在发生的事情。重点是动态引用的资源,如画笔。在您的情况下,正确的方法是定义由 ContentPresenter 自动拾取的 DataTemplate。然后,您将使用 DynamicResource 引用此 DataTemplate 中的资源。真正重要的一点是如何动态更改引用的资源。以上是关于在 ResourceDictionary 中交换 DynamicResource的主要内容,如果未能解决你的问题,请参考以下文章
ResourceDictionary 中的 IValueConverter
从 Catel WPF UserControl 中的 ResourceDictionary 中绑定
WPF 添加 Resources Dictionary 资源 一般类库项目中无法添加资源文件(ResourceDictionary)