DCSIMG
Windows Phone 7 Custom Message Box - Essential XAML

Windows Phone 7 Custom Message Box

UPDATE: Notification box has been updated: Take a look at http://wpassets.codeplex.com, the new home place for NotificationBox and others.

I’ve wrote a custom message box called NotificationBox for the Windows Phone 7. It gives you an option to pick whatever button you like to have inside the message box. Also It provides a cool feature which is ‘Show this message again’ with isolated settings persistency so you won’t have to create this mechanism by yourself each time you want to give an option to the user suppressing a message.

The usage is similar to the regular message box, except for the fact that you can pass commands with custom actions instead of return values – then switch/ifs.

For example:

NotificationBox.Show(
                "Exit",
                "Are you sure?",
                new NotificationBoxCommand("Yes", () => { }),
                new NotificationBoxCommand("No", () => { }));
NotificationBox.Show(
                "Erase",
                "You are about to loose your data!",
                new NotificationBoxCommand("Ok", () => { }),
                new NotificationBoxCommand("Cancel", () => { }));
NotificationBox.Show(
                "Choose",
                "Choose an option.",
                new NotificationBoxCommand("Save", () => { }),
                new NotificationBoxCommand("Load", () => { }));
NotificationBox.Show(
                "Custom",
                "Click on any of the buttons below.",
                new NotificationBoxCommand("Xxx", () => { }),
                new NotificationBoxCommand("Xxx", () => { }),
                new NotificationBoxCommand("Zzz", () => { }));

Yields:

image image image image

Another version of the Show method called ShowAgain, gives an option to suppress a message:

NotificationBox.ShowAgain(
                "Show Again",
                "Uncheck the show again message and this message won't appear again.",
                "Show this message again",
                false,
                suppressed => { },
                GetType().ToString(),                
                new NotificationBoxCommand("Xxx", () => { }),
                new NotificationBoxCommand("Xxx", () => { }),
                new NotificationBoxCommand("Zzz", () => { }));
NotificationBox.ShowAgain(
                "Show Again",
                "Check the show again message so this message will appear again.",
                "Show this message again",
                true,
                suppressed => { },
                GetType().ToString(),                
                new NotificationBoxCommand("Xxx", () => { }),
                new NotificationBoxCommand("Xxx", () => { }),
                new NotificationBoxCommand("Zzz", () => { }));

The first call with false, displays the following message:

image

Now calling the same method again with the same parameters (at least with GetType().ToString()), won’t open the message in case that the user suppressed it by unchecking the check box.

The second snippet (fourth param is true) forces the message to open event if suppressed, but now the same message appears and the checkbox left unchecked. The user have an option to check it again.

I’ll be happy to have inputs, so please – don’t be shy ;)

You can download the source from CodePlex.

Published Tuesday, October 19, 2010 12:26 AM by Tomer Shamam

Comments

# re: Windows Phone 7 Custom Message Box

Thursday, October 21, 2010 3:09 PM by Martin Plante

Awesome work. I like the commands support, though some people will still prefer return values.

I see that you are ignoring the page's ApplicationBar. When your notification shows up, the user could still press AppBar buttons or menus.

In my own popups, I keep a ref on the page's ApplicationBar and set it to null until the popup is closed. I wonder if it's the good way to do it.

# re: Windows Phone 7 Custom Message Box

Thursday, October 21, 2010 11:08 PM by Tomer Shamam

Thanks Martin.

Totally forgot from the app-bar ;) and I think that hiding the app-bar is simple and good idea.

Maybe it would be better if we could display a full screen popup. Need to investigate that.

Thanks.

# re: Windows Phone 7 Custom Message Box

Friday, October 22, 2010 4:29 PM by Aiden

Either I'm missing something or the popup isn't handling the back button correctly.

Hitting back should close the popup.

# re: Windows Phone 7 Custom Message Box

Friday, October 22, 2010 7:04 PM by Tomer Shamam

Hi Aiden,

Havn't took care of that, but will.

Thanks.

# re: Windows Phone 7 Custom Message Box

Tuesday, October 26, 2010 9:21 AM by Kacey Green

Looks good.

It isn't handling back button presses correctly, nor does it handle light theming correctly, dark themes are just fine.

# re: Windows Phone 7 Custom Message Box

Tuesday, October 26, 2010 9:30 AM by Tomer Shamam

Thanks Kacey.

I'm planning to fix these issues as soon as I’ll have free time ;)

Promise to post. Keep watching.

