【问题标题】:Is it possible to remove or change default flyout transition animation?是否可以删除或更改默认的弹出过渡动画?
【发布时间】:2016-02-17 22:16:59
【问题描述】:

当您为按钮控件添加浮出控件时,例如,在您点击该按钮后,浮出控件会显示一个过渡动画,该动画取决于浮出控件的 Placement 属性。如果 Placement 设置为 Top,动画看起来就像从那个按钮下拉。在我的情况下,我想要么完全删除动画,要么通过该按钮将其扩展为 x 和 y。该怎么做?

【问题讨论】:

    标签: xaml win-universal-app flyout


    【解决方案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();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      与 Rob Caplan 和 Jayden 所说的相反,实际上可以覆盖 ContentDialog 的默认转换。

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

      我知道这是一个迟到的答案,这可能是一个有点笨拙的解决方案,但因为我一直在寻找它,所以我希望我能帮助别人。

      示例代码(无 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
                      };
                  }
              };
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多