スタイル アセンブリへの参照を追加するだけでは不十分です。WPF にリソースをマージさせるには、何かを行う必要があります。ただし、C# の 1 行 (または XAML の数行) をアプリケーション アセンブリに追加するだけでよい方法でこれを行うことができます。
最も簡単な解決策は、おそらく、共有スタイル アセンブリに厳密に型指定されたものを作成し、起動時にResourceDictionary
アプリ レベルに追加することです。ResourceDictionary
たとえば、CustomStyles.xaml
共有スタイル アセンブリに を作成し、すべてのスタイル リソースをそのファイルに (直接または 経由でMergedDictionaries
) 取り込みます。Build Action が「Page」に設定されていることを確認し、次のように要素にx:Class
ディレクティブを追加します。ResourceDictionary
<ResourceDictionary x:Class="YourNamespace.CustomStyles"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Your styles declared or imported here -->
</ResourceDictionary>
組み込みまたはサードパーティのコントロール スタイルを置き換えることを意図したスタイルの場合、スタイルを暗黙的に宣言できます。つまり、x:Key
完全にオフのままにするか、コントロールのタイプをキーとして使用できます (例: x:Key="{x:Type ComboBox}"
.
ディレクティブを追加するだけでは、Visual Studioが XAML コンテンツを実際にロードx:Class
するコンストラクターを生成するにはおそらく十分ではないため、ファイルを手動で追加し、それを呼び出すコンストラクターを与える必要があります(VS はこれを生成する必要があります)。CustomStyles()
CustomStyles.xaml.cs
InitializeComponent()
namespace YourNamespace
{
partial class CustomStyles
{
public CustomStyles()
{
InitializeComponent();
}
}
}
アプリケーションでは、このディクショナリをディクショナリにマージする必要がありますApplication.Resources
。App.xaml
必要に応じて、ファイルからこれを行うことができます。
<Application x:Class="YourNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cs="clr-namespace:YourNamespace;assembly=YourCustomStylesAssembly">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<cs:CustomStyles />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
...または、C# 側で実行できます。
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.Resources.MergedDictionaries.Add(new CustomStyles());
}
}
ここで注意が必要なのは、これらのスタイルを XAML デザイナーで機能させることです。頭に浮かぶ解決策の 1 つは、すべてのビューで設定でき、デザイナーで実行している場合にのみ適用されるカスタム添付プロパティを追加することです。
partial class CustomStyles
{
public static readonly DependencyProperty EnableDesignTimeStylesProperty =
DependencyProperty.RegisterAttached(
"EnableDesignTimeStyles",
typeof(bool),
typeof(CustomStyles),
new PropertyMetadata(
default(bool),
OnEnableDesignTimeStylesChanged));
private static CustomStyles DesignTimeResources;
private static void OnEnableDesignTimeStylesChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if (!DesignerProperties.GetIsInDesignMode(d))
return;
var element = d as FrameworkElement;
if (element == null)
return;
if (DesignTimeResources == null)
DesignTimeResources = new CustomStyles();
if ((bool)e.NewValue)
element.Resources.MergedDictionaries.Add(DesignTimeResources);
else
element.Resources.MergedDictionaries.Remove(DesignTimeResources);
}
public static void SetEnableDesignTimeStyles(
DependencyObject element,
bool value)
{
element.SetValue(EnableDesignTimeStylesProperty, value);
}
public static bool GetEnableDesignTimeStyles(DependencyObject element)
{
return (bool)element.GetValue(EnableDesignTimeStylesProperty);
}
}
次に、ビューでCustomStyles.EnableDesignTimeStyles="True"
、デザイナーにスタイル リソースを強制的にマージするように設定します。実行時にDesignerProperties.GetIsInDesignMode(d)
は と評価されfalse
、最終的にすべてのビューにスタイルの新しいコピーをロードすることはありません。アプリレベルのリソースから継承するだけです。