# re: Windows Phone 7 Custom Message Box

Wednesday, October 27, 2010 11:11 PM by Oli

Thank you, this is good stuff.

Do you (or somebody else) know, why the standard MessageBox just supports the OK and Cancel button? It seems really strange to me.

# re: Windows Phone 7 Custom Message Box

Wednesday, October 27, 2010 11:28 PM by Tomer Shamam

I really don’t know why, but even though they would have given it, still they wouldn’t give custom action support with custom content.

# re: Windows Phone 7 Custom Message Box

Tuesday, November 30, 2010 10:30 PM by Phil

Thanks for providing the community with a more flexible message box!

Any idea why this won't show over a pivot control? I can use this on other pages in my app, but the pivot page refuses to display it. I get an error if I try and re-create it, so I know the popup IS there. But I don't see it. Also, if I swap in the stock messagebox, it displays just fine....

# re: Windows Phone 7 Custom Message Box

Wednesday, December 08, 2010 9:22 PM by Tomer Shamam

Hi Phil,

I've just checked it on a Pivot page, works just fine.

# re: Windows Phone 7 Custom Message Box

Tuesday, December 14, 2010 9:34 PM by Torben

paste-it.net/.../x4850e8

A XAML version for light and dark theme

# re: Windows Phone 7 Custom Message Box

Wednesday, December 15, 2010 8:39 AM by Tomer Shamam

Torben, very cool thanks ;)

# re: Windows Phone 7 Custom Message Box

Tuesday, December 28, 2010 9:56 PM by Magnus

@Torben / @Tomer: the paste-it link is taken down. Could you please post the theme fix somewhere?

/M

# re: Windows Phone 7 Custom Message Box

Friday, December 31, 2010 11:26 AM by Tomer Shamam

Sorry Magnus, somehow I don't have it.

# re: Windows Phone 7 Custom Message Box

Monday, January 03, 2011 1:44 PM by Udit

Good work but it does not work well will Landscape orientation.

# re: Windows Phone 7 Custom Message Box

Friday, January 07, 2011 2:43 PM by Udit

private static void parentPage_BackKeyPress needs to change otherwise we have to press the backbutton twice to go back

Code should be

       private static void parentPage_BackKeyPress(object sender, CancelEventArgs e)

       {

           CurrentPage.BackKeyPress -= parentPage_BackKeyPress;

           if (_popup != null && _popup.IsOpen)

           {

               ClosePopup();

               e.Cancel = true;

           }

       }

# re: Windows Phone 7 Custom Message Box

Wednesday, January 12, 2011 11:57 AM by Lee Armstrong

How do I put this into my project?  I have never done this sort of thing before?

I added the project to the solution but it can't find the Tomers namespace.

# re: Windows Phone 7 Custom Message Box

Wednesday, January 12, 2011 7:25 PM by Tomer Shamam

Hi Lee,

Basically you should download and extract my file. Open and build the project with VS2010. This should create a .dll under bin\Debug folder.

In you project add reference to this dll and look at my tester as how to use it. Also you can take a look on my tester to see the usage.

Tomer

# re: Windows Phone 7 Custom Message Box

Friday, January 14, 2011 2:35 PM by Lee Armstrong

Thanks Tomer,

In the end I added the project to the solution and built it that way and added that reference.  That way I suppose it updates.  

Any idea on the light/dark theming, the link in the posts above is broken.  Shame!

# re: Windows Phone 7 Custom Message Box

Friday, January 14, 2011 5:08 PM by Tomer Shamam

Don't have that theme, sorry.

Hope that Torben will see this and will be kind to add it again.

# re: Windows Phone 7 Custom Message Box

Saturday, January 29, 2011 11:50 PM by rosi

this message box is not working for multi thread operations. for example, i am running background theread and display multiple messaeg boxes, it throwing error as "Message is already shown.". not sure how to fix this issue.

# re: Windows Phone 7 Custom Message Box

Wednesday, March 09, 2011 11:12 PM by Nino Padrutt

Is there a possibility to use it for something like a first-use tutorial?

If i try do put the code in the constructor of the MainPage there is a NullReferenceException by:  

var currentPage = rootFrame.Content as PhoneApplicationPage;

PS: And sorry for my bad english ^^

# re: Windows Phone 7 Custom Message Box

Thursday, March 10, 2011 8:22 AM by Tomer Shamam

