C# 自定义事件不起作用,有人有建议吗?
C# Custom Event not working, does someone have a suggestion?
我目前正在尝试通过编写自己的自定义事件来理解 C# 中的事件。我的目标是在用户向控制台输入内容后触发一个事件。如果字符串等于“--start”,应该会发生一些事情。我目前没有在我的自定义事件的构造函数中到达我的断点。我希望你能帮助我。
这是我的代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Welcome to the BlackJack Console Game!{Environment.NewLine}" );
Console.WriteLine($"You get 500 Credits. The minimal Bet is 10 and the maximal 50!{Environment.NewLine}");
Console.WriteLine($"You can check your Creditcount with --credits{Environment.NewLine}");
Console.WriteLine($"To Start the Game write --start in the command line{Environment.NewLine}");
string userInput = Console.ReadLine();
Game game = new Game();
game.UserInput = userInput;
}
}
public class Game
{
public event EventHandler<UserInputEvent> UserWritedInput;
private string _userInput;
public string UserInput
{
get { return _userInput; }
set
{
_userInput = value;
OnUserWritedInput();
}
}
public void OnUserWritedInput()
{
UserWritedInput?.Invoke(this, new UserInputEvent(_userInput));
}
}
public class UserInputEvent : EventArgs
{
private string _userInput;
public UserInputEvent(string userInput)
{
this._userInput = userInput;
if (_userInput.Equals("--start"))
{
Console.WriteLine("game started!");
}
}
}
您还没有订阅该活动:
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Welcome to the BlackJack Console Game!{Environment.NewLine}" );
Console.WriteLine($"You get 500 Credits. The minimal Bet is 10 and the maximal 50!{Environment.NewLine}");
Console.WriteLine($"You can check your Creditcount with --credits{Environment.NewLine}");
Console.WriteLine($"To Start the Game write --start in the command line{Environment.NewLine}");
string userInput = Console.ReadLine();
Game game = new Game();
game.UserWritedInput += OnUserWritedInput;
game.UserInput = userInput;
}
private void OnUserWritedInput(object sender, UserInputEvent args)
{
if (args.UserInput.Equals("--start"))
{
Console.WriteLine("game started!");
}
}
}
public class UserInputEvent : EventArgs
{
public string UserInput {get;}
public UserInputEvent(string userInput)
{
UserInput = userInput;
}
}
我目前正在尝试通过编写自己的自定义事件来理解 C# 中的事件。我的目标是在用户向控制台输入内容后触发一个事件。如果字符串等于“--start”,应该会发生一些事情。我目前没有在我的自定义事件的构造函数中到达我的断点。我希望你能帮助我。
这是我的代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Welcome to the BlackJack Console Game!{Environment.NewLine}" );
Console.WriteLine($"You get 500 Credits. The minimal Bet is 10 and the maximal 50!{Environment.NewLine}");
Console.WriteLine($"You can check your Creditcount with --credits{Environment.NewLine}");
Console.WriteLine($"To Start the Game write --start in the command line{Environment.NewLine}");
string userInput = Console.ReadLine();
Game game = new Game();
game.UserInput = userInput;
}
}
public class Game
{
public event EventHandler<UserInputEvent> UserWritedInput;
private string _userInput;
public string UserInput
{
get { return _userInput; }
set
{
_userInput = value;
OnUserWritedInput();
}
}
public void OnUserWritedInput()
{
UserWritedInput?.Invoke(this, new UserInputEvent(_userInput));
}
}
public class UserInputEvent : EventArgs
{
private string _userInput;
public UserInputEvent(string userInput)
{
this._userInput = userInput;
if (_userInput.Equals("--start"))
{
Console.WriteLine("game started!");
}
}
}
您还没有订阅该活动:
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"Welcome to the BlackJack Console Game!{Environment.NewLine}" );
Console.WriteLine($"You get 500 Credits. The minimal Bet is 10 and the maximal 50!{Environment.NewLine}");
Console.WriteLine($"You can check your Creditcount with --credits{Environment.NewLine}");
Console.WriteLine($"To Start the Game write --start in the command line{Environment.NewLine}");
string userInput = Console.ReadLine();
Game game = new Game();
game.UserWritedInput += OnUserWritedInput;
game.UserInput = userInput;
}
private void OnUserWritedInput(object sender, UserInputEvent args)
{
if (args.UserInput.Equals("--start"))
{
Console.WriteLine("game started!");
}
}
}
public class UserInputEvent : EventArgs
{
public string UserInput {get;}
public UserInputEvent(string userInput)
{
UserInput = userInput;
}
}