主頁 > 移動端開發 > 在ResourceDictionary中交換DynamicResource

在ResourceDictionary中交換DynamicResource

2021-12-02 16:11:41 移動端開發

我有一個包含許多 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>

uj5u.com熱心網友回復:

您絕對不想在視圖模型中管理視圖資源。并且您不想更改參考鍵本身:這意味著您必須重建所有元素/XAML 以更新參考的資源鍵。
您想使用相同的資源鍵,例如DynamicBrushKey,更改資源,例如Color它參考的資源

如果您可以直接訪問ResourceDictionary定義資源的 ,例如,通過擁有元素 或MergedDictionary,您可以凍結它們以提高性能。您還可以Color使用參考資源StaticResource,這進一步提高了性能。
由于您必須在代碼隱藏中處理資源鍵,我建議創建靜態資源鍵以避免魔術字串。這也簡化了重構。這些靜態鍵可以在任何類中定義。

主視窗.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;
  }
}

主視窗.xaml

<Window>
  <Rectangle Fill="{DynamicResource {x:Static local:MainWindow.DynamicBrushKey}}"
             Height="100" Width="100"  />
</Window>

顏色檔案

<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>

應用程式.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");

應用程式.xaml

<Application>
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Colors.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

Colors.xaml
It is highly recommended to reference the brushes inside the ProcessX brushes as StaticResource. They don't change and referencing them as StaticResource allows them to be frozen when added to the ResourceDictionary. This improves perfomance. Always prefer StaticResource over DynamicResource except when the resource is changing during runtime or can only be resolved during runtime.

<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}" />

The actual resource swapping

  // 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] = dynamicBrush;
  }

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371403.html

標籤:小白 xaml 捆绑 资源词典 动态资源

上一篇:覆寫來自ASP.NETCore應用程式的ElasticSearch日志中的fields.RequestId

下一篇:帶有附加屬性和自定義模板的ListViewGridViewColumnHeader

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【從零開始擼一個App】Dagger2

    Dagger2是一個IOC框架,一般用于Android平臺,第一次接觸的朋友,一定會被搞得暈頭轉向。它延續了Java平臺Spring框架代碼碎片化,注解滿天飛的傳統。嘗試將各處代碼片段串聯起來,理清思緒,真不是件容易的事。更不用說還有各版本細微的差別。 與Spring不同的是,Spring是通過反射 ......

    uj5u.com 2020-09-10 06:57:59 more
  • Flutter Weekly Issue 66

    新聞 Flutter 季度調研結果分享 教程 Flutter+FaaS一體化任務編排的思考與設計 詳解Dart中如何通過注解生成代碼 GitHub 用對了嗎?Flutter 團隊分享如何管理大型開源專案 插件 flutter-bubble-tab-indicator A Flutter librar ......

    uj5u.com 2020-09-10 06:58:52 more
  • Proguard 常用規則

    介紹 Proguard 入口,如何查看輸出,如何使用 keep 設定入口以及使用實體,如何配置壓縮,混淆,校驗等規則。

    ......

    uj5u.com 2020-09-10 06:59:00 more
  • Android 開發技術周報 Issue#292

    新聞 Android即將獲得類AirDrop功能:可向附近設備快速分享檔案 谷歌為安卓檔案管理應用引入可安全隱藏資料的Safe Folder功能 Android TV新主界面將顯示電影、電視節目和應用推薦內容 泄露的Android檔案暗示了傳說中的谷歌Pixel 5a與折疊屏新機 谷歌發布Andro ......

    uj5u.com 2020-09-10 07:00:37 more
  • AutoFitTextureView Error inflating class

    報錯: Binary XML file line #0: Binary XML file line #0: Error inflating class xxx.AutoFitTextureView 解決: <com.example.testy2.AutoFitTextureView android: ......

    uj5u.com 2020-09-10 07:00:41 more
  • 根據Uri,Cursor沒有獲取到對應的屬性

    Android: 背景:呼叫攝像頭,拍攝視頻,指定保存的地址,但是回傳的Cursor檔案,只有名稱和大小的屬性,沒有其他諸如時長,連ID屬性都沒有 使用 cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATIO ......

    uj5u.com 2020-09-10 07:00:44 more
  • Android連載29-持久化技術

    一、持久化技術 我們平時所使用的APP產生的資料,在記憶體中都是瞬時的,會隨著斷電、關機等丟失資料,因此android系統采用了持久化技術,用于存盤這些“瞬時”資料 持久化技術包括:檔案存盤、SharedPreference存盤以及資料庫存盤,還有更復雜的SD卡記憶體儲。 二、檔案存盤 最基本存盤方式, ......

    uj5u.com 2020-09-10 07:00:47 more
  • Android Camera2Video整合到自己專案里

    背景: Android專案里呼叫攝像頭拍攝視頻,原本使用的 MediaStore.ACTION_VIDEO_CAPTURE, 后來因專案需要,改成了camera2 1.Camera2Video 官方demo有點問題,下載后,不能直接整合到專案 問題1.多次拍攝視頻崩潰 問題2.雙擊record按鈕, ......

    uj5u.com 2020-09-10 07:00:50 more
  • Android 開發技術周報 Issue#293

    新聞 谷歌為Android TV開發者提供多種新功能 Android 11將自動填表功能整合到鍵盤輸入建議中 谷歌宣布Android Auto即將支持更多的導航和數字停車應用 谷歌Pixel 5只有XL版本 搭載驍龍765G且將比Pixel 4更便宜 [圖]Wear OS將迎來重磅更新:應用啟動時間 ......

    uj5u.com 2020-09-10 07:01:38 more
  • 海豚星空掃碼投屏 Android 接收端 SDK 集成 六步驟

    掃碼投屏,開放網路,獨占設備,不需要額外下載軟體,微信掃碼,發現設備。支持標準DLNA協議,支持倍速播放。視頻,音頻,圖片投屏。好點意思。還支持自定義基于 DLNA 擴展的操作動作。好像要收費,沒體驗。 這里簡單記錄一下集成程序。 一 跟目錄的build.gradle添加私有mevan倉庫 mave ......

    uj5u.com 2020-09-10 07:01:43 more
最新发布
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:40:31 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:40:11 more
  • 歡迎頁輪播影片

    如圖,引導開始,球從上落下,同時淡入文字,然后文字開始輪播,最后一頁時停止,點擊進入首頁。 在來看看效果圖。 重力球先不講,主要歡迎輪播簡單實作 首先新建一個類 TextTranslationXGuideView,用于影片展示 文本是類似的,最后會有個圖片箭頭影片,布局很簡單,就是一個 TextVi ......

    uj5u.com 2023-04-20 08:39:36 more
  • 【FAQ】關于華為推送服務因營銷訊息頻次管控導致服務通訊類訊息

    一. 問題描述 使用華為推送服務下發IM訊息時,下發訊息請求成功且code碼為80000000,但是手機總是收不到訊息; 在華為推送自助分析(Beta)平臺查看發現,訊息發送觸發了頻控。 二. 問題原因及背景 2023年1月05日起,華為推送服務對咨詢營銷類訊息做了單個設備每日推送數量上限管理,具體 ......

    uj5u.com 2023-04-20 08:39:13 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:16:23 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:16:15 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:15:46 more
  • iOS從UI記憶體地址到讀取成員變數(oc/swift)

    開發除錯時,我們發現bug時常首先是從UI顯示發現例外,下一步才會去定位UI相關連的資料的。XCode有給我們提供一系列debug工具,但是很多人可能還沒有形成一套穩定的除錯流程,因此本文嘗試解決這個問題,順便提出一個暴論:UI顯示例外問題只需要兩個步驟就能完成定位作業的80%: 定位例外 UI 組 ......

    uj5u.com 2023-04-19 09:14:53 more
  • FIDE重磅更新!性能飛躍!體驗有禮!

    FIDE 開發者工具重構升級啦!實作500%性能提升,誠邀體驗! 一直以來不少開發者朋友在社區反饋,在使用 FIDE 工具的程序中,時常會遇到諸如加載不及時、代碼預覽/渲染性能不如意的情況,十分影響開發體驗。 作為技術團隊,我們深知一件趁手的開發工具對開發者的重要性,因此,在2023年開年,FinC ......

    uj5u.com 2023-04-19 09:14:08 more
  • 游戲內嵌社區服務開放,助力開發者提升玩家互動與留存

    華為 HMS Core 游戲內嵌社區服務提供快速訪問華為游戲中心論壇能力,支持玩家直接在游戲內瀏覽帖子和交流互動,助力開發者擴展內容生產和觸達的場景。 一、為什么要游戲內嵌社區? 二、游戲內嵌社區的典型使用場景 1、游戲內打開論壇 您可以在游戲內繪制論壇入口,為玩家提供沉浸式發帖、瀏覽、點贊、回帖、 ......

    uj5u.com 2023-04-19 09:08:34 more