Win8 store app 模拟Messagebox
Win8中没有messagebox,一般使用下面这种方式:
MessageDialog md = new MessageDialog("消息内容"); md.Title = "消息提示:"; md.ShowAsync();
下面分享一个 用Popup 模拟对话框的例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Grid > <Grid.Background> <LinearGradientBrush EndPoint="0.964,0.156" StartPoint="0.028,0.159"> <GradientStop Color="#FF1C973E" Offset="0.746"/> <GradientStop Color="White" Offset="1"/> </LinearGradientBrush> </Grid.Background> <TextBlock Name="txtTitle" HorizontalAlignment="Left" Margin="21,20,0,0" Text="提示信息:" VerticalAlignment="Top" FontSize="21.333" FontFamily="AngsanaUPC" FontWeight="Bold"/> <Button Name="btnYes" Content="Yes" Visibility="Collapsed" HorizontalAlignment="Left" Margin="84,205,0,0" VerticalAlignment="Top" Width="69" Click="btnYes_Click"/> <Button Name="btnNo" Content="No" Visibility="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="183,205,0,0" Width="70" Click="btnNo_Click"/> <Button Name="btnCancel" Content="Cancel" Visibility="Collapsed" HorizontalAlignment="Left" Margin="276,205,0,0" VerticalAlignment="Top" Width="69" Click="btnCancel_Click"/> <Button Name="btnOK" Content="OK" Visibility="Collapsed" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="158,205,0,0" Width="70" Click="btnOK_Click"/> <TextBlock Name="txtMessage" HorizontalAlignment="Left" TextWrapping="Wrap" Height="127" Margin="31,64,0,0" Text="" VerticalAlignment="Top" Width="295"/> </Grid>
后台代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Win8Popup { public sealed partial class LikeMessageBox : UserControl { public LikeMessageBox() { this.InitializeComponent(); } public delegate LikeMessageBoxResult DelegateMessageButtonClick(LikeMessageBoxResult likeMessageBoxResult); public DelegateMessageButtonClick MessageButtonClick; #region 属性 private LikeMessageBoxParameter _likeMessageBoxParameter; public LikeMessageBoxParameter likeMessageBoxParameter { set { _likeMessageBoxParameter = value; IniControls(_likeMessageBoxParameter); } } private LikeMessageBoxResult MessageBoxResult { get; set; } #endregion private void IniControls(LikeMessageBoxParameter _likeMessageBoxParameter) { btnOK.Visibility = Windows.UI.Xaml.Visibility.Collapsed; btnYes.Visibility = Windows.UI.Xaml.Visibility.Collapsed; btnNo.Visibility = Windows.UI.Xaml.Visibility.Collapsed; btnCancel.Visibility = Windows.UI.Xaml.Visibility.Collapsed; txtTitle.Text = _likeMessageBoxParameter.Title; txtMessage.Text = _likeMessageBoxParameter.MessageContent; switch (_likeMessageBoxParameter.likeMessageBoxType) { case Win8Popup.LikeMessageBoxType.IsOK: btnOK.Visibility = Windows.UI.Xaml.Visibility.Visible; btnOK.Content = _likeMessageBoxParameter.OkContent; break; case Win8Popup.LikeMessageBoxType.YesNo: btnYes.Visibility = Windows.UI.Xaml.Visibility.Visible; btnYes.Content = _likeMessageBoxParameter.YesContent; btnNo.Visibility = Windows.UI.Xaml.Visibility.Visible; btnNo.Content = _likeMessageBoxParameter.NoContent; break; case Win8Popup.LikeMessageBoxType.YesNoCancel: btnYes.Visibility = Windows.UI.Xaml.Visibility.Visible; btnYes.Content = _likeMessageBoxParameter.YesContent; btnNo.Visibility = Windows.UI.Xaml.Visibility.Visible; btnNo.Content = _likeMessageBoxParameter.NoContent; btnCancel.Visibility = Windows.UI.Xaml.Visibility.Visible; btnCancel.Content = _likeMessageBoxParameter.CancelContent; break; default: btnOK.Visibility = Windows.UI.Xaml.Visibility.Visible; btnOK.Content = _likeMessageBoxParameter.OkContent; break; } } private void btnYes_Click(object sender, RoutedEventArgs e) { this.MessageBoxResult = LikeMessageBoxResult.YES; MessageButtonClick(this.MessageBoxResult); } private void btnNo_Click(object sender, RoutedEventArgs e) { this.MessageBoxResult = LikeMessageBoxResult.NO; MessageButtonClick(this.MessageBoxResult); } private void btnOK_Click(object sender, RoutedEventArgs e) { this.MessageBoxResult = LikeMessageBoxResult.OK; MessageButtonClick(this.MessageBoxResult); } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.MessageBoxResult = LikeMessageBoxResult.CANCEL; MessageButtonClick(this.MessageBoxResult); } } public class LikeMessageBoxParameter { public string Title { get; set; } public string MessageContent { get; set; } public string YesContent { get; set; } public string NoContent { get; set; } public string OkContent { get; set; } public string CancelContent { get; set; } public LikeMessageBoxType likeMessageBoxType { get; set; } public LikeMessageBoxParameter(LikeMessageBoxType likeMessageBoxType, string Title, string MessageContent, string OkContent = "OK", string YesContent = "YES", string NoContent = "NO", string CancelContent = "Cancel") { this.Title = Title; this.MessageContent = MessageContent; this.OkContent = OkContent; this.YesContent = YesContent; this.NoContent = NoContent; this.CancelContent = CancelContent; this.likeMessageBoxType = likeMessageBoxType; } } public enum LikeMessageBoxType { /// <summary> /// 对错 /// </summary> YesNo, /// <summary> /// 确认 /// </summary> IsOK, /// <summary> /// 是,否,取消 /// </summary> YesNoCancel } public enum LikeMessageBoxResult { None, YES, NO, OK, CANCEL } }
调用实例 前台:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Page x:Class="Win8Popup.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="using:Win8Popup" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Content="弹出窗口OK" x:Name="btnOK" Width="149" Margin="346,54,0,0" VerticalAlignment="Top" Height="52" Click="btn1_Copy5_Click" /> <Button Content="弹出窗口YesNo" x:Name="btnYesNo" Width="170" Margin="933,54,0,0" VerticalAlignment="Top" Height="52" Click="btnYesNo_Click" /> <Button Content="弹出窗口YesNoCancel" x:Name="btnYesNoCancel" Width="199" Margin="696,54,0,0" VerticalAlignment="Top" Height="52" Click="btnYesNoCancel_Click" /> <Popup x:Name="tip5" IsLightDismissEnabled="True" HorizontalOffset="850" VerticalOffset="250"> <Popup.ChildTransitions> <TransitionCollection> <AddDeleteThemeTransition/> </TransitionCollection> </Popup.ChildTransitions> <local:LikeMessageBox x:Name="likeMessageXox"/> </Popup> <TextBox Name="txt" HorizontalAlignment="Left" Margin="541,185,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/> <Button Content="普通对话框" x:Name="btnOK_Copy" Width="149" Margin="146,54,0,0" VerticalAlignment="Top" Height="52" Click="btnOK_Copy_Click" /> </Grid> </Page>
调用实例 后台:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } /// <summary> /// 在此页将要在 Frame 中显示时进行调用。 /// </summary> /// <param name="e">描述如何访问此页的事件数据。Parameter /// 属性通常用于配置页。</param> protected override void OnNavigatedTo(NavigationEventArgs e) { } private void btn1_Copy5_Click(object sender, RoutedEventArgs e) { this.tip5.IsOpen = true; likeMessageXox.likeMessageBoxParameter = new LikeMessageBoxParameter(LikeMessageBoxType.IsOK, "消息提示", "大家好这里是信息提示", "确定"); likeMessageXox.MessageButtonClick -= messageButtonClick; likeMessageXox.MessageButtonClick += messageButtonClick; } private LikeMessageBoxResult messageButtonClick(LikeMessageBoxResult likeMessageBoxResult) { txt.Text = likeMessageBoxResult.ToString(); this.tip5.IsOpen = false; return likeMessageBoxResult; } private void btnYesNo_Click(object sender, RoutedEventArgs e) { this.tip5.IsOpen = true; likeMessageXox.likeMessageBoxParameter = new LikeMessageBoxParameter(LikeMessageBoxType.YesNo, "消息提示", "大家好这里是信息提示", "","是","否"); likeMessageXox.MessageButtonClick -= messageButtonClick; likeMessageXox.MessageButtonClick += messageButtonClick; } private void btnYesNoCancel_Click(object sender, RoutedEventArgs e) { this.tip5.IsOpen = true; likeMessageXox.likeMessageBoxParameter = new LikeMessageBoxParameter(LikeMessageBoxType.YesNoCancel, "消息提示", "大家好这里是信息提示", "", "是", "否","取消"); likeMessageXox.MessageButtonClick -= messageButtonClick; likeMessageXox.MessageButtonClick += messageButtonClick; } private void btnOK_Copy_Click(object sender, RoutedEventArgs e) { MessageDialog md = new MessageDialog("消息内容"); md.Title = "消息提示:"; md.ShowAsync(); } }
效果: