简体   繁体   English

WP7-控制模板-未知异常

[英]WP7 - Control Template - unknown exception

I'm working through a beginners book to wp7 and in one of the basic tutorials I have been encountering an unknown exception. 我正在研究一本针对wp7的初学者书籍,在其中一个基本教程中,我遇到了一个未知异常。

The control template defined in the Application resources (App.xaml) file is as below. 在应用程序资源(App.xaml)文件中定义的控制模板如下。

<Application.Resources>
    <Style x:Key="CustomButtonStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel>
                            <Image Source="Images\image.png" Width="200" Height="300" />
                            <TextBlock Text="{TemplateBinding Content}" TextAlignment="Center" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

Then when I create a button that uses this template eg 然后当我创建一个使用此模板的按钮时

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button x:Name="CustomButton" Width="250" Height="350" Content="Custom Button" Style="{StaticResource CustomButtonStyle}" />
        </Grid>

I always get a unknown exception raised. 我总是收到一个未知的异常。 I've narrowed it down to the 我把范围缩小到

Text="{TemplateBinding Content}"

Attribute in the control template but, why is there an exception? 控制模板中的属性,但是为什么会有异常? I have the content there, its in a string format. 我在那里有内容,它是字符串格式。 I don't know what else it may be? 我不知道还可能是什么? When I create a project I target Windows Phone 7.1. 创建项目时,我的目标是Windows Phone 7.1。 Was there some change I'm unaware of? 我不知道有什么变化吗? Any help is appreciated. 任何帮助表示赞赏。 Thanks Joe 谢谢乔

Change <TextBlock Text="{TemplateBinding Content}" TextAlignment="Center" /> to <ContentPresenter HorizontalAlignment="Center" /> <TextBlock Text="{TemplateBinding Content}" TextAlignment="Center" />更改为<ContentPresenter HorizontalAlignment="Center" />

           <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel>
                            <Image Width="200"
                                   Height="300"
                                   Source="Images\image.png" />
                            <ContentPresenter HorizontalAlignment="Center" />
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

Some details about ContentControls 有关ContentControls的一些详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM