其中是Windows Phone 8用户控件的VisibleChanged事件

本文关键字:控件 VisibleChanged 事件 用户 Windows Phone | 更新日期: 2023-09-27 18:00:31

我在MSDN页面上发现了不止一个对VisibleChanged事件的引用,该事件表示它适用于Windows Phone 8平台。然而,当我试图通过Intellisense访问我正在构建的顶级用户控件(使用"this"关键字)或LayoutRoot网格时,我看不到它。我通过对象浏览器进行了全面搜索,也看不到任何内容。它在哪里?当用户控件可见时,我只需要执行某些任务,当不可见时,需要它们停止。

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.visiblechanged(v=vs.110).aspx

其中是Windows Phone 8用户控件的VisibleChanged事件

您的参考指的是适用于Windows的Windows窗体应用程序,而不是Windows Phone。您在Windows Phone上询问的属性是Visibility(而不是Visible),因此您应该查找VisibilityChanged,但它不存在。

但是,您可以创建自己的控件,方法是将事件所需的控件子类化,然后使用新控件。例如:

public class MyControl : SomeOtherControl
{
    public MyControl()
    {
        DefaultStyleKey = typeof(MyControl);
    }
    public static readonly DependencyProperty VisibilityChangedProperty = DependencyProperty.Register(
        "VisibilityChanged",
        typeof(string),
        typeof(MyControl),
        new PropertyMetadata("VisibilityChanged event handler"));
    public event VisibilityChangedEventHandler VisibilityChanged;
    public delegate void VisibilityChangedEventHandler(object sender, EventArgs e);
    public new Visibility Visibility
    {
        get { return base.Visibility; }
        set
        {
            if (base.Visibility == value) return;
            base.Visibility = value;
            VisibilityChanged(this, new EventArgs());
        }
    }
}

当然,如果您对控件的源代码有完全的控制权,那么就不用考虑继承了。

如果您想为任意控件设置VisibilityChanged事件,有一个稍微复杂的解决方法。首先,围绕该控件创建一个包装类,该包装类将具有自己的Visibility属性,并绑定到目标的Visibility特性。当你有了它,你可以监听通知。

首先,扩展方法:

public static FrameworkElementExtender Extender(this FrameworkElement element)
{
    return new FrameworkElementExtender(element);
}

帮助程序事件参数类:

public class VisibilityChangedEventArgs : EventArgs
{
    public Visibility Visibility { get; private set; }
    public VisibilityChangedEventArgs(Visibility visibility)
    {
        this.Visibility = visibility;
    }
}

现在是实际的包装器:

public class FrameworkElementExtender : FrameworkElement
{
    public new static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(
        "Visibility", typeof(Visibility), typeof(FrameworkElementExtender), new PropertyMetadata(default(Visibility), PropertyChangedCallback));
    private static void PropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        ((FrameworkElementExtender)o).OnVisibilityChanged((System.Windows.Visibility)e.NewValue);
    }
    public new Visibility Visibility
    {
        get { return (Visibility)GetValue(VisibilityProperty); }
        set { SetValue(VisibilityProperty, value); }
    }
    private readonly FrameworkElement _element;
    public FrameworkElementExtender(FrameworkElement element)
    {
        _element = element;
        var binding = new Binding("Visibility")
        {
            Source = element,
        };
        SetBinding(VisibilityProperty, binding);
    }
    public event EventHandler<VisibilityChangedEventArgs> VisibilityChanged;
    protected virtual void OnVisibilityChanged(Visibility visible)
    {
        var handler = VisibilityChanged;
        if (handler != null)
            handler(this, new VisibilityChangedEventArgs(visible));
    }
}

正如你所看到的,我们监听目标依赖属性的变化,当我们检测到变化时,我们会触发自己的事件。使用它非常简单:

control.Extender().VisibilityChanged += OnVisibilityChanged;
control.Visibility = Visibility.Collapsed;
control.Visibility = Visibility.Visible;