Silverlight 3.0 Silverlight 3没有';t显示自定义控件';默认模板

Silverlight 3.0 Silverlight 3没有';t显示自定义控件';默认模板,silverlight-3.0,controls,Silverlight 3.0,Controls,Silverlight 3不显示我正在处理的自定义控件的默认模板 我的解决方案中有三个项目: 自定义控件。控件-Silverlight类库 CustomControl.Silverlight-Silverlight应用程序 CustomControl.Silverlight.Web-Web应用程序 在CustomControl.Controls中,我有以下类: [TemplateVisualState(Name = "Normal", GroupName = "FocusStates")] pu

Silverlight 3不显示我正在处理的自定义控件的默认模板

我的解决方案中有三个项目:

  • 自定义控件。控件-Silverlight类库
  • CustomControl.Silverlight-Silverlight应用程序
  • CustomControl.Silverlight.Web-Web应用程序
  • 在CustomControl.Controls中,我有以下类:

    [TemplateVisualState(Name = "Normal", GroupName = "FocusStates")]
    public class SampleControl : ContentControl
    {
        public SampleControl()
        {
            DefaultStyleKey = typeof(SampleControl);
        }
    
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            UpdateVisualState(false);
        }
    
        void UpdateVisualState(bool useTransitions)
        {
            VisualStateManager.GoToState(this, "Normal", useTransitions);
        }
    }
    
    Themes/generic.xaml配置为嵌入资源,并包含以下内容:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <Style TargetType="controls:SampleControl">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="controls:SampleControl">
                        <Border Background="Orange" CornerRadius="5" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
    </ResourceDictionary>
    
    
    
    最后,我在CustomControl.Silverlight中使用MainPage.xaml中的自定义控件:

    <UserControl x:Class="CustomControl.Silverlight.MainPage"
                 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:sample="clr-namespace:CustomControl.Controls;assembly=CustomControl.Controls"
                 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
        <StackPanel x:Name="LayoutRoot">
            <sample:SampleControl Width="100" Height="200" />
            <Button Width="100" Height="200" Content="bar" />
        </StackPanel>
    </UserControl>
    
    
    
    在浏览器中,SampleControl不可见(它仍然占据200px的高度,所以它就在那里),在它下面,会显示一个按钮

    我正在使用VisualStudio2008SP1+Silverlight3工具

    为了将在Themes/generic.xaml中定义的模板应用于SampleControl,我还需要做什么


    谢谢,我找到问题了。Themes/generic.xaml不应添加为“嵌入资源”,而应添加为“资源”

    愚蠢的错误耗费了我好几个小时的生命(