有什么方法可以在没有点击的情况下调用 img_Tapped 方法(不是当我点击 UI 元素(按钮)时),而是从构造函数中调用它?我有 MessageReceived 的处理程序。如果没有人可以建议我如何为同一功能重新制作这两种方法 - 我不需要点击元素,这些方法应该在应用程序启动或页面加载时执行......
方法一:
private async void img_Tapped(object sender, TappedRoutedEventArgs e)
{
...
mws = new MessageWebSocket();
mws.Control.MessageType = SocketMessageType.Binary;
mws.MessageReceived += MessageReceived;
await mws.ConnectAsync(server);
messageWriter = new DataWriter(mws.OutputStream);
messageWriter.WriteBytes(buff);
await messageWriter.StoreAsync();
}
方法二:
private void MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
try
{
using (DataReader reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
byte[] read = reader.ReadBuffer(reader.UnconsumedBufferLength).ToArray();
}
}
catch (Exception ex)
{
}
}
. . .
我发现这个但它不好,因为仍然需要单击/点击按钮,我不想要它。
img.Tapped += new TappedEventHandler(my method with button parameters);