Yes feel free. As for the exception this operation should be delayed using the dispatcher or else. It just need the root to calculate the size. If remember correctly. Having fixed size based on layout will be just great.

# re: Windows Phone 7 Custom Message Box

Thursday, March 24, 2011 8:03 PM by Rob

How do I create a function for the buttons?

# re: Windows Phone 7 Custom Message Box

Friday, March 25, 2011 8:46 AM by Tomer Shamam

Hi Rob,

The NotificationBoxCommand has two parameters, the second one is a delegate. You can provide your function in this parameter.

The function should be something like:

  void YourFuncName();

So you should have:

  new NotificationBoxCommand("Cmd Name", YourFuncName);

Hope it helps.

# re: Windows Phone 7 Custom Message Box

Tuesday, March 29, 2011 2:17 PM by Enno

Hi,

how about license? can i use this control for apps, which i deploy in the marketplace? and what is with commercial projects? any suggestions?

enno

# re: Windows Phone 7 Custom Message Box

Tuesday, March 29, 2011 2:54 PM by Tomer Shamam

Hi Enno,

Feel free to use it in wherever you like to, just be kind and leave some credit ;)

# re: Windows Phone 7 Custom Message Box

Thursday, March 31, 2011 12:09 PM by Nitro52

Bellow are the fixes i made to support Light Mode.

in NotificationBox.Show and NotificationBox.ShowAgain

Change the Height property of _popup from CurrentPage.ActualHeight to 800

this is because ActualHeight does not update when you hide the application bar

In the Generic.xaml

Update the button collor, the xaml should look like this

   <DataTemplate x:Key="CommandTemplate">

       <Button Width="120" Foreground="#FFDDDDDD" BorderBrush="#FFDDDDDD"

               tc:NotificationBoxCommand.Command="{Binding}"

               Content="{Binding Content}" />

   </DataTemplate>  

Update the Text color, xaml should look like this

<TextBlock Text="{TemplateBinding Title}" VerticalAlignment="Center" Margin="0,0,0,2" FontSize="32" Foreground="#FFDDDDDD" />

                        <TextBlock Text="{TemplateBinding Message}" TextWrapping="Wrap" Grid.Row="1" Margin="0,2,0,2" FontSize="22" Foreground="#FFDDDDDD" />

# re: Windows Phone 7 Custom Message Box

Monday, April 11, 2011 5:42 PM by Walt

When I try to use this, nothing appears on the screen.  But if I click the button again it complains that it's already shown.

So I wonder if maybe there is something wrong with the Z-order or something?  Any suggestions?

# re: Windows Phone 7 Custom Message Box

Sunday, April 17, 2011 10:50 PM by ckruhs

Great work!

I discovered a strange effect using the control.

If I show the NotificationBox from at the loaded event handler of the apps first page, the phone's back button does not work correctly.

The first push closes the NotificationBox as expected. But the second push has no effect. Maybe because of the removal of the BackKeyPress event?

The third push closes the app.

Any ideas how to solve that problem?

Thanks

ckruhs

# re: Windows Phone 7 Custom Message Box

Thursday, May 12, 2011 1:43 PM by Panagiotis Koutsias

First of all thanks for the great control!

I think this is the proper fix for the light theme issue

In Generic.xaml, change this:

<Border x:Name="border" Background="#FF1F1F1F" VerticalAlignment="Top">

into this:

<Border x:Name="border" VerticalAlignment="Top">

  <Border.Background>

      <SolidColorBrush Color="{StaticResource PhoneContrastForegroundColor}"/>

  </Border.Background>

Also remove all the "Foreground=" from Textblocks, Buttons & Checkboxes

# re: Windows Phone 7 Custom Message Box

Thursday, May 12, 2011 6:04 PM by Tomer Shamam

hI Panagiotis Koutsias,

Thanks for sharing.

# re: Windows Phone 7 Custom Message Box

Friday, May 13, 2011 1:43 AM by Panagiotis Koutsias

@ckruhs

I used my page's Loaded event to show the MessageBox and the problem solved for me. I also modified parentPage_BackKeyPress as Udit said in a comment above.

Hope that helps!

# re: Windows Phone 7 Custom Message Box

Sunday, July 03, 2011 8:22 PM by JasonBSteele

Many thanks for this control, I will definitely be using it. I do have one suggestion:

Shouldn't  ShowAgain should call the suppression action even if the message is suppressed?

This gives a chance for the default action to be performed.

Thanks,

Jason

# re: Windows Phone 7 Custom Message Box

