我想像这样使用 PhotoChooserTask 返回一张照片:
private void getimage_Click(object sender, EventArgs e)
{
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
try
{
photoChooserTask.Show();
}
catch (System.InvalidOperationException ex)
{
MessageBox.Show("An error occurred.");
}
}
void photoChooserTask_Completed(object sender, PhotoResult ee)
{
if (ee.TaskResult == TaskResult.OK)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(ee.ChosenPhoto);
if (ee.TaskResult == TaskResult.OK && ee.Error == null)
{
WriteableBitmap wb = new WriteableBitmap(bmp);
notes.Add(new chatinfo() { sendimage = bmp });
noteListBox.ItemsSource = null;
noteListBox.ItemsSource = notes;
}
}
}
但每次程序到达这里:“bmp.SetSource(ee.ChosenPhoto);” 将调用 SocketException。
private void OnRecieveFrom()
{
var receiveArgs = new SocketAsyncEventArgs();
receiveArgs.RemoteEndPoint = this.IPEndPoint;
receiveArgs.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
var strBdr = new StringBuilder();
receiveArgs.Completed += (__, result) =>
{
string message = CreateMessage(result);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
this.RaiseReceived(message);
});
socket.ReceiveFromAsync(receiveArgs);
};
socket.ReceiveFromAsync(receiveArgs);
}
SocketException 由“socket.ReceiveFromAsync(receiveArgs);”调用 我只是想从手机中获取照片,没有发送或接收操作。我不知道为什么调用接收函数。
应用跳转到相册时会不会丢失socket通信(socket的“RemoteEndPoint”值变为null)?ps “socket”是“Socket”类的一个对象。如果是这样,我应该在每次应用程序跳出时重新创建“套接字”吗?
谢谢!