Hi,
I'm trying to pass the text from a entry box, but it doesn't seem to be passing it. I got it working for a search bar using the same concept.
xaml:
<Entry x:Name="entryBox" Placeholder="Enter chat text here:" ReturnType="Send" Keyboard="Plain" BindingContext="{Binding Source={viewModels:ChatroomViewModel}}" ReturnCommand="{Binding SendCommand}" ReturnCommandParameter="{Binding Source={x:Reference entryBox}, Path=Text}" Completed="EntryTextSend_Completed" />
ViewModel:
public class ChatroomViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone = new ManualResetEvent(false);
private static ManualResetEvent sendDone = new ManualResetEvent(false);
private static ManualResetEvent receiveDone = new ManualResetEvent(false);
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ICommand SendCommand => new Command<string>((string query) =>
{
Console.WriteLine("query: " + query);
Send(query);
});
private void Send(String data)
{
try
{
Socket client = LoginPageViewModel.clientSocket;
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
}
catch (SocketException ex)
{
/*connected = false;
chatRoomPage.UpdateChatWindow("<Server> - You are disconnected from the server!");
chatRoomPage.setStatusLabel("Disconnected.");*/
Console.WriteLine("SocketException: " + ex.Message);
}
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}