C# 属性更改时引发事件
当属性的值更改时,我需要引发一个事件。在我的例子中,这是webView.Source被更改的时候。我无法生成派生类,因为该类已标记为密封。有什么办法可以举办一个活动吗 谢谢。您可以使用包装原始类并引发装饰属性的事件。您可以使用包装原始类并引发装饰属性的事件 属性更改时引发事件 对于此场景,您可以创建一个C# 属性更改时引发事件,c#,events,uwp,derived-class,sealed,C#,Events,Uwp,Derived Class,Sealed,当属性的值更改时,我需要引发一个事件。在我的例子中,这是webView.Source被更改的时候。我无法生成派生类,因为该类已标记为密封。有什么办法可以举办一个活动吗 谢谢。您可以使用包装原始类并引发装饰属性的事件。您可以使用包装原始类并引发装饰属性的事件 属性更改时引发事件 对于此场景,您可以创建一个dependencPropertyWatcher来检测dependencProperty已更改事件。下面是可以直接使用的工具类 public class DependencyPropertyWat
dependencPropertyWatcher
来检测dependencProperty
已更改事件。下面是可以直接使用的工具类
public class DependencyPropertyWatcher<T> : DependencyObject, IDisposable
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(object),
typeof(DependencyPropertyWatcher<T>),
new PropertyMetadata(null, OnPropertyChanged));
public event DependencyPropertyChangedEventHandler PropertyChanged;
public DependencyPropertyWatcher(DependencyObject target, string propertyPath)
{
this.Target = target;
BindingOperations.SetBinding(
this,
ValueProperty,
new Binding() { Source = target, Path = new PropertyPath(propertyPath), Mode = BindingMode.OneWay });
}
public DependencyObject Target { get; private set; }
public T Value
{
get { return (T)this.GetValue(ValueProperty); }
}
public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
DependencyPropertyWatcher<T> source = (DependencyPropertyWatcher<T>)sender;
if (source.PropertyChanged != null)
{
source.PropertyChanged(source.Target, args);
}
}
public void Dispose()
{
this.ClearValue(ValueProperty);
}
}
公共类DependencyPropertyWatcher:DependencyObject,IDisposable
{
公共静态只读从属属性ValueProperty=
从属属性。寄存器(
“价值”,
类型(对象),
类型(DependencyPropertyWatcher),
新的PropertyMetadata(null,OnPropertyChanged));
公共事件相关的PropertyChangedEventHandler属性已更改;
公共DependencyPropertyWatcher(DependencyObject目标,字符串属性路径)
{
这个。目标=目标;
BindingOperations.SetBinding(
这
ValueProperty,
新绑定(){Source=target,Path=newpropertypath(PropertyPath),Mode=BindingMode.OneWay});
}
公共依赖对象目标{get;private set;}
公共价值
{
获取{return(T)this.GetValue(ValueProperty);}
}
公共静态void OnPropertyChanged(对象发送方,DependencyPropertyChangedEventArgs参数)
{
DependencyPropertyWatcher源=(DependencyPropertyWatcher)发送方;
if(source.PropertyChanged!=null)
{
source.PropertyChanged(source.Target,args);
}
}
公共空间处置()
{
这是ClearValue(ValueProperty);
}
}
用法
var watcher = new DependencyPropertyWatcher<string>(this.MyWebView, "Source");
watcher.PropertyChanged += Watcher_PropertyChanged;
private void Watcher_PropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
var-watcher=newdependencPropertywatcher(this.MyWebView,“Source”);
watcher.PropertyChanged+=watcher\u PropertyChanged;
私有无效观察者\u属性已更改(对象发送者,DependencyPropertyChangedEventArgs e)
{
}
属性更改时引发事件
对于此场景,您可以创建一个dependencPropertyWatcher
来检测dependencProperty
已更改事件。下面是可以直接使用的工具类
public class DependencyPropertyWatcher<T> : DependencyObject, IDisposable
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(object),
typeof(DependencyPropertyWatcher<T>),
new PropertyMetadata(null, OnPropertyChanged));
public event DependencyPropertyChangedEventHandler PropertyChanged;
public DependencyPropertyWatcher(DependencyObject target, string propertyPath)
{
this.Target = target;
BindingOperations.SetBinding(
this,
ValueProperty,
new Binding() { Source = target, Path = new PropertyPath(propertyPath), Mode = BindingMode.OneWay });
}
public DependencyObject Target { get; private set; }
public T Value
{
get { return (T)this.GetValue(ValueProperty); }
}
public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
DependencyPropertyWatcher<T> source = (DependencyPropertyWatcher<T>)sender;
if (source.PropertyChanged != null)
{
source.PropertyChanged(source.Target, args);
}
}
public void Dispose()
{
this.ClearValue(ValueProperty);
}
}
公共类DependencyPropertyWatcher:DependencyObject,IDisposable
{
公共静态只读从属属性ValueProperty=
从属属性。寄存器(
“价值”,
类型(对象),
类型(DependencyPropertyWatcher),
新的PropertyMetadata(null,OnPropertyChanged));
公共事件相关的PropertyChangedEventHandler属性已更改;
公共DependencyPropertyWatcher(DependencyObject目标,字符串属性路径)
{
这个。目标=目标;
BindingOperations.SetBinding(
这
ValueProperty,
新绑定(){Source=target,Path=newpropertypath(PropertyPath),Mode=BindingMode.OneWay});
}
公共依赖对象目标{get;private set;}
公共价值
{
获取{return(T)this.GetValue(ValueProperty);}
}
公共静态void OnPropertyChanged(对象发送方,DependencyPropertyChangedEventArgs参数)
{
DependencyPropertyWatcher源=(DependencyPropertyWatcher)发送方;
if(source.PropertyChanged!=null)
{
source.PropertyChanged(source.Target,args);
}
}
公共空间处置()
{
这是ClearValue(ValueProperty);
}
}
用法
var watcher = new DependencyPropertyWatcher<string>(this.MyWebView, "Source");
watcher.PropertyChanged += Watcher_PropertyChanged;
private void Watcher_PropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
var-watcher=newdependencPropertywatcher(this.MyWebView,“Source”);
watcher.PropertyChanged+=watcher\u PropertyChanged;
私有无效观察者\u属性已更改(对象发送者,DependencyPropertyChangedEventArgs e)
{
}
这不起作用,因为我没有访问WebView类的权限。我不能这样做:`public class MyClass:WebView',要包装原始类,我需要这样做。我不确定外部代码如何通过WebView类或接口使用您的WebView?您可以像WebView类一样执行MyClass:FrameworkElement、IWebView、IWebView2、IWebView3、IWebView4、IWebView5。我无法使用IWebView,因为我在C#UWP中。我唯一的网络浏览器是WebView,我不能像你说的那样做。这不起作用,因为我没有访问WebView类的权限。我不能这样做:`public class MyClass:WebView',要包装原始类,我需要这样做。我不确定外部代码如何通过WebView类或接口使用您的WebView?您可以像WebView类一样执行MyClass:FrameworkElement、IWebView、IWebView2、IWebView3、IWebView4、IWebView5。我无法使用IWebView,因为我在C#UWP中。我唯一的网络浏览器是WebView,我不能像你说的那样做。