第一个WPF有两个代码,第二个是控制台应用程序
// This is a default Form1 class of a WPF application
// and I have added a button to the form.
partial class Form1
{
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button1.Location = new System.Drawing.Point(62, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Lloyd";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
}
// here the buttons on the form are declared private
// how does the mouse click access the private button's event
private System.Windows.Forms.Button button1;
}
这是一个控制台应用程序。 在尝试这个程序时我感到困惑。 这里的PublishingBall类类似于Button类, 这个类有一个类似于Click事件的事件Throw。 在主要方法中,我试图复制鼠标点击 通过选择一些将调用PublishingBall事件的数字 但我只能访问PublishingBall事件,因为它们是在FielderSubscriber的类中公开定义的 但是在以前的代码中,按钮对象是在Form1类中私有定义的。 那么鼠标如何点击访问按钮的事件!
namespace PublisherAndSubscriber2ndNameSpace
{
class PublisherSubscriberPattern
{
static void Main(string[] args)
{
Console.WriteLine("want to throw the ball");
int decision = int.Parse(Console.ReadLine());
PublisherBall puball = new PublisherBall();
FielderSubscriber fielder = new FielderSubscriber(puball);
// this is like the basic way to raise events, how i learnt it.
// but i wanted to know how the mouse click raises button's event
if (Convert.ToBoolean(decision))
{
puball.OnThrowEvent(new BallEventArgs() { Speed = 1001 });
}
Console.WriteLine("throw a ball");
int selectBall;
while (!Console.ReadKey().Equals(ConsoleKey.Escape))
{
Console.WriteLine("Enter a ball to throw 1 2 3 4");
selectBall = int.Parse(Console.ReadLine());
// trying to simulate a mouse click by using keyboard key value
// Here I can only access PublishingBall objets because they are defined public in FielderSubscriber class
switch (selectBall)
{
case 1: fielder.pace.OnThrowEvent(new BallEventArgs() { Speed = 162 });
break;
case 2: fielder.spin.OnThrowEvent(new BallEventArgs() { Speed = 90 });
break;
case 3: fielder.googly.OnThrowEvent(new BallEventArgs() { Speed = 50 });
break;
case 4: fielder.bouncer.OnThrowEvent(new BallEventArgs() { Speed = 150 });
break;
default: Console.WriteLine("enter between 1 to 4");
break;
}
}
}
}
// Custom eventArgs defined for the PublisherBall
class BallEventArgs : EventArgs
{
public int Speed { get; set; }
}
// This class is analogous to a button class
class PublisherBall
{
public event EventHandler<BallEventArgs> Throw = delegate { };
public void OnThrowEvent(BallEventArgs e)
{
Throw(this, e);
}
}
// This class is analogous to the Form1 class where i would Instantiate the button objects(private)
class FielderSubscriber
{
public PublisherBall pace;
public PublisherBall spin;
public PublisherBall googly;
public PublisherBall bouncer;
// one way is to send the PublisherBall's object to the constructor
public FielderSubscriber(PublisherBall ball)
{
ball.Throw += ball_Throw;
InitializeComponent();
}
void InitializeComponent()
{
pace = new PublisherBall();
pace.Throw += pace_Throw;
spin = new PublisherBall();
spin.Throw += spin_Throw;
googly = new PublisherBall();
googly.Throw += googly_Throw;
bouncer = new PublisherBall();
bouncer.Throw += bouncer_Throw;
}
void bouncer_Throw(object sender, BallEventArgs e)
{
Console.WriteLine("A bouncer @{0}kmph", e.Speed);
}
void googly_Throw(object sender, BallEventArgs e)
{
Console.WriteLine("A googly @{0}kmph", e.Speed);
}
void spin_Throw(object sender, BallEventArgs e)
{
Console.WriteLine("A spin @{0}kmph", e.Speed);
}
void pace_Throw(object sender, BallEventArgs e)
{
Console.WriteLine("A pace @{0}kmph", e.Speed);
}
// An Event Handler method
void ball_Throw(object sender, BallEventArgs e)
{
if (e.Speed > 100)
Console.WriteLine("wew tough catch {0}kmph", e.Speed);
else
Console.WriteLine("easy catch {0}kmph");
}
}
}
//P.S I am a total noob , please be gentle
答案 0 :(得分:1)
Button在Windows窗体中的工作方式要复杂得多。正如您所看到的,使用Reflector或类似工具,Button类继承自ButtonBase,并且受保护的WndProc方法接受消息。如果邮件包含“单击”代码,则此方法会引发Click事件。简单来说,添加到可视树(显示)的每个控件都会从Window循环接收消息,无论在该特定窗口上发生什么。如果控件确定该消息很重要,那么它会引发适当的事件。