是否可以删除或更改默认的弹出过渡动画?

时间:2016-02-17 22:16:59

标签: xaml win-universal-app flyout

当您为按钮控件添加弹出按钮时,例如,在您点击该按钮后,弹出按钮将显示一个过渡动画,该过渡动画取决于弹出按钮的“放置”属性。如果“放置”设置为“顶部”,则动画看起来就像是从该按钮下拉。在我的情况下,我想要完全删除动画或使其从该按钮按x和y展开。怎么做?

2 个答案:

答案 0 :(得分:1)

有一个类似的案例:How to change ContentDialog transition

正如Rob Caplan所说,你不能覆盖ContentDialog中的转换等。它们被设计为获得标准行为的简单方法,并将始终使用PopupThemeTransition。

因此,如果您需要非标准行为,则可以编写自定义控件,该控件使用您自己的TransitionCollection或不使用Transition

例如:

<UserControl
    x:Class="Is_it_possible_to_remove_or_change_default_flyou.MyUserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Is_it_possible_to_remove_or_change_default_flyou"
    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"
   LostFocus="UserControl_LostFocus">

    <UserControl.Transitions>
        <TransitionCollection>
             <!--Add the Transition that you want-->
        </TransitionCollection>
    </UserControl.Transitions>

    <Grid Name="MyGrid" BorderBrush="Gray" BorderThickness="1" Background="LightGray">
        <StackPanel>
            <TextBox Name="MyText" Text="Hello"></TextBox>
            <TextBox Text="!"></TextBox>
        </StackPanel>
    </Grid>
</UserControl>

背后的代码:

 public sealed partial class MyUserControl1 : UserControl
 {
     public Popup hostPopup;
     public double actHei;
     public double actWei;

     public MyUserControl1()
     {
         this.InitializeComponent();
         hostPopup = new Popup();
         hostPopup.Child = this;
     }

     public void Show()
     {
         hostPopup.IsOpen = true;
         actHei = MyGrid.ActualHeight;
         actWei = MyGrid.ActualWidth;
         MyText.Focus(FocusState.Pointer);
     }

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

在MainPage代码后面:

public sealed partial class MainPage : Page
{
    public MyUserControl1 popup;

    public MainPage()
    {
        this.InitializeComponent();
        popup = new MyUserControl1();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (popup.hostPopup.IsOpen != true)
        {
            var ttv = MyButton.TransformToVisual(Window.Current.Content);
            Point screenCoords = ttv.TransformPoint(new Point(0, 0));
            popup.hostPopup.VerticalOffset = screenCoords.Y - 100;
            popup.hostPopup.HorizontalOffset = screenCoords.X;
            popup.Show();
        }
    }
}

答案 1 :(得分:0)

与Rob Caplan和Jayden所说的相反,实际上有可能覆盖ContentDialog的默认过渡。

您可以创建一个扩展ContentDialog的类,并订阅Loading事件,如下面的代码所示。 在那里,您可以为Transitions覆盖新创建的弹出窗口中的ContentDialog

我知道这是一个较晚的答案,这可能有点棘手,但是由于我一直在寻找,所以希望我能为您提供帮助。

示例代码(无xaml):

public class CustomContentDialog : ContentDialog {
    public CustomContentDialog() {
        Loading += (sender, args) => {
            // gets the background rectangle and the popup containing the ContentDialog
            var popups = VisualTreeHelper.GetOpenPopups(Window.Current);

            // remove all transitions and childtransitions from the popups
            foreach (var p in popups) {
                p.ChildTransitions = new TransitionCollection {
                    // your transitions
                };
                p.Transitions = new TransitionCollection {
                    // your transitions
                };
            }
        };
    }
}