Monday, July 18, 2011 9:30 AM by Sumit

There is one issue with the control

If i write some code in back button then first notificationbox is displayed to user and in background that code is also executed. Is there any way to solve this issue??

# re: Windows Phone 7 Custom Message Box

Monday, July 18, 2011 10:43 PM by Tomer Shamam

Hi JasonBSteele,

This is exactly how it works:

internal void Close()

{

       if (SuppressionCallback != null)

       {

           SuppressionCallback(!ShowAgainOption);

       }

       ClosePopup();

}

# re: Windows Phone 7 Custom Message Box

Thursday, July 21, 2011 2:24 PM by giacoder (giababu2000@yahoo.com)

To make this dialog work when BACK BUTTON is pressed (i.e. inside of  OnBackKeyPress function) for example for such "Are you sure you want to loose all changes?" message, you need:

1. to cancel navigation to previous page ( e.Cancel = true; )

2. TO PLACE NotificationBox.Show(..)  INSIDE THE Dispatcher

3. to add a function to one of the button (NotificationBoxCommand) to navigate to previous page.

so the code is

       protected override void OnBackKeyPress(CancelEventArgs e) {

           e.Cancel = true;

           this.Dispatcher.BeginInvoke(() =>

               NotificationBox.Show("Leaving the page",

                   "Are you sure you want to loose all changes?",

                   new NotificationBoxCommand("Yes", () => { this.NavigationService.GoBack(); }),

                   new NotificationBoxCommand("No", () => { }))

           );

       }

# re: Windows Phone 7 Custom Message Box

Thursday, July 21, 2011 2:41 PM by giacoder (giababu2000@yahoo.com)

CORRECTION to my today's message:

To make this dialog appear when BACK BUTTON is pressed (i.e. inside of  OnBackKeyPress function) for example for such "Are you sure you want to loose all changes?" message, you need:

1. to cancel navigation to previous page ( e.Cancel = true; )

2. to add IsOpen static function to NotificationBox class. This is needed to allow hiding the dialog when it is open and back button is pressed.

3. to place NotificationBox.Show(..)  inside of Dispatcher

4. to add a function to one of the button (NotificationBoxCommand) to navigate to previous page.

so the code is

NotificationBox class:

       public static bool IsOpen {

           get {

               return _popup != null;

           }

       }

some page:

       protected override void OnBackKeyPress(CancelEventArgs e) {

           if (!NotificationBox.IsOpen) {

               e.Cancel = true;

               this.Dispatcher.BeginInvoke(() =>

                   NotificationBox.Show("Leaving the page",

                       "Are you sure you want to loose all changes?",

                       new NotificationBoxCommand("Yes", () => { this.NavigationService.GoBack(); }),

                       new NotificationBoxCommand("No", () => { }))

               );

           }

       }

# re: Windows Phone 7 Custom Message Box

Tuesday, July 26, 2011 1:52 PM by Tomer Shamam

Hi giacoder,

Thanks for your solution, though I've cleaner one, such you don't need to change anything in your page for having that. I'll post my solution later, as part of my efforts loading it to CodePlex ;)

# re: Windows Phone 7 Custom Message Box

Sunday, September 11, 2011 9:29 PM by Martijn

Hi Tomer, just a quick question: i'd like to make the buttons a bit larger. Doing so in the  Generic.xml works fine from within your demo app. But when i reference the rsulting dll form my own project: the buttons keep their old size. Any idea? Thanks!

# re: Windows Phone 7 Custom Message Box

Thursday, September 22, 2011 3:11 PM by Tomer Shamam

Giacoder, I've published a project in CodePlex.

You can use an updated version of the NotificationBox with a fix to the problem you've described.

Here is the link for the project:

http://wpassets.codeplex.com

# re: Windows Phone 7 Custom Message Box

Thursday, September 22, 2011 3:32 PM by Tomer Shamam

Hi Martin,

You should create a ControlTemplate for the NotificationBox (use Blend, its easy), then set the internal ItemsControl to use your Button data-template:

<ItemsControl ItemsSource="{TemplateBinding CommandsSource}"

  ItemTemplate="{StaticResource YourButtonDataTemplateKey}"

                                             Grid.Row="2"

                                             Margin="0,2,0,8">

I know it's lame, but it's a first draft of this control ;)

Better option is to expose "CommandTemplate" property from the notification box so the user can override it.

# re: Windows Phone 7 Custom Message Box

Tuesday, November 01, 2011 9:21 AM by Mohib Sheth

