在WPF的Page控件中按F5会刷新控件,如有需要进行屏蔽,可使用附加属性实现:
public static class DisableNavigation
{
public static bool GetDisable(DependencyObject o)
{
return (bool)o.GetValue(DisableProperty);
}
public static void SetDisable(DependencyObject o, bool value)
{
o.SetValue(DisableProperty, value);
}
public static readonly DependencyProperty DisableProperty =
DependencyProperty.RegisterAttached("Disable", typeof(bool), typeof(DisableNavigation),
new PropertyMetadata(false, DisableChanged));
public static void DisableChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var frame = (Frame)sender;
frame.Navigating += DontNavigating;
frame.NavigationUIVisibility = NavigationUIVisibility.Hidden;
}
private static void DontNavigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Refresh)
e.Cancel = true;
else if (e.NavigationMode == NavigationMode.Back)
e.Cancel = true;
}
}
使用:
<Frame NavigationUIVisibility="Hidden"
Source="{Binding MainModel.Url}"
beha:DisableNavigation.Disable="True"/>