2016-02-15 28 views
0

還有的代碼在這裏第一次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 
+2

1的例子就是Windows窗體不是WPF –

+0

啊我的壞,你是正確的 – Skr

+0

@Marcin Iwanowski作爲一個方面的問題,這是創建和引發事件還是有更好的方法來做到這一點,WRT出版商和用戶的正確道路模式? – Skr

回答

1

按鈕在Windows窗體中的工作方式非常複雜。正如你可以看到使用反射器或類似的工具,Button類繼承自ButtonBase,並且存在受保護的接受消息的WndProc方法。如果消息包含「單擊」代碼,則此方法引發Click事件。簡而言之,添加到可視化樹中的每個控件(顯示)都會接收來自Window循環的消息,無論發生在該特定窗口上。如果控制決定該消息很重要,那麼它會引發適當的事件。