您好,我从https://www.youtube.com/watch?v=jQgwEsJISy0&t=1230s了解了事件和委托。他说,要使事件发生,我们需要三个步骤
我跟随他并在控制台中制作了该应用程序,但是由于我在WPF中工作,因此我将在此处发布我在WPF中使用的代码,其代码如下:
namespace WpfApp5
{
public delegate void step1DelegateDefinition(); // Step-1: Define a delegate
public interface INotifyOnVideoEncoded
{
event step1DelegateDefinition EventDefinedInInterface;// Step-2a: Define an event based on that delegate
}
public partial class MainWindow : Window, INotifyOnVideoEncoded
{
public event step1DelegateDefinition EventDefinedInInterface; //Step-2b: Define an event based on that delegate
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
ObservableCollection<string> NotificationText = new ObservableCollection<string>();
EventDefinedInInterface += SubscriberMethodForConection;// A method corresponding to delegate is subscribed for that event
Encode();
}
public void Encode()
{
MessageBox.Show("Encoding Video...");
Thread.Sleep(3000);
PublisherMethodForConnection(); //Step-3: Raise an event
}
public void PublisherMethodForConnection()
{
if (EventDefinedInInterface != null)
EventDefinedInInterface();
else
MessageBox.Show("No Subscriber");
}
public void SubscriberMethodForConection()
{
MessageBox.Show("MailService: Sending an email...");
}
}
}
因此,我的知识是
必须订阅事件才能使用+ =符号执行。
但是与我所知,当我使用INotifyPropertyChange中的propertychange事件时,不需要+ =符号。而且奇怪的是似乎+ =(订阅事件)是动态完成的,但是因为如果我首先初始化属性(在我的情况下,如果我初始化FirstName = Jeff和LastName = Buckley的值,在下面的代码中显示),则它将触发我代码的其他部分并在开始时显示消息“没有MyOnPropertyChanged函数可以调用的订阅服务器”。我认为这是因为事件订阅者为空(即没有像我期望的那样有+ =事件分配语句),但是稍后在加载窗口之后,似乎有事件订阅者,尽管我没有在代码上这样做。下面是我的代码实现属性已更改。
namespace UnderstandingINotifyPropertyChanged
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _FirstName;
private string _FullName;
private string _LastName;
public string FirstName
{
get { return _FirstName; }
set
{
if (_FirstName != value)
{
_FirstName = value;
MyOnPropertyChanged_PublisherMethod("FirstName");
MyOnPropertyChanged_PublisherMethod("FullName");
}
}
}
public string LastName
{
get { return _LastName; }
set
{
if (_LastName != value)
{
_LastName = value;
MyOnPropertyChanged_PublisherMethod("Lastname");
MyOnPropertyChanged_PublisherMethod("FullName");
}
}
}
public string FullName
{
get { return _FullName = _FirstName + " " + _LastName; }
}
private void MyOnPropertyChanged_PublisherMethod(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
else
MessageBox.Show("There is no Subscriber to which MyOnPropertyChanged function can call ");
}
public MainWindow()
{
InitializeComponent();
FirstName = "Jeff";
LastName = "Buckley";
this.DataContext = this;
}
}
}
所以我的困惑是在第一个代码中,我必须使用+ =分配事件,但是在第二个代码中,它无需执行+ =即可工作。我无法弄清楚在不使用+ =的情况下第二个代码如何运行,因为需要使用+ =链接到订户。
我尝试在线阅读并查看视频进行解释,但不明白这是我在这里询问的原因。到目前为止,我已经从这里学到了很多东西,谢谢您,也感谢您抽出宝贵的时间阅读此问题,在您的所有帮助下我都可以理解。
答案 0 :(得分:1)
简短答案。
第一个代码示例在EventDefinedInInterface
内订阅MainWindow.ctor()
,并在PublisherMethodForConnection
内引发。
第二个代码示例根本没有订阅PropertyChanged
。它只是在MyOnPropertyChanged_PublisherMethod
内部引发此事件。
好答案。
通常,事件旨在将外部用户通知对象内部发生的某些更改(属性已更改其值,视频已编码等)。
从技术上来说,您可以订阅自己的事件,通常这没有任何意义。例如,如果MainWindow
实例想要做某事,则在更改LastName
时,它可以处理此内部属性设置器或MyOnPropertyChanged_PublisherMethod
方法。无需事件订阅。
因此,当您想要订阅某个对象的事件时,您必须使用+=
语法添加事件处理程序。当您要引发事件时,通常会调用私有方法或受保护的方法来执行此操作,但是这不是事件处理/订阅。
下面是在C#中实现事件之前要阅读的三个链接:
答案 1 :(得分:0)
我希望上面提供的描述和下面的代码一起玩,其他人也可以从中受益
MainWindow.xaml.cs
namespace UnderstandingINotifyPropertyChanged
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
Class1 Class1Object = new Class1();
private string _FirstName;
private string _FullName;
private string _LastName;
public string FirstName
{
get { return _FirstName; }
set
{
if (_FirstName != value)
{
_FirstName = value;
RaisePropertyChanged("FirstName");
RaisePropertyChanged("FullName");
}
}
}
public string LastName
{
get { return _LastName; }
set
{
if (_LastName != value)
{
_LastName = value;
RaisePropertyChanged("Lastname");
RaisePropertyChanged("FullName");
}
}
}
public string FullName
{
get { return _FullName = _FirstName + " " + _LastName; }
}
protected virtual void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Class1 ObjectOfClass1 = new Class1();
public MainWindow()
{
InitializeComponent();
FirstName = "Jeff";
LastName = "Buckley";
this.DataContext = this;
PropertyChanged += ObjectOfClass1.MethodToBeTracked;
}
}
public class Class1
{
public void MethodToBeTracked(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("propertychanged event fired due to change in property " + e);
}
}
}
在WPF中运行的XAML
<Window x:Class="UnderstandingINotifyPropertyChanged.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UnderstandingINotifyPropertyChanged"
mc:Ignorable="d"
Title="MainWindow" Height="250" Width="400">
<StackPanel HorizontalAlignment="Center" Margin="0,30,0,0">
<StackPanel Orientation="Horizontal" Margin="10">
<Label Content="First Name : " />
<!--<TextBox Width="200" VerticalContentAlignment="Center" Text="{Binding Path=FirstName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>-->
<TextBox Width="200" VerticalContentAlignment="Center" Text="{Binding Path=FirstName,Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<Label Content="Last Name : " />
<!--<TextBox Width="200" VerticalContentAlignment="Center" Text="{Binding LastName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>-->
<TextBox Width="200" VerticalContentAlignment="Center" Text="{Binding LastName,Mode=TwoWay}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<Label Content="Full Name : " />
<!--<TextBox Width="200" VerticalContentAlignment="Center" Text="{Binding Path=FullName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>-->
<Label Width="200" VerticalContentAlignment="Center" Content="{Binding Path=FullName,Mode=OneWay}"/>
</StackPanel>
</StackPanel>
</Window>