【问题标题】:How to show a full screen Modal ContentDialog in Windows Phone 8.1如何在 Windows Phone 8.1 中显示全屏模式 ContentDialog
【发布时间】:2014-08-13 20:49:50
【问题描述】:

当用户尝试登录我的应用程序时,我正在显示一个 ContentDialog,其中包含一些 TextBlock 和一个 ProgressBar。

我选择 ContentDialog 是因为它是模态的,会在应用程序收集所需信息并准备好导航到下一页之前阻止用户。

以下link 显示了可用于 Windows Phone 8.1(通用应用程序)的内容对话框类。

以下代码显示了我编写的用于显示 ContentDialog 的代码隐藏(我暂时将其放在 OnNavigatedTo 中进行测试,稍后将其移至适当的通知函数)

//Progress Bar
ProgressBar bar = new ProgressBar();
bar.IsIndeterminate = true;

//Downloading Data text
TextBlock txt = new TextBlock();
txt.Text = "Downloading data...";
txt.FontSize = 17;
txt.Foreground = new SolidColorBrush(Colors.White);
txt.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;

//This could take a few seconds
TextBlock txt2 = new TextBlock();
txt2.Text = "This could take a few seconds.";
txt2.FontSize = 17;
txt2.Foreground = new SolidColorBrush(Colors.White);
txt2.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;

//Please do not close the application.
TextBlock txt3 = new TextBlock();
txt3.Text = "Please do not close the application.";
txt3.FontSize = 17;
txt3.Foreground = new SolidColorBrush(Colors.White);
txt3.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center;

StackPanel stk = new StackPanel();
stk.Children.Add(bar);
stk.Children.Add(txt);
stk.Children.Add(txt2);
stk.Children.Add(txt3);


ContentDialog dlg = new ContentDialog();
dlg.Content = stk;
SolidColorBrush color = new SolidColorBrush(Colors.Black);
color.Opacity = 0.7;
dlg.Background = color;
dlg.Margin = new Thickness(0, 250, 0, 0);
dlg.ShowAsync();

这显示为 上面你可以看到它只覆盖了部分背景

我希望它显示为

通过制作全屏模式。

我已尝试更改高度和其他属性,但无法正常工作。

如果有人能指出我正确的方向,我会很高兴。

【问题讨论】:

    标签: c# windows-phone-8.1 win-universal-app


    【解决方案1】:

    例如,您可以将 Grid 作为 ContentDialog 的内容并将其 Height/Width 设置为 Current Window 或 LayoutGrid 的 Bounds

    // your code
    stk.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
    Grid contentGrid = new Grid();
    contentGrid.Height = Window.Current.Bounds.Height;
    contentGrid.Width = Window.Current.Bounds.Width;
    // or from LayoutGrid
    //contentGrid.Height = LayoutRoot.ActualHeight;
    //contentGrid.Width = LayoutRoot.ActualWidth;
    contentGrid.Width -= 40; // it seems that ContentDialog has some defaul margin
    contentGrid.Children.Add(stk);
    
    ContentDialog dlg = new ContentDialog();
    dlg.Content = contentGrid;
    SolidColorBrush color = new SolidColorBrush(Colors.Black);
    color.Opacity = 0.7;
    dlg.Background = color;
    await dlg.ShowAsync();
    

    您也可以考虑使用PopUp

    【讨论】:

    • 弹出不是一个选项,因为它不是模态的。
    • 您上面给出的解决方案非常有效。谢谢。但我使用的是 MVVM 模式,它几乎不鼓励任何代码隐藏。你能想出什么办法把它从后面的代码中移出来吗?我在下面有一个使用 Canvas.ZIndex 并使用可见性的分析器。但我不确定这是否更像是一种解决方法而不是正确的方法。
    • mvvm 模式不是“无代码隐藏”模式。这是一种模式,您的应用程序逻辑不应该知道您的界面。那是完全不同的。是的,您应该避免代码隐藏,以使您的 xaml 尽可能地易于维护,但如果代码隐藏可以解决您的问题,请不要犹豫
    【解决方案2】:

    我找到了一个消除背后代码的解决方案。不确定这是否更像是一种解决方法。但它让我可以轻松地使用 Binding 来决定何时显示此模式对话框以及何时隐藏它。

    这是我的 XAML

    <Grid>
    <Grid Visibility="{Binding IsDownloadingData}" Canvas.ZIndex="1" Background="{StaticResource PartiallyTransparentBlackColor}" HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <ProgressBar Grid.Row="1" IsIndeterminate="True"/>
        <TextBlock Grid.Row="2" Text="Downloading Data..." FontSize="17" Foreground="White" HorizontalAlignment="Center"/>
        <TextBlock Grid.Row="3" Text="This could take a few seconds." FontSize="17" Foreground="White" HorizontalAlignment="Center"/>
        <TextBlock Grid.Row="4" Text="Please do not close the application." FontSize="17" Foreground="White" HorizontalAlignment="Center"/>
    </Grid>
    <ScrollViewer Canvas.ZIndex="0" VerticalAlignment="Stretch" Margin="0,10,0,10">
        <!-- The XAML that should be hidden goes here (In my case LOGIN PAGE XAML) -->
    </ScrollViewer>
    

    我使用绑定来处理具有 Canvas.ZIndex="1" 的网格的可见性,并决定何时显示模式窗口。

    【讨论】:

    • 使用 UIElements 的 Visibility 播放也是一个不错的解决方案。只记得处理手机的BackKey,不会自动处理。
    • 只是备注:不要忘记将 'IsIndeterminate' 绑定到可见性。如果 Visibility 将被折叠,但 IsIndeterminate 仍为 true,它仍会在后台运行。
    【解决方案3】:

    在 Windows Phone 8.1 中,ContentDialog 具有 FullSizeDesired 布尔属性,当设置为 true 时,将以全尺寸模式打开对话框。 (MSDN link)。如果需要,您需要将Background 设置为透明颜色值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多