I have a chat program, consisting of an input textbox and a send button, set up like this:
<input @bind="messageInput" size="50" @onkeydown="submitMessageInput" />
<button @onclick="Send">Send</button>
The Send method does this:
async Task Send()
{
if (messageInput.StartsWith("/login") .....
The submitMessageInput method looks like this in order to handle pressing enter rather than clicking the send button manually:
public async Task submitMessageInput(KeyboardEventArgs e)
{
if (e.Code == "Enter" || e.Code == "NumpadEnter")
{
await Send();
}
}
If the button is clicked, it works fine. However if enter is pressed, the first line of the Send() method throws an object not set to instance of an object error.
I wonder if it's some kind of context issue that's making messageInput null? What's the correct way to handle this situation?