Coding4Fun WP7 Message Prompt in depth
published on: 4/18/2011 | Views: N/A | Tags: C4FToolkit
by WindowsPhoneGeek
In this article I am going to talk about the MessagePrompt control from the Coding4fun Toolkit in details. I will explain everything about the main features, available public API, and will give lots of examples in different scenarios.
Basically MessagePrompt is an UI component that derives from the toolkit`s UserPrompt class . As its name says it is a kind of extended popup that displays a message. MessagePrompt can display different content like Title, composite Body content, custom buttons, etc .
NOTE: In this article I will use the latest version 1.3.2 of the toolkit assemblies(the dll is attached to the sample project at the end of the article).
Getting Started
To begin using MessagePrompt first add a reference to the Coding4Fun.Phone.Controls.dll assembly.
NOTE: You can download this assembly from here: Coding4Fun Toolkit .
NOTE: You will also have to add a reference to Microsoft.Phone.Controls.Toolkit from the Silverlight Toolkit because of some dependencies. (This assembly is included in the download package of the Coding4Fun Toolkit. It is also attached to the sample project at the end of the article).
Generally the MessagePrompt is designed to be used in code. The sample code should looks like:
private void btnSimpleMessage_Click(object sender, RoutedEventArgs e)
{
var messagePrompt = new MessagePrompt
{
Title = "Simple Message",
Message = "This is a demo of the Coding4Fun MessagePrompt.",
};
messagePrompt.Show();
}
Here is how a basic structure of a MessagePrompt looks like:
Key Properties
- ActionPopUpButtons
This is a dependency property of type List<Button>. It represents a list of action buttons. You can add whatever button you prefer here and as a result it will appear immediately next to the default OK button. (see Example 4 below!)
- Body
This is a dependency property of type object. It gets or sets the Body of the MessagePrompt control.
- IsAppBarVisible
This is a dependency property of type bool. It determine whether the app bar is visible or not.
- IsCancelVisible
This is a dependency property of type bool. It determine whether the cancel button is visible or not.
- IsOpen
This is a read only dependency property of type bool. Determine whether the popup is opened or not.
- HasGesturesDisabled
This is a dependency property of type bool. It determines whether Gestures are disabled or not disabled.The default value is true.
NOTE: With the current Gesture Service in the Silverlight Toolkit (Feb 2011 release), if two controls are overlapped and the bottom control has a listener attached, events will still bubble through with no way to cancel it without putting on a listener. In a control that is called PopUp, it is self defeating to have this bubble through effect happening. If the SL toolkit corrects the behavior then the Coding4fun team will remove this as it would no longer be needed. This would also remove the dependency on the Silverlight Toolkit.
- Message
This is a dependency property of type string. It gets or sets the Message of the MessagePrompt control.
- Overlay
This is a dependency property of type Brush. It gets or sets the overlay of the MessagePrompt control.
- Title
This is a dependency property of type string. It gets or sets the Title of the MessagePrompt control.
Key Methods and Events
- Show()
This method shows the MessagePrompt control.
- Completed event
This event Occurs when the popup is closed.
Examples
1. Sample Usage:
Here is an example shows how to use this control. You can use IsCancelVisible property to Show/Hide the MessagePrompt `s cancel button. As to the IsAppBarVisible property of MessagePrompt: if you set this property to True as a result the Application Bar will be visible when the popup is opened, otherwise if you set it to False the App Bar will not be visible.
private void bntAdvancedMessage_Click(object sender, RoutedEventArgs e)
{
var messagePrompt = new MessagePrompt
{
Title = "Advanced Message",
Body = new TextBlock { Text = "Bory of the Coding4Fun MessagePrompt.", Foreground = new SolidColorBrush(Colors.Green),FontSize = 30.0, TextWrapping=TextWrapping.Wrap},
IsAppBarVisible = true,
IsCancelVisible = true
};
messagePrompt.Show();
}
2.Advanced Body in XAML using UserControl
Generally the MessagePrompt is designed to be used in code but sometimes users want to put some composite elements into the popup body so in this case it is easy to define these element is XAML. Here is one possible solution with UserControl. Just create a new UserControl named BodyUserControl.xaml and add the following code into it:
<Border BorderBrush="YellowGreen" BorderThickness="2">
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Margin="0,0,0,10">
<TextBlock Text="Body declared in UserControl XAML" HorizontalAlignment="Center"/>
<Image Source="AppIcon.png" Height="80" Width="80" HorizontalAlignment="Center"/>
<Border Background="YellowGreen" Height="100" Width="400">
<TextBlock Text="This is the first text in the Body section. Another Body content here" TextWrapping="Wrap" VerticalAlignment="Center"/>
</Border>
</StackPanel>
</Border>
After that go back to MainPage.xaml.cs and add the following code:
private void btnAdvancedMessageXAML_Click(object sender, RoutedEventArgs e)
{
MessagePrompt messagePrompt = new MessagePrompt();
messagePrompt.Title = "UserControl test";
messagePrompt.Body = new BodyUserControl();
messagePrompt.Show();
}
3. Adding Custom Buttons and colors
This example demonstrates how to add a sample Custom Button to the ActionPopUpButtons collection of the MessagePrompt . When the button is pressed the background of the MessagePrompt will be changed:
MessagePrompt messagePrompt;
private void btncustomizedMessage_Click(object sender, RoutedEventArgs e)
{
messagePrompt = new MessagePrompt();
messagePrompt.Message = "Some Message.";
Button customButton = new Button() { Content = "Custom Button" };
customButton.Click += new RoutedEventHandler(customButton_Click);
messagePrompt.ActionPopUpButtons.Add(customButton);
messagePrompt.Show();
}
void customButton_Click(object sender, RoutedEventArgs e)
{
messagePrompt.Background = new SolidColorBrush(Colors.Gray);
}
That was all about the MessagePrompt control from the Coding4fun Toolkit in depth. You can find the full source code here:
I hope that the article was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Nice work
posted by: J on 4/30/2011 10:30:02 PM
Hi,
Ive implemented the tool into my application and it is working well!
Is there anyway to link an on click event to the standard buttons so when a user actually clicks on the tick or the cross the application can respond accordingly?
Failing that, is there anyway to hide the tick button? So that i can create 2 x custom buttons?
Thanks
RE:Hiding button, RE:Standard buttons click
posted by: winphonegeek on 5/2/2011 10:17:59 AM
You can hide the tick button by simply clear the ActionPopUpButtons collection before adding the new buttons like for example:
MessagePrompt messagePrompt;
private void btncustomizedMessage_Click(object sender, RoutedEventArgs e)
{
messagePrompt = new MessagePrompt();
messagePrompt.Message = "Some Message.";
messagePrompt.ActionPopUpButtons.Clear();
Button customButton = new Button() { Content = "Custom Button" };
customButton.Click += new RoutedEventHandler(customButton_Click);
messagePrompt.ActionPopUpButtons.Add(customButton);
messagePrompt.Show();
}
void customButton_Click(object sender, RoutedEventArgs e)
{
//do something
}
As to the question "Is there anyway to link an on click event to the standard buttons so when a user actually clicks on the tick or the cross the application can respond accordingly?" Can you please explain in more details what are you trying to achieve(I do you mean by "respond correctly") so that we could be able to answer your question correctly?
RE: Standard buttons click
posted by: J on 5/2/2011 1:56:12 PM
Thanks for the quick response!
With the Message Prompt popup, i would like to ask the user a question. If the user accepts the question they would click on the tick button, this would then cause the application to run a set of commands.
However, if the user declines/rejects the question they would then click on the cross button. This would then cause the application to run a different set of commands.
I hope this is a little clearer as to what i am trying to achieve?
Closing popup
posted by: J on 5/2/2011 3:34:40 PM
I have been busy implementing the use of the custom buttons and have managed to get them working the way i want them too.
As i am hiding the standard buttons with the use of: messagePrompt.ActionPopUpButtons.Clear();
Is there any way of closing the popup when the user clicks on one of the custom buttons?
Thanks
Follow up
posted by: J on 5/15/2011 4:28:17 PM
Hi there,
Was just wondering if there was any update on the previous 2 posts.
Cheers
RE:Close popup
posted by: winphonegeek on 5/15/2011 9:20:25 PM
In the latest release of the Coding4Fun toolkit there are a new Hide() method exposed that close the popup. You need something like:
cancelButton.Click += (sender, e) =>
{
messagePrompt.Hide();
};
Just download the latest assemblies: http://coding4fun.codeplex.com/
Back press when messageprompt is open
posted by: Punit Ganshani on 5/15/2011 10:54:56 PM
Hi,
I am opening a MessagePrompt and trying to press BACK button and instead of closing the popup, it is going back. Please let me know if I am doing wrong anywhere?
public static void Show(string message, params MessageBoxExCommand[] commands)
{
MessagePrompt prompt = new MessagePrompt();
prompt.Body = new MessageBoxExControl
{
Message = message
};
prompt.Title = new AppInfo().Title;
prompt.IsCancelVisible = false;
prompt.IsAppBarVisible = false;
prompt.ActionPopUpButtons = new System.Collections.Generic.List<Button>();
for (int i = 0; i < commands.Length; i++)
{
Button button = new Button();
button.Margin = new Thickness(0, -10, 0, 0);
button.Content = commands[i].Content;
button.Tag = commands[i];
button.Click += new RoutedEventHandler(delegate(object sender, RoutedEventArgs args)
{
MessageBoxExCommand currentCommand = (MessageBoxExCommand)((Button)sender).Tag;
if (currentCommand.OnClick != null)
currentCommand.OnClick();
prompt.OnCompleted(new PopUpEventArgs<string, PopUpResult>
{
Result = prompt.Value,
PopUpResult = PopUpResult.Ok
});
});
prompt.ActionPopUpButtons.Add(button);
}
prompt.Show();
}
On the back button press, it should close the prompt
MessagePrompt result?!?
posted by: Kafar on 11/9/2011 5:34:37 PM
Hello,
I need to know how to manage the result choice (ie. "OK", "Cancel") in code. All sample I read not shows this. Please reply me. Thanks
-Kafar
RE: MessagePrompt result
posted by: winphonegeek on 11/10/2011 11:50:32 AM
You can get the result by handling the Completed event and then check the PopUpResult property from the event arguments.
Handling the back key pressed and a bug?
posted by: George on 4/10/2012 12:23:31 PM
Hello,
First of all when you run the following an exception is been thrown
messagePromt.Show(); messagePromt.Hide(); messagePromt.Show();
And the other thing is that i want to handle the back key pressed when the popup is showed and either close the application or the popup depending on a value.
Messageprompt
posted by: Don K on 4/16/2012 1:15:06 PM
well how can i add 2 buttons in a messageprompt 1 below the other and not together
message Prompt Result
posted by: Greengrenadez on 5/2/2012 1:16:48 AM
For the life of me, I can not get the results from this message box. I sure would appreciate a sample input_completed event. Thank you
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
