Изменить источник ресурсов в среде выполнения (изменить тему оформления материала) - PullRequest
0 голосов
/ 05 ноября 2019

Я использую Material design в моем wpf. Я меняю тему (Светлый / Темный) во время выполнения. эта работа правильная. но я использую Dialoghost в главном окне, как этот код

    <Window x:Class="MyWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    Background="{DynamicResource MaterialDesignPaper}"
    TextElement.Foreground="{DynamicResource MaterialDesignBody}">
<materialDesign:DialogHost Identifier="RootDialog">
  <Grid>
     <!--some code-->
  </Grid>
</materialDesign:DialogHost>

Мой контентhosthost использует некоторый usercontrol, а в usercontol я использую materialDesign: Card. но темная тема не работает в карте. Так что я использую Resourcedictionary для DialogHost, как это

<materialDesign:DialogHost.Resources>
        <Style TargetType="materialDesign:Card" BasedOn="{StaticResource {x:Type materialDesign:Card}}">
            <Style.Resources>
                <ResourceDictionary>
                    <ResourceDictionary.MergedDictionaries>
                        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.dark.xaml" />
                    </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>
            </Style.Resources>
            <Setter Property="Background" Value="{DynamicResource MaterialDesignCardBackground}" />
        </Style>
    </materialDesign:DialogHost.Resources>

, и это работает правильно, но когда моя тема темная. Когда я меняю тему, следует изменить эту строку.

<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.light.xaml" />

Но я хочу изменить тему во время выполнения, что невозможно. Когда я пишу так, вторая строка запускается и не помогает.

<ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.dark.xaml" />
      <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.light.xaml" />
</ResourceDictionary.MergedDictionaries>

Затем проверяйте этот путь.

ResourceDictionary ResourceDic = new ResourceDictionary();
if (BoolValue)
      ResourceDic.Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.dark.xaml", UriKind.RelativeOrAbsolute);
else
      ResourceDic.Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.light.xaml", UriKind.RelativeOrAbsolute);
Application.Current.Resources.MergedDictionaries.Add(ResourceDic);

, но я не знаю, как определить typeof (MaterialDesign: Card)для этого, и я не знаю, как использовать его для UserControl. Пожалуйста, помогите мне. Как сделать это во время выполнения?

...