TriggeredConnectionStateChangedEventArgs Clase
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Contiene propiedades que el evento TriggeredConnectionStateChanged pasa a una aplicación.
public ref class TriggeredConnectionStateChangedEventArgs sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class TriggeredConnectionStateChangedEventArgs final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class TriggeredConnectionStateChangedEventArgs
Public NotInheritable Class TriggeredConnectionStateChangedEventArgs
- Herencia
- Atributos
Requisitos de Windows
Familia de dispositivos |
Windows 10 (se introdujo en la versión 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
|
API contract |
Windows.Foundation.UniversalApiContract (se introdujo en la versión v1.0)
|
Características de aplicaciones |
ID_CAP_PROXIMITY [Windows Phone]
proximity
ID_CAP_PROXIMITY [Windows Phone]
|
Ejemplos
// Click event for "Start" button.
private void StartFindingPeersButton_Click(object sender, RoutedEventArgs e)
{
if ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) ==
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered)
{
Windows.Networking.Proximity.PeerFinder.Start();
Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged +=
TriggeredConnectionStateChanged;
WriteMessageText("Waiting for peers. Enter proximity to connect. " +
"Call PeerFinder.start() on the peer to send and receive messages.\n");
}
else {
WriteMessageText("Peer discovery using tapping is not supported.\n");
}
}
private void TriggeredConnectionStateChanged(object sender,
Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs e)
{
if (e.State == Windows.Networking.Proximity.TriggeredConnectState.Completed)
{
WriteMessageText("Connected. You may now send a message.\n");
SendMessage(e.Socket);
}
}
Windows.Networking.Sockets.StreamSocket proximitySocket;
Windows.Storage.Streams.DataWriter dataWriter;
// Reference socket streams for writing and reading messages.
private void SendMessage(Windows.Networking.Sockets.StreamSocket socket) {
// Get the network socket from the proximity connection.
proximitySocket = socket;
// Create DataWriter for writing messages to peers.
dataWriter = new Windows.Storage.Streams.DataWriter(proximitySocket.OutputStream);
// Listen for messages from peers.
Windows.Storage.Streams.DataReader dataReader =
new Windows.Storage.Streams.DataReader(proximitySocket.InputStream);
StartReader(proximitySocket, dataReader);
}
// Send a message to the socket.
private async void SendMessageText() {
string msg = SendMessageTextBlock.Text;
if (msg.Length > 0) {
var msgLength = dataWriter.MeasureString(msg);
dataWriter.WriteInt32(msg.Length);
dataWriter.WriteString(msg);
try
{
await dataWriter.StoreAsync();
WriteMessageText("Message sent: " + msg + "\n");
}
catch (Exception e)
{
WriteMessageText("Send error: " + e.Message + "\n");
}
}
}
// Read out and print the message received from the socket.
private async void StartReader(
Windows.Networking.Sockets.StreamSocket socket,
Windows.Storage.Streams.DataReader reader)
{
uint initialLength = 4;
try
{
await reader.LoadAsync(initialLength);
uint msgLength = (uint)reader.ReadInt32();
try
{
await reader.LoadAsync(msgLength);
string message = reader.ReadString(msgLength);
WriteMessageText("Received message: " + message + "\n");
// After receiving a message, listen for the next message.
StartReader(socket, reader);
}
catch (Exception e)
{
WriteMessageText("Error: " + e.Message + "\n");
socket.Dispose();
}
}
catch (Exception e)
{
WriteMessageText("Error: " + e.Message + "\n");
socket.Dispose();
}
}
// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;
async private void WriteMessageText(string message, bool overwrite = false)
{
await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
if (overwrite)
MessageBlock.Text = message;
else
MessageBlock.Text += message;
});
}
' Click event for "Start" button.
Private Sub StartFindingPeersButton_Click(sender As Object, e As RoutedEventArgs)
If ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) =
Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) Then
Windows.Networking.Proximity.PeerFinder.Start()
AddHandler Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged,
AddressOf TriggeredConnectionStateChanged
WriteMessageText("Waiting for peers. Enter proximity to connect. " &
"Call PeerFinder.start() on the peer to send and receive messages." & vbTab)
Else
WriteMessageText("Peer discovery using tapping is not supported." & vbTab)
End If
End Sub
Private Sub TriggeredConnectionStateChanged(sender As Object,
e As Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs)
If e.State = Windows.Networking.Proximity.TriggeredConnectState.Completed Then
WriteMessageText("Connected. You may now send a message." & vbTab)
SendMessage(e.Socket)
End If
End Sub
Private proximitySocket As Windows.Networking.Sockets.StreamSocket
Private dataWriter As Windows.Storage.Streams.DataWriter
' Reference socket streams for writing and reading messages.
Private Sub SendMessage(socket As Windows.Networking.Sockets.StreamSocket)
' Get the network socket from the proximity connection.
proximitySocket = socket
' Create DataWriter for writing messages to peers.
dataWriter = New Windows.Storage.Streams.DataWriter(proximitySocket.OutputStream)
' Listen for messages from peers.
Dim dataReader = New Windows.Storage.Streams.DataReader(proximitySocket.InputStream)
StartReader(proximitySocket, dataReader)
End Sub
' Send a message to the socket.
Private Async Sub SendMessageText()
Dim msg = SendMessageTextBlock.Text
If msg.Length > 0 Then
Dim msgLength = dataWriter.MeasureString(msg)
dataWriter.WriteInt32(msg.Length)
dataWriter.WriteString(msg)
Try
Await dataWriter.StoreAsync()
WriteMessageText("Message sent: " & msg & vbTab)
Catch e As Exception
WriteMessageText("Send error: " & e.Message & vbTab)
End Try
End If
End Sub
' Read out and print the message received from the socket.
Private Async Sub StartReader(
socket As Windows.Networking.Sockets.StreamSocket,
reader As Windows.Storage.Streams.DataReader)
Dim initialLength As UInteger = 4
Try
Await reader.LoadAsync(initialLength)
Dim msgLength = CType(reader.ReadInt32(), UInteger)
Try
Await reader.LoadAsync(msgLength)
Dim message = reader.ReadString(msgLength)
WriteMessageText("Received message: " & message & vbTab)
' After receiving a message, listen for the next message.
StartReader(socket, reader)
Catch e As Exception
WriteMessageText("Error: " & e.Message & vbTab)
socket.Dispose()
End Try
Catch e As Exception
WriteMessageText("Error: " & e.Message & vbTab)
socket.Dispose()
End Try
End Sub
' Write a message to MessageBlock on the UI thread.
Private Async Sub WriteMessageText(message As String, Optional overwrite As Boolean = False)
Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
Sub()
If overwrite Then
MessageBlock.Text = message
Else
MessageBlock.Text &= message
End If
End Sub)
End Sub
Propiedades
Id |
Obtiene el identificador único de la conexión en curso. |
Socket |
Obtiene la instancia de StreamSocket para la conexión. |
State |
Obtiene el estado de la conexión en curso. |