2

アプリケーションのコードの乱雑さを最小限に抑える方法があるかどうかを知りたいだけです。

私はこれに似たコードを書きました:

   private void btnNext_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnNext.Opacity = 1;
    }

    private void btnNext_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnNext.Opacity = 0.5;
    }

    private void btnShowAll_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnShowAll.Opacity = 1;
    }

    private void btnShowAll_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnShowAll.Opacity = 0.5;
    }

    private void btnPrev_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnPrev.Opacity = 1;
    }

    private void btnPrev_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnPrev.Opacity = 0.5;
    }

    private void btnSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearch.Opacity = 1;
    }

    private void btnSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearch.Opacity = 0.5;
    }

    private void btnSearchStore_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearchStore.Opacity = 1;
    }

    private void btnSearchStore_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnSearchStore.Opacity = 0.5;
    }

    private void btnCloseSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnCloseSearch.Opacity = 1;
    }

    private void btnCloseSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnCloseSearch.Opacity = 0.5;
    }

    private void btnHome_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnHome.Opacity = 1;
    }

    private void btnHome_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        btnHome.Opacity = 0.5;
    }

などなど...

最初に実行される「関数」を作成する必要がありますか?または、それらを「整理」できるようにするために、別のクラスを作成する必要がありますか?

助言がありますか?

4

5 に答える 5

15

これらすべての関数を2に書き直すことができます。

private void FadeBtn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    Button btn = (Button)sender;
    btn.Opacity = 1;
}

private void FadeBtn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    Button btn = (Button)sender;
    btn.Opacity = 0.5;
}

次に、すべてのボタンMouseEnterおよびMouseLeaveイベントをそれらの関数にポイントします。

于 2010-03-12T16:06:20.500 に答える
2

ChangeButtonOpacity メソッドが必要です。

private void ChangeButtonOpacity(Button button, double newOpacity)
{
    button.Opacity = newOpacity;
}

また、ハンドラーを次のように実装できます。

private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    ChangeButtonOpacity((Button)sender, 1);
}

private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    ChangeButtonOpacity((Button)sender, 0.5);
}

このようにして、必要なハンドラーは 2 つだけになります。

于 2010-03-12T16:09:19.057 に答える
0

マウス入力イベントを作成し、すべてのボタンを登録します。メソッド内で、送信者オブジェクトをボタンとしてキャストしていることに気付くでしょう。したがって、ボタンがそれを呼び出すものは何でも、この不透明度アクションを実行できます。

private void ButtonMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
  Button button = (Button) sender;
  button.Opacity = 1;
}
于 2010-03-12T16:06:39.347 に答える
-1

私が見る限り、あなたの場合、次のように短縮できます。

private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
    (sender as Button).Opacity = 1;
}

private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
    (sender as Button).Opacity = 0.5;
}

デザイナーでは、ボタンごとに新しいイベント ハンドラーを作成する代わりに、これらのイベント ハンドラーを選択できます。

于 2010-03-12T16:08:36.923 に答える
-1

ボタンの Tag プロパティを他に使用しない場合は、おそらく使用できます。次に、次の操作を実行できます。

private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    (sender as Button).Opacity = (double)((sender as Button).Tag); 
} 

private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) 
{ 
    (sender as Button).Opacity = 0.5;
} 

これにより、2 つのハンドラーのみを使用して、異なるボタンに異なる値を設定できます。

于 2010-03-12T16:54:08.753 に答える