未调用OnApplyTemplate

时间:2010-10-06 13:38:12

标签: silverlight silverlight-3.0 windows-phone-7

我有一个包含2个项目的解决方案:Windows Phone App和Windows Phone Class Library。类库有一个名为MessageBoxExtended的控件,它继承自ContentControl。该项目还有一个带有generic.xaml文件的Themes文件夹。该文件的Build Action设置为Page,它看起来像这样:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:KontactU.Common.WPControls;assembly=KontactU.Common.WPControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
mc:Ignorable="d">
<Style TargetType="local:MessageBoxExtended">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:MessageBoxExtended">
                <Grid x:Name="LayoutRoot">
                <StackPanel>
                        <TextBlock x:Name="lblTitle" Text="Title Goes Here" Style="{StaticResource PhoneTextTitle3Style}"/>
                        <TextBlock x:Name="lblMessage" Text="Some long message here repeated over and over again. Some long message here repeated over and over again. " TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" />
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <Button x:Name="btnLeft" Content="Button1" Click="btnLeft_Click"></Button>
                            <Button x:Name="btnCenter" Content="Button2" Click="btnCenter_Click"></Button>
                            <Button x:Name="btnRight" Content="Button3" Click="btnRight_Click"></Button>
                        </StackPanel>
                    </StackPanel>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

控制代码如下所示:

 public class MessageBoxExtended : ContentControl
{
    private TextBlock lblTitle;
    private TextBlock lblMessage;
    private Button btnLeft;
    private Button btnCenter;
    private Button btnRight;

    private bool currentSystemTrayState;

    internal Popup ChildWindowPopup
    {
        get;
        private set;
    }

    private static PhoneApplicationFrame RootVisual
    {
        get
        {
            return Application.Current == null ? null : Application.Current.RootVisual as PhoneApplicationFrame;
        }
    }

    public MessageBoxExtended()
        : base()
    {
        DefaultStyleKey = typeof(MessageBoxExtended);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        lblTitle = this.GetTemplateChild("lblTitle") as TextBlock;
        lblMessage = this.GetTemplateChild("lblMessage") as TextBlock;
        btnLeft = this.GetTemplateChild("btnLeft") as Button;
        btnCenter = this.GetTemplateChild("btnCenter") as Button;
        btnRight = this.GetTemplateChild("btnRight") as Button;

        InitializeMessageBoxExtended("Title", "Message", MessageBoxExtendedButtonType.Ok);
    }

    private void InitializeMessageBoxExtended(string title, string message, MessageBoxExtendedButtonType buttonType)
    {
        HideSystemTray();
        lblTitle.Text = title;
        lblMessage.Text = message;

        switch (buttonType)
        {
            case MessageBoxExtendedButtonType.Ok:
                btnLeft.Visibility = System.Windows.Visibility.Collapsed;
                btnRight.Visibility = System.Windows.Visibility.Collapsed;
                btnCenter.Content = "ok";
                break;
            case MessageBoxExtendedButtonType.OkCancel:
                btnCenter.Visibility = System.Windows.Visibility.Collapsed;
                btnLeft.Content = "ok";
                btnRight.Content = "cancel";
                break;
            case MessageBoxExtendedButtonType.YesNo:
                btnCenter.Visibility = System.Windows.Visibility.Collapsed;
                btnLeft.Content = "yes";
                btnRight.Content = "no";
                break;
        }
    }

    public void Show(string title, string message, MessageBoxExtendedButtonType buttonType)
    {
        if (ChildWindowPopup == null)
        {
            ChildWindowPopup = new Popup();

            try
            {
                ChildWindowPopup.Child = this;
            }
            catch (ArgumentException)
            {
                throw new InvalidOperationException("The control is already shown.");
            }
        }

        if (ChildWindowPopup != null && Application.Current.RootVisual != null)
        {
            // Configure accordingly to the type
            InitializeMessageBoxExtended(title, message, buttonType);

            // Show popup
            ChildWindowPopup.IsOpen = true;
        }
    }

    private void HideSystemTray()
    {
        // Capture current state of the system tray
        this.currentSystemTrayState = SystemTray.IsVisible;
        // Hide it
        SystemTray.IsVisible = false;
    }
}

Windows Phone应用程序通过实例化并调用Show方法引用它并在后面的代码中调用它:

MessageBoxExtended mbe = new MessageBoxExtended();
mbe.Show();

问题是从不调用OnApplyTemplate。我已经尝试评论generic.xaml中的所有行,但我得到了相同的结果。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

没关系,这是我的错。我添加了

if (lblTitle == null)
            return;

到InitializeMessageBoxExtended()方法,现在它可以正常工作。如果遵循逻辑,则在调用InitializeMessageBoxExtended()的OnApplyTemplate()之前调用构造函数,因此值为null。通过添加上面的代码,控件不会抛出异常,它会继续,当控件是VisualTree的一部分时,将调用OnApplyTemplate。

希望这可以帮助那里的任何人。