Windows phone 8.1 Flyout隐藏行为问题

时间:2014-06-05 17:39:45

标签: xaml windows-phone-8.1

以下行为xaml出了什么问题,在运行时没有任何反应(甚至不是例外)。我试图在没有代码逻辑的情况下关闭flyout。

<AppBarButton HorizontalAlignment="Left"
                          Label="Pin to dashboard"
                          x:Name="pinBtn">
                <AppBarButton.Flyout>
                    <Flyout x:Name="flyout"
                            Placement="Full">
                        <StackPanel x:Name="stackPanel"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Top">
                            <TextBlock Text="Save as"
                                       HorizontalAlignment="Center"
                                       FontSize="16" />
                            <TextBox Width="275"
                                     Style="{StaticResource RoundedTextBox}"
                                     FontFamily="Global User Interface" />
                            <StackPanel Orientation="Horizontal"
                                        HorizontalAlignment="Center">
                                <Button Content="Save"
                                        Width="50" />
                                <Button x:Name="button"
                                        Content="Cancel"
                                        Width="50"
                                        Margin="10,0,0,0">
                                     <Interactivity:Interaction.Behaviors>
                                        <Core:EventTriggerBehavior x:Name="eventTriggerBehavior" EventName="Click">
                                            <Core:CallMethodAction TargetObject="{Binding Flyout, ElementName=pinBtn}"
                                                                   MethodName="Hide" />
                                        </Core:EventTriggerBehavior>
                                    </Interactivity:Interaction.Behaviors>
                                </Button>
                            </StackPanel>
                        </StackPanel>
                    </Flyout>
                </AppBarButton.Flyout>
            </AppBarButton>

3 个答案:

答案 0 :(得分:2)

我最近写了a blog post,了解如何使用自定义行为或操作完成此操作。

单击该按钮时,您将想要走向可视树,直到找到FlyoutPresenter,然后将其作为弹出窗口并将IsOpen设置为false;

var flyout = element.GetVisualParent<FlyoutPresenter>();
if (flyout != null)
{
    var popup = flyout.Parent as Popup;
    if (popup != null)
    {
        popup.IsOpen = false;
    }
}

答案 1 :(得分:0)

我使用自定义操作并在WinRT XAML Toolkit的帮助下使用它。

/// <summary>
/// Using MVVM to close a flyout
/// </summary>
public class CloseFlyoutAction : DependencyObject, IAction
{
    /// <inheritdoc/>
    public object Execute(object sender, object parameter)
    {
        var element = sender as DependencyObject;
        var flyout = element.GetFirstAncestorOfType<FlyoutPresenter>();
        var popup = flyout.Parent as Popup;
        if (popup != null)
        {
            popup.IsOpen = false;
        }
        return null;
    }
}

用法:

<Button HorizontalAlignment="Right" Content="Cancel">
<interactivity:Interaction.Behaviors>
    <core:EventTriggerBehavior EventName="Tapped">
        <common:CloseFlyoutAction />
    </core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>

答案 2 :(得分:-1)

我确信您必须处理代码背后的“保存”和“取消”按钮,以便为什么不关闭代码后面的弹出按钮。

<Button x:Name="buttonCancel"
     Content="Cancel" Width="50" Margin="10,0,0,0" 
     Click="buttonCancel_Click">


private void buttonCancel_Click(object sender, RoutedEventArgs e)
    {
        // Dismiss the Flyout after the action is confirmed.
        pinBtn.Flyout.Hide();
    }