【问题标题】:How to change ContentDialog transition?如何更改 ContentDialog 过渡?
【发布时间】:2014-09-15 07:08:34
【问题描述】:

Windows Phone 8.1 为对话框和浮出控件引入了一个新的过渡,看起来像 Venetian blind。我不喜欢这个;我更喜欢它在 Windows Phone 8 中旋转/倾斜的外观。有什么办法可以改变吗?

我尝试过类似的方法:

<ContentDialog.Transitions>
    <TransitionCollection>
    </TransitionCollection>
</ContentDialog.Transitions>

但它不会改变过渡。

【问题讨论】:

标签: xaml windows-phone-8.1


【解决方案1】:

您不能覆盖 ContentDialog 等中的过渡。它们旨在成为获得标准行为的简单方法,并且将始终使用 PopupThemeTransition。

如果您想要非标准行为,那么您可以编写一个使用您自己的 TransitionCollection 的自定义控件。我找不到任何现有的样本,但请查看 Callisto 的 CustomDialog 了解一般概念。它模仿了 Windows MessageDialog,内容位于全屏调光窗口上方的水平居中条中,但切换 UI 以匹配 Windows Phone 的顶部停靠面板应该不难。

一旦您掌握了自己的控制权,就可以使用您喜欢的任何过渡。我没有方便的 WP8 设备来查看过渡是什么,但带有 Edge="Left" 的 PaneThemeTransition 听起来与您的描述相符。如果没有,那么一旦您进行了转场,您可以将其换成您喜欢的转场,或者删除所有转场并应用您自己的情节提要动画。我要么坚持使用对用户有意义的标准过渡,要么进行完全自定义,因为主题过渡可能会再次改变。

创建一个看起来正确的面板非常容易。棘手的部分是如何显示控件。如果您将它包含在您的 Xaml 中,因此它是可视化树的一部分,那么您就可以显示它。如果它不在可视树中,那么您需要将其添加到可视树中或将其托管在弹出窗口中。

这是一个快速而肮脏的 UserControl,它在 Popup 中托管自己并使用 PaneThemeTransition 从右侧滑入。

<UserControl
x:Class="App199.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App199"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
PointerReleased="UserControl_PointerReleased">
<UserControl.Transitions>
    <TransitionCollection>
        <PaneThemeTransition Edge="Left"/>
    </TransitionCollection>
</UserControl.Transitions>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition x:Name="statusBarRow" Height="0" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row ="1" Background="Black">
        <Ellipse Height="100" Width="100" Fill="Yellow" />
        <TextBlock>Lorem Ipsum</TextBlock>
        <Rectangle Height="100" Width="100" Fill="Red" />
    </StackPanel>
    <Border Grid.Row="2" Background="#7F000000" />   
</Grid>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace App199
{
    public sealed partial class MyUserControl1 : UserControl
    {
        Popup hostPopup;
        public MyUserControl1()
        {
            this.InitializeComponent();
            hostPopup = new Popup();
            hostPopup.Child = this;

            Loaded += MyUserControl1_Loaded;
            Unloaded += MyUserControl1_Unloaded;
        }

        void MyUserControl1_Loaded(object sender, RoutedEventArgs e)
        {
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        }

        void MyUserControl1_Unloaded(object sender, RoutedEventArgs e)
        {
            HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        }   

        public void Show()
        {
            this.Height = Window.Current.Bounds.Height;
            this.Width = Window.Current.Bounds.Width;

            var occRect = Windows.UI.ViewManagement.StatusBar.GetForCurrentView().OccludedRect;
            statusBarRow.Height = new GridLength(occRect.Height);

            hostPopup.IsOpen = true;
        }

        void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            if (hostPopup.IsOpen)
            { 
                hostPopup.IsOpen = false;
                e.Handled = true;
            }
        }

        private void UserControl_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            hostPopup.IsOpen = false;
        }
    }
}

【讨论】:

  • 这一切都很好。但是您失去了 ContentDialog 提供的模态对话框的真实性质。您需要禁用与下面的框架/页面的任何可能交互。只是全屏覆盖并不能解决这个问题。硬件 BackButton 也由 ContentDialog 更好地处理。使用 Popup 您必须自己处理。根据对话框和页面之间的注册顺序,这可能会变得混乱。所有这些只是为了删除这个愚蠢的动画。
猜你喜欢
  • 2017-05-03
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 2016-07-26
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 2015-03-17
相关资源
最近更新 更多