ContextFlyout 在10586或10240的使用

时间:2021-04-23 20:04:45

虽然ContextFlyout只能在红石以上版本使用,但可以采用附加属性的方法手动写一个

    public static class ContextFlyoutSetter
{
public static Windows.UI.Xaml.Controls.Primitives.FlyoutBase GetCompatibleContextFlyout(FrameworkElement obj)
{
return (Windows.UI.Xaml.Controls.Primitives.FlyoutBase)obj.GetValue(CompatibleContextFlyoutProperty);
} public static void SetCompatibleContextFlyout(FrameworkElement obj, Windows.UI.Xaml.Controls.Primitives.FlyoutBase value)
{
obj.SetValue(CompatibleContextFlyoutProperty, value);
}
static readonly bool issupport = Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "ContextFlyout");
// Using a DependencyProperty as the backing store for ContextFlyout. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CompatibleContextFlyoutProperty =
DependencyProperty.RegisterAttached("CompatibleContextFlyout", typeof(Windows.UI.Xaml.Controls.Primitives.FlyoutBase), typeof(FrameworkElement), new PropertyMetadata(null, CompatibleContextFlyoutChanged));
private static void CompatibleContextFlyoutChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var uie=obj as FrameworkElement;
if(uie!=null)
{
if(issupport )
{
uie.ContextFlyout = e.NewValue as Windows.UI.Xaml.Controls.Primitives.FlyoutBase;
}
else
{
Windows.UI.Xaml.Controls.Primitives.FlyoutBase.SetAttachedFlyout(uie, e.NewValue as Windows.UI.Xaml.Controls.Primitives.FlyoutBase);
if (e.NewValue!=null)
{
uie.Holding += Uie_Holding;
uie.RightTapped += Uie_RightTapped;
}
else
{
uie.Holding -= Uie_Holding;
uie.RightTapped -= Uie_RightTapped;
}
}
}
} private static void Uie_Holding(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e)
{
Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
} private static void Uie_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
Windows.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAttachedFlyout(sender as FrameworkElement);
}
}

最后使用的时候只需要这样

                            <localControls:ContextFlyoutSetter.CompatibleContextFlyout>
<MenuFlyout> </MenuFlyout>
</localControls:ContextFlyoutSetter.CompatibleContextFlyout>