【问题标题】:Show ContentDialog before page navigation在页面导航之前显示 ContentDialog
【发布时间】:2014-07-23 18:38:22
【问题描述】:

我正在开发一个 Windows phone 8.1 应用程序,并尝试在导航到任何页面之前显示一个登录对话框(一个 ContentDialog)。我尝试在App.xaml.cs 中添加OnLaunched 方法中的代码,但对话框没有出现:

protected async override void OnLaunched(LaunchActivatedEventArgs e)
{
...
await new ContentDialog1().ShowAsync();
if (!rootFrame.Navigate(typeof(FirstPage), e.Arguments))
            {
                throw new Exception("Failed to create initial page");
            }
}

虽然如果我改用消息对话框,它会显示得很好。有什么建议吗?

【问题讨论】:

  • ContentDialog1 是什么?它看起来怎么样?
  • @Romasz 它是一个默认的 ContentDialog 对象。见msdn.microsoft.com/library/windows/apps/…
  • 我没有太大变化。我刚刚在我的项目中添加了一个默认的 ContentDialog 类并将其命名为 ContentDialog1。所有的 UI 和东西也是默认的。
  • 你能在页面的 Loaded 事件中显示你的对话框吗?或者你坚持OnLaunched,我不确定,但你对生命周期的怀疑可能是它不起作用的原因。
  • App.xaml.cs 中没有找到OnLoaded 函数,但在页面类中它应该可以工作。连线的事情是MessageDialog 工作得很好,我认为它应该与ContentDialog 具有相同的机制。显然有一些我还不知道的细微差别

标签: c# xaml dialog windows-phone-8.1


【解决方案1】:

我又看了看,似乎您需要在显示 ContentDialog 之前使用 Frame 填充当前窗口。因此,您需要稍微重新排序默认初始化代码。这适用于我的设置:

    Protected Overrides Async Sub OnLaunched(e As LaunchActivatedEventArgs)
      Dim rootFrame As Frame = New Frame()
      Window.Current.Content = rootFrame
      Window.Current.Activate() '//'Without this the dialog is "shown" but invisible and untouchable :D
      Dim a As New ContentDialog1
      Await a.ShowAsync()
      ...
      rootFrame.Navigate(GetType(MainPage), e.Arguments)
      ...

【讨论】:

    【解决方案2】:

    当用户使用hardwarebackkey 或与之关联的页面历史返回您的应用程序时,'App.OnLaunched' 不会运行。您应该处理“Window.Current.Activated”事件。

    AddHandler Window.Current.Activated, AddressOf WindowActivated
    
    Private Sub WindowActivated(sender As Object, e As Windows.UI.Core.WindowActivatedEventArgs)
        Select Case e.WindowActivationState
            Case Windows.UI.Core.CoreWindowActivationState.CodeActivated
                '//'TODO
            Case Windows.UI.Core.CoreWindowActivationState.Deactivated
                '//'TODO
        End Select
    End Sub
    

    PS:

    你需要导航到某个地方;也许到一个空白页。

    如果你不这样做,超时会终止你的应用程序。

    【讨论】:

    • 确实调用了 OnLaunched 方法,我尝试在其中设置断点,它被命中。当我用 MessageDialog 替换 ContentDialog 时,它也会显示出来。所以我认为这不是问题所在。
    猜你喜欢
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    相关资源
    最近更新 更多