@Tomer, i tried creating a custom datatemplate but the buttons are still showing up small. Any working code?

# re: Windows Phone 7 Custom Message Box

Tuesday, November 01, 2011 9:29 AM by Mohib Sheth

I think its not related to buttons. No matter what changes i make to Generic.xaml (like changing foreground color of textblocks etc.) none of them show up in my app. It works for your Tester app though. Any ideas?

# re: Windows Phone 7 Custom Message Box

Tuesday, November 01, 2011 9:30 AM by Ong Joon Kiat

Is it possible to cutomise the buttons size and put images on it,How? Also,how do you also put a link inside the buttons,such as by clicking the "Home" Button it will go directly to Home Page.

# re: Windows Phone 7 Custom Message Box

Thursday, November 03, 2011 11:18 PM by Tomer Shamam

Hi Mohib Sheth,

First I encourage you downloading my new WP7 Assets library from Codeplex:

http://wpassets.codeplex.com/

It contains newest version of the Notification Box.

Now that you have it, look at the demo. It has what you need.

# re: Windows Phone 7 Custom Message Box

Monday, December 05, 2011 1:52 AM by zorgan

hi,

i 've just update to the new version. i change NotificationBox by NotificationTool.

How to change the color of buttons, text, title when the theme is set to light or dark ?

Do you have sample ? (send me: zorgan [at] free dot fr)

Thanks

# re: Windows Phone 7 Custom Message Box

Monday, December 05, 2011 9:29 AM by Tomer Shamam

Hi Zorgan,

Please open a discussion in CodePlex so we can share the solution.

wpassets.codeplex.com/discussions

Thanks,

Tomer

# re: Windows Phone 7 Custom Message Box

Tuesday, May 01, 2012 9:26 PM by Alex

Found one issue that will cause a fatal error.

> Popup comes up

> You click "ok" or another button added

> Code is executed

> Dialog goes away

> You press backbutton to go back one view

> Fatal error

The error is in the backbutton listener in NotificationTool class. I think it's either if(IsShown) or ClosePrompt().

I wasn't too familiar with this advanced navigation and frame coding, but after scratching my head on this for a while, I realized that the backbutton listener only is stopped on backbutton during the messagebox, not if you click one of the buttons. So I added

RootFrame.BackKeyPress -= parentPage_BackKeyPress;

in ClosePrompt()-method which is run after the commands, like you do in the backbutton listener. That way you stop listening for backbuttons after the messagebox is gone.

Thank you very much for this project, it really helped me out when making a dark theme only app, that needs custom options.

# re: Windows Phone 7 Custom Message Box

Tuesday, May 01, 2012 10:05 PM by Tomer Shamam

Thanks for sharing Alex.

# re: Windows Phone 7 Custom Message Box

Wednesday, May 16, 2012 6:33 PM by Steve

Does this NotificationBox support Orientation changes, e.g. if the user is viewing a Landscape orientation on Windows Phone 7 and the NotificationBox is displayed as a Landscape box. Currently it looks to be Portrait mode only.

# re: Windows Phone 7 Custom Message Box

Wednesday, May 16, 2012 8:46 PM by Tomer Shamam

Hi Steve, It's portrait only.

# re: Windows Phone 7 Custom Message Box

Wednesday, May 30, 2012 3:03 PM by Cristian Wilgenhoff

Hi Tomer,

Your set of assets it's amazing and it really enhances WPSDK but I encountered and issue in order to show two Notification box messages on a row. In few words, after the first is shown a second one must be shown depending on the answer for the first one but I'm just getting just the first one. The call for the second one is being made (debugging I can hit the code) but nothing happens.

If you prefer so I can leave a copy of this issue on codeplex.

Much Thanks,

Cristian

# re: Windows Phone 7 Custom Message Box

Wednesday, May 30, 2012 3:52 PM by Artem

Where can i get just dll for it, without compiling it?

# re: Windows Phone 7 Custom Message Box

Sunday, June 03, 2012 12:34 AM by Tomer Shamam

Hi Cristian,

The notification box was not design to support more than one message at a time. Currently it doesn't support kind of scenario. Maybe I'll add it in the future.

# re: Windows Phone 7 Custom Message Box

Thursday, March 07, 2013 8:00 AM by JAck

I am trying to your code implement my project but I write NotificationBOx.show() it giives me error. It suggest me only 3 (showagaintext,showagainproperty,showagainoption). What i do?

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems