是否可以删除或更改默认的弹出式过渡动画?
Is it possible to remove or change default flyout transition animation?
当您为按钮控件添加弹出按钮时,例如,在您点击该按钮后,弹出按钮会显示一个过渡动画,该动画取决于弹出按钮的放置 属性。如果 Placement 设置为 Top,则动画看起来像是从该按钮下拉。在我的例子中,我想要么完全删除动画,要么让它从那个按钮扩展 x 和 y。怎么做?
还有一个类似的案例: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();
}
}
}
与 Rob Caplan 和 Jayden 声明的相反,实际上可以覆盖 ContentDialog 的默认转换。
您可以创建一个 class 扩展 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
};
}
};
}
}
当您为按钮控件添加弹出按钮时,例如,在您点击该按钮后,弹出按钮会显示一个过渡动画,该动画取决于弹出按钮的放置 属性。如果 Placement 设置为 Top,则动画看起来像是从该按钮下拉。在我的例子中,我想要么完全删除动画,要么让它从那个按钮扩展 x 和 y。怎么做?
还有一个类似的案例: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();
}
}
}
与 Rob Caplan 和 Jayden 声明的相反,实际上可以覆盖 ContentDialog 的默认转换。
您可以创建一个 class 扩展 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
};
}
};
}
}