現象:WPF 類庫輸出 DLL,引用專項中自定義控件樣式不生效,Themes/Generic.xaml 未被打包/讀取。
WPF 查找控件默認樣式流程:
DefaultStyleKeyProperty.OverrideMetadata。DefaultStyleKey 類型尋找 Themes/Generic.xaml 中對應 Style。Generic.xaml 未嵌入資源、或 Build Action 非 Resource,則找不到。Themes/Generic.xaml 存在,屬性:static MyControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl))); }Generic.xaml 中定義:<Style TargetType="{x:Type local:MyControl}"> ... </Style>註冊後,**無法在外部顯式引用 Generic.xaml 中的 Style**(如 Style={StaticResource MyStyle})。
規避:將可複用樣式單獨放入 Styles/Shared.xaml,ResourceDictionary.MergedDictionaries 合併引用。