Windows Phone 7 - Message Box

In this article we will discuss about the MessageBox in Windows Phone 7. We will also discuss on how to customize the MessageBox in Windows Phone 7. We will use Microsoft.Xna.Framework.GamerServices to create customize MessageBox in Windows Phone 7.


Let's write some code:


Step 1: Create a Windows Phone Application project.


 


Windows Phone 7 - MessageBox


 


Step 2: Let's add three buttons in the MainPage.xaml. First two buttons will be used to demonstrate normal message box. First message box will have only OK button and second message box will have OK and Cancel both.


The third button will be used to demonstrate customized message box using Microsoft.Xna.Framework.GamerServices.


The textbox will be used to display which button of messagge box has been clicked.


<Button Content="Ok Demo" Height="72" HorizontalAlignment="Center"
Margin="0,0,0,0" Name="Ok" VerticalAlignment="Top" Width="160" Click="Ok_Click" />


<Button Content="Ok Cancel Demo" Height="72" HorizontalAlignment="Center"
Margin="0,75,0,0" Name="OkCancel" VerticalAlignment="Top" Width="260" Click="OkCancel_Click" />


<Button Content="XNA MessageBox Demo" Height="72" HorizontalAlignment="Center"
Margin="0,150,0,0" Name="XNAMessageBox" VerticalAlignment="Top" Width="340" Click="XNAMessageBox_Click" />


<TextBlock x:Name="MessageResult" Text="" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Center"Margin="0,170,0,0" Height="40"/>


Step 3: Add reference of Microsoft.Xna.Framework.GamerServices


 


Windows Phone 7 - XNA


Step 4: Add below code for first two buttons to get OK and OK and Cancel message box.


private void Ok_Click(object sender, RoutedEventArgs e)
{
   MessageBoxResult res = MessageBox.Show("OK""First Message Box"MessageBoxButton
.OK);
   if (res == MessageBoxResult
.OK)
   {
      MessageResult.Text = "Ok Selected"
;
   }
}


private void OkCancel_Click(object sender, RoutedEventArgs e)
{
   MessageBoxResult res = MessageBox.Show("OK and Cancel""First Message Box"MessageBoxButton
.OKCancel);
   if (res == MessageBoxResult
.OK)
   {
      MessageResult.Text = "Ok Selected"
;
   }
   if (res == MessageBoxResult
.Cancel)
   {
      MessageResult.Text = "Cancel Selected"
;
   }
}


MessageBoxButton has only two property OK and OKCancel


 


Windows Phone 7 - OK Message Box


MessageBoxResult has Cancel, No, None, OK and Yes.


 


Windows Phone 7 - Message Result


Now MessageBoxButton has only OK and OKCancel option but MessageBoxResult has Cancel, No, None, OK and Yes option. As there is no Yes and No is present in MessageBoxButton so there is no use of Yes and No option of MessageBoxResult. Infact None option of MessageBoxResult also doesn't have any significance.


Now let's add code for the third button.


private void XNAMessageBox_Click(object sender, RoutedEventArgs e)
{
   //0 specifies index of focus of button
   Guide.BeginShowMessageBox("XNA MessageBox Demo""This is XNA Message Box"new string[] { "Custom OK""Custom Cancel" }, 0,MessageBoxIcon.Warning, new AsyncCallback(OnMessageBoxAction), null
);
}


Signture of Guide.BeginShowMessageBox


The button name can be customized using Microsoft.Xna.Framework.GamerServices. I have used the MessageBox button caption as Custom OK and Custom Cancel.


MessageBoxIcon button has Alert, Error, None and Warning.


 


Windows Phone 7 - MessageBoxIcon


None won't generate any sound, I got same sound for rest of three Alert, Error and Warning.


To perform any action based on the option selected the asynchronous method needs to be called.


