こんにちは。
Windows10でWPFのアプリケーションを開発しています。
Visual Studio 2015 Communityを使っています。
###前提・実現したいこと
event handlerを.csのコードで割り当てる場合、複数回割り当てるとeventを複数回実行してしまいます。
if (button.Clickのeventhandlerの設定をしていなければ)と条件づけたいです。
###試したこと
button.Click -= Button_Click;
回数が不明なので、これでは不十分。
###発生している問題・エラーメッセージ
event handlerを.csのコードで割り当てる場合、複数回割り当てるとeventを複数回実行してしまう。
###該当のソースコード
xaml
1<Window x:Class="MultiEvent.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:MultiEvent" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 9 <Grid> 10 <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="229,260,0,0" VerticalAlignment="Top" Width="75"/> 11 12 </Grid> 13</Window>
C#
1using System.Windows; 2 3namespace MultiEvent { 4 public partial class MainWindow : Window { 5 public MainWindow() { 6 InitializeComponent(); 7 } 8 9 private void Window_Loaded(object sender, RoutedEventArgs e) { 10 button.Click += Button_Click; 11 } 12 13 int counter = 0; 14 private void Button_Click(object sender, RoutedEventArgs e) { 15 //if (button.Clickのeventhandlerの設定をしていなければ) 16 button.Click += Button_Click; 17 MessageBox.Show(counter.ToString()); 18 } 19 } 20}
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2015
Version 14.0.25424.00 Update 3
Microsoft .NET Framework
Version 4.6.01038
インストールしているバージョン:Community
Visual C# 2015 00322-20000-00000-AA575
Microsoft Visual C# 2015
です。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/07 11:45
2016/11/07 12:26
2016/11/07 13:41
2016/11/07 15:00
2016/11/08 01:22
2016/11/08 04:57 編集
2016/11/08 07:28
2016/11/08 10:00