Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:40:58+00:00 2026-05-17T15:40:58+00:00

I have a solution with 2 projects: Windows Phone App and a Windows Phone

  • 0

I have a solution with 2 projects: Windows Phone App and a Windows Phone Class Library. The class library has a control called MessageBoxExtended which inherits from ContentControl. The project also has a Themes folder with a generic.xaml file. The file has the Build Action set to Page and it looks like this:

<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>

The control code looks like this:

 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;
    }
}

The Windows Phone App references it and calls it in the code behind by instantiating it and calling the Show method:

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

The problem is that the OnApplyTemplate is never called. I’ve tried commenting out all the lines in the generic.xaml, but I get the same result.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:40:58+00:00Added an answer on May 17, 2026 at 3:40 pm

    Never mind, it was my mistake. I added

    if (lblTitle == null)
                return;
    

    to the InitializeMessageBoxExtended() method and now it works. If you follow the logic the constructor is called before the OnApplyTemplate() which calls the InitializeMessageBoxExtended() and therefore the values are null. By adding the code above the control doesn’t throw an exception, it continues and when the control is part of the VisualTree the OnApplyTemplate is called.

    Hope this helps anyone out there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XAML page in a separate Windows Phone class library. The library
I have a solution with several projects most of which are code or control
I have a solution with several projects, where the startup project has a post-build
I have a solution containing several projects that have migrated from VS 2003, 2005,
I have a solution consisting of five projects, each of which compile to separate
I have a solution with several projects: MyLibrary (a VB.NET dll) .dll app.config MyService
I have a solution made up of multiple projects which have various dependencies on
I have a VisualStudio Solution with 5 projects. I have 3/5 projects that has
We have a .Net 2.0 web app and are converting the solution and projects
I current have a solution with an Azure WCF service and a Windows Phone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.