private void OnMessageBoxAction(IAsyncResult ar)
{
   int? selectedButton = Guide
.EndShowMessageBox(ar);
   switch
 (selectedButton)
   {
      case
 0:
         Deployment.Current.Dispatcher.BeginInvoke(() => MessageResult.Text = "Custom Ok Selected"
 );
         break
;

      case
 1:
         Deployment.Current.Dispatcher.BeginInvoke(() => MessageResult.Text = "Custom Cancel Selected"
);
         break
;

      default
:
         break
;
   }
}


Step 5: Now run the application.


 

Windows Phone 7 - MessageBox 

You will get below different kind of message box on click of any of the above button.


 


Windows Phone 7 - MessageBox OK Demo  Windows Phone 7 - MessageBox OKCancelDemo  Windows Phone 7 - MessageBox Custom OKCancelDemo

This ends the article of Windows Phone 7 message box.

Like us if you find this post useful. Thanks
<div class="top-opt-button top-opt-button-text element-hidden" id="top-opt-text-pager"> <div class="top-button-icon-bg"><span class="icon-ion mdi mdi-microphone-message top-button-icon-text"></span> </div> <span class="top-button-text-text">文字广播</span> </div> <div class="btn-opt-box btn-opt-intercom" id="opt-intercom"> <div class="btm-opt-icon-bg"><span class="fa fa-phone btn-opt-icon"></span></div> <span class="btn-opt-text">对讲管理</span> </div> <div class="top-opt-button element-hidden" id="top-opt-pager"> <div class="top-button-icon-bg"><span class="fa fa-bullhorn top-button-icon"></span></div> <span class="top-button-text">实时广播</span> </div> <div class="top-opt-button" id="top-opt-timer"> <div class="top-button-icon-bg"><span class="icon-ion mdi mdi-timetable btn-opt-icon"></span></div> <span class="top-button-text">定时管理</span> </div> <div class="top-opt-button" id="top-opt-group"> <div class="top-button-icon-bg"><span class="fa fa-heart btn-opt-icon"></span></div> <span class="top-button-text">设备分组</span> </div> <div class="btn-opt-box btn-opt-devices" id="opt-devices"> <div class="btm-opt-icon-bg"><span class="icon-ion ion-laptop btn-opt-icon"></span></div> <span class="btn-opt-text">设备管理</span> </div> <div class="btn-opt-box btn-opt-tasks element-show" id="opt-tasks"> <div class="btm-opt-icon-bg"><span class="icon-ion mdi mdi-playlist-play btn-opt-icon"></span></div> <span class="btn-opt-text">系统任务</span> </div> <div class="top-opt-button element-hidden" id="top-opt-alarm"> <div class="top-button-icon-bg"><span class="fa ion-fireball top-button-icon"></span></div> <span class="top-button-text">气象预警</span> </div> <div class="top-opt-button element-hidden" id="top-opt-offlinemap"> <div class="top-button-icon-bg"><span class="icon-ion mdi mdi-map-marker-radius-outline top-button-icon"></span></div> <span class="top-button-text">电子地图</span> </div> <div class="top-opt-button element-hidden" id="top-opt-dashboard"> <div class="top-button-icon-bg"><span class="icon-ion mdi mdi-monitor-dashboard top-button-icon"></span> </div> <span class="top-button-text">数据监控</span> </div> <div class="top-opt-button" id="top-opt-monitor"> <div class="top-button-icon-bg"><span class="fa fa-video-camera top-button-icon"></span></div> <span class="top-button-text">监控</span> </div> <div class="top-opt-button element-hidden" id="top-opt-adap"> <div class="top-button-icon-bg"><span class="icon-ion mdi mdi-refresh-auto top-button-icon"></span></div> <span class="top-button-text">AI自适应</span> </div> <div class="btn-opt-box btn-opt-settings" id="opt-settings"> <div class="btm-opt-icon-bg"><span class="fa fa-cogs btn-opt-icon"></span></div> <span class="btn-opt-text">系统设置</span> </div>像这种按钮怎么做动态的?
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值