0

WPFを使用して.netでWindowsアプリケーションを開発するつもりです。では、実行時に動的テーマを実装する方法を説明します。私はこれについてたくさん検索しましたが、私はこれを理解することができません。app.xamlに以下の行を追加すると、Thing行を直接追加する方法が原因で、エラーが表示されます。「ExpressionDark」という名前のファイルはありませんが。

<ResourceDictionary Source="Themes/ExpressionDark.xaml"/>
***or*** 
<ResourceDictionary Source="ExpressionDark.xaml"/>

前もって感謝します :)

4

2 に答える 2

0

次のように、App.Xamlでテーマをマージできます。

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

defaulttheme.xamlファイルはプロジェクトのルートにある必要があります。テーマ用に独自のプロジェクトを構築したい場合は、次のようにリソースをマージできます。

  <ResourceDictionary Source="/MyThemeProject;component/defaulttheme.xaml" />       

ここで、defaulthteme.xamlもルートのMyThemeProjectに含まれている必要があり、メインプロジェクトからそのプロジェクトへの追加参照を追加することを忘れないでください。

構造を構築するために、好きなようにフォルダを追加できます。

<ResourceDictionary Source="/MyThemeProject;component/Folder1/Folder2/defaulttheme.xaml" />

テーマを切り替えるには、最初にMergedDictionariesをクリアしてから、新しいテーマを追加します

NewTheme = new Uri(@"/MyThemeProject;component/folder1/Folder2/bluetheme.xaml", UriKind.Relative);

Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(NewTheme); 

よろしく

フルッサー

于 2012-05-22T17:07:01.630 に答える
0

で、実行時にテーマを配置することを意味すると仮定するとDynamicThemes、リソース ディクショナリをロードするための最良の方法であり、コントロール スタイルがメイン アプリケーションまたは任意のコントロールのリソースに読み込まれます。

    public static ResourceDictionary GetThemeResourceDictionary(Uri theme)
    {
        if (theme != null)
        {
            return Application.LoadComponent(theme) as ResourceDictionary;
        }
        return null;
    }

    public static void ApplyTheme(this ContentControl control /* Change this to Application to use this function at app level */, string theme)
    {
        ResourceDictionary dictionary = GetThemeResourceDictionary(theme);

        if (dictionary != null)
        {
            // Be careful here, you'll need to implement some logic to prevent errors.
            control.Resources.MergedDictionaries.Clear();
            control.Resources.MergedDictionaries.Add(dictionary);
            // For app level
            // app.Resources.MergedDictionaries.Clear();
            // app.Resources.MergedDictionaries.Add(dictionary);

        }
    }
于 2012-05-23T06:58:52.780 に答える