我使用的是Visual Studio Express,因此没有WPF Custom Control Library项目类型。使用snoop我可以看到根本没有使用该样式,但是如果我将样式放在Window.Resources中就可以了。
我的结构如下:
MyApp.Controls
- Themes (Folder)
- generic.xaml (build action "Content", no custom tool)
MyApp
*References MyApp.Controls*
在MyApp.Controls.Properties.Assemblyinfo.cs的末尾,我有:
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
generic.xaml内容:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MyApp.Controls">
<Style TargetType="{x:Type controls:LogView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:LogView}">
<TextBlock>Hello!</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
日志视图类本身具有以下内容:
public class LogView : Control
{
static LogView()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(LogView),
new FrameworkPropertyMetadata(typeof(LogView)));
}
任何可能仍然遗漏的想法,或者我做错了什么?