从 Flyout 内的按钮访问 Flyout XAML
Access Flyout from button inside Flyout XAML
我觉得这是一个新手 XAML 问题,但这里是。
我想做的事情:
我正在开发一个 Windows Phone 8.1 应用程序,我想向自定义弹出窗口添加功能,以便连续两次单击弹出窗口中的相同菜单按钮,关闭弹出窗口.
示例:用户单击弹出窗口中的 "Goto settings" 菜单项。如果用户现在再次单击它,则意味着我们已经在设置菜单中,因此我只想关闭弹出窗口。
问题:
我的问题是,当单击其中的按钮时,我需要一些方法来调用弹出窗口中的代码。我没有选择在这里做任何代码隐藏,因为我正在使用 MVVMCross 和 Xamarin(而且我不想将 windows-phone 特定逻辑移动到通用平台视图模型)。
目前已尝试:
我尝试通过制作一个继承自 Button 的自定义按钮来解决此问题。当按钮加载时,一个事件被订阅到它的点击事件。发生这种情况时,我尝试通过递归查看按钮的父级(然后是父级的父级)来获取弹出窗口的句柄,直到找到它。
...这没有用,因为我从来没有将 Flyout 作为父项,而是得到了一个 Flyout-presenter(它不允许我访问我的自定义 flyout),所以我无法在此处调用我想要的功能。
我尝试制作一个继承自 Button 的自定义 "FlyoutButton"。此按钮具有可在 XAML 中设置的弹出窗口的 DependencyProperty,因此我在按钮内具有弹出窗口的句柄。
然而,当我尝试这样做时,我只得到异常 "System.Void cannot be used from C#",我真的不明白为什么我会得到。下面是我的代码的样子。
我的代码:
XAML 片段:
<Button.Flyout>
<controls:MainMenuFlyout x:Name="test"
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<controls:MainMenuButton MainMenuFlyout="{Binding ElementName=test}" Grid.Row="0"/>
<controls:MainMenuButton MainMenuFlyout="{Binding ElementName=test}" Grid.Row="0"/>
<controls:MainMenuButton MainMenuFlyout="{Binding ElementName=test}" Grid.Row="0"/>
<controls:MainMenuFlyout />
<Button.Flyout />
C#:
public class MainMenuButton : Button
{
public static DependencyProperty MainMenuFlyoutProperty = DependencyProperty.Register("MainMenuFlyout", typeof(MainMenuFlyout), typeof(MainMenuButton), new PropertyMetadata(string.Empty, MainMenuFlyoutPropertyChangedCallback));
public static void SetMainMenuFlyout(UIElement element, MainMenuFlyout value)
{
element.SetValue(MainMenuFlyoutProperty, value);
}
public MainMenuFlyout GetMainMenuFlyout(UIElement element)
{
return (MainMenuFlyout)element.GetValue(MainMenuFlyoutProperty);
}
private static void MainMenuFlyoutPropertyChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
}
}
依赖项属性声明错误。应该看起来像这样,使用常规 属性 包装器而不是静态 getter 和 setter 方法,并且 null
作为默认 属性 值,而不是 string.Empty
:
public static DependencyProperty MainMenuFlyoutProperty =
DependencyProperty.Register(
"MainMenuFlyout", typeof(MainMenuFlyout), typeof(MainMenuButton),
new PropertyMetadata(null, MainMenuFlyoutPropertyChangedCallback));
public MainMenuFlyout MainMenuFlyout
{
get { return (MainMenuFlyout)GetValue(MainMenuFlyoutProperty); }
set { SetValue(MainMenuFlyoutProperty, value); }
}
我觉得这是一个新手 XAML 问题,但这里是。
我想做的事情: 我正在开发一个 Windows Phone 8.1 应用程序,我想向自定义弹出窗口添加功能,以便连续两次单击弹出窗口中的相同菜单按钮,关闭弹出窗口. 示例:用户单击弹出窗口中的 "Goto settings" 菜单项。如果用户现在再次单击它,则意味着我们已经在设置菜单中,因此我只想关闭弹出窗口。
问题: 我的问题是,当单击其中的按钮时,我需要一些方法来调用弹出窗口中的代码。我没有选择在这里做任何代码隐藏,因为我正在使用 MVVMCross 和 Xamarin(而且我不想将 windows-phone 特定逻辑移动到通用平台视图模型)。
目前已尝试: 我尝试通过制作一个继承自 Button 的自定义按钮来解决此问题。当按钮加载时,一个事件被订阅到它的点击事件。发生这种情况时,我尝试通过递归查看按钮的父级(然后是父级的父级)来获取弹出窗口的句柄,直到找到它。 ...这没有用,因为我从来没有将 Flyout 作为父项,而是得到了一个 Flyout-presenter(它不允许我访问我的自定义 flyout),所以我无法在此处调用我想要的功能。
我尝试制作一个继承自 Button 的自定义 "FlyoutButton"。此按钮具有可在 XAML 中设置的弹出窗口的 DependencyProperty,因此我在按钮内具有弹出窗口的句柄。 然而,当我尝试这样做时,我只得到异常 "System.Void cannot be used from C#",我真的不明白为什么我会得到。下面是我的代码的样子。
我的代码: XAML 片段:
<Button.Flyout>
<controls:MainMenuFlyout x:Name="test"
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<controls:MainMenuButton MainMenuFlyout="{Binding ElementName=test}" Grid.Row="0"/>
<controls:MainMenuButton MainMenuFlyout="{Binding ElementName=test}" Grid.Row="0"/>
<controls:MainMenuButton MainMenuFlyout="{Binding ElementName=test}" Grid.Row="0"/>
<controls:MainMenuFlyout />
<Button.Flyout />
C#:
public class MainMenuButton : Button
{
public static DependencyProperty MainMenuFlyoutProperty = DependencyProperty.Register("MainMenuFlyout", typeof(MainMenuFlyout), typeof(MainMenuButton), new PropertyMetadata(string.Empty, MainMenuFlyoutPropertyChangedCallback));
public static void SetMainMenuFlyout(UIElement element, MainMenuFlyout value)
{
element.SetValue(MainMenuFlyoutProperty, value);
}
public MainMenuFlyout GetMainMenuFlyout(UIElement element)
{
return (MainMenuFlyout)element.GetValue(MainMenuFlyoutProperty);
}
private static void MainMenuFlyoutPropertyChangedCallback(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
}
}
依赖项属性声明错误。应该看起来像这样,使用常规 属性 包装器而不是静态 getter 和 setter 方法,并且 null
作为默认 属性 值,而不是 string.Empty
:
public static DependencyProperty MainMenuFlyoutProperty =
DependencyProperty.Register(
"MainMenuFlyout", typeof(MainMenuFlyout), typeof(MainMenuButton),
new PropertyMetadata(null, MainMenuFlyoutPropertyChangedCallback));
public MainMenuFlyout MainMenuFlyout
{
get { return (MainMenuFlyout)GetValue(MainMenuFlyoutProperty); }
set { SetValue(MainMenuFlyoutProperty, value); }
}