本文整理汇总了C#中Windows.Storage.Streams.DataWriter.DetachStream方法的典型用法代码示例。如果您正苦于以下问题:C# DataWriter.DetachStream方法的具体用法?C# DataWriter.DetachStream怎么用?C# DataWriter.DetachStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.Storage.Streams.DataWriter
的用法示例。
在下文中一共展示了DataWriter.DetachStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConnectAsync
public static async Task<bool> ConnectAsync()
{
if (imageComparisonServer != null)
return true;
try
{
imageComparisonServer = new StreamSocket();
await imageComparisonServer.ConnectAsync(new HostName(ParadoxImageServerHost), ParadoxImageServerPort.ToString());
// Send initial parameters
using (var memoryStream = new MemoryStream())
{
var binaryWriter = new BinaryWriter(memoryStream);
ImageTestResultConnection.Write(binaryWriter);
var dataWriter = new DataWriter(imageComparisonServer.OutputStream);
dataWriter.WriteBytes(memoryStream.ToArray());
await dataWriter.StoreAsync();
await dataWriter.FlushAsync();
dataWriter.DetachStream();
}
return true;
}
catch (Exception)
{
imageComparisonServer = null;
return false;
}
}
示例2: SaveAsync
/// <summary>
/// Writes a string to a text file.
/// </summary>
/// <param name="text">The text to write.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="folder">The folder.</param>
/// <param name="options">
/// The enum value that determines how responds if the fileName is the same
/// as the name of an existing file in the current folder. Defaults to ReplaceExisting.
/// </param>
/// <returns></returns>
public static async Task SaveAsync(
this string text,
string fileName,
StorageFolder folder = null,
CreationCollisionOption options = CreationCollisionOption.ReplaceExisting)
{
folder = folder ?? ApplicationData.Current.LocalFolder;
var file = await folder.CreateFileAsync(
fileName,
options);
using (var fs = await file.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outStream = fs.GetOutputStreamAt(0))
{
using (var dataWriter = new DataWriter(outStream))
{
if (text != null)
dataWriter.WriteString(text);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
await outStream.FlushAsync();
}
}
}
示例3: DoCommand
private async Task<string> DoCommand(string command)
{
StringBuilder strBuilder = new StringBuilder();
using (StreamSocket clientSocket = new StreamSocket())
{
await clientSocket.ConnectAsync(new HostName("192.168.9.108"), "9001");
using (DataWriter writer = new DataWriter(clientSocket.OutputStream))
{
writer.WriteString(command);
await writer.StoreAsync();
writer.DetachStream();
}
using (DataReader reader = new DataReader(clientSocket.InputStream))
{
reader.InputStreamOptions = InputStreamOptions.Partial;
await reader.LoadAsync(8192);
while (reader.UnconsumedBufferLength > 0)
{
strBuilder.Append(reader.ReadString(reader.UnconsumedBufferLength));
await reader.LoadAsync(8192);
}
reader.DetachStream();
}
}
return (strBuilder.ToString());
}
示例4: OnWrite
private async void OnWrite(Object sender, RoutedEventArgs e)
{
// 获取本地目录的引用
StorageFolder local = ApplicationData.Current.LocalFolder;
// 创建新文件
StorageFile newFIle = await local.CreateFileAsync("demo.dat", CreationCollisionOption.ReplaceExisting);
// 打开文件流
using(IRandomAccessStream stream = await newFIle.OpenAsync(FileAccessMode.ReadWrite))
{
// 实例化 DataWriter
DataWriter dw = new DataWriter(stream);
// 设置默认编码格式
dw.UnicodeEncoding = UnicodeEncoding.Utf8;
// 写入 bool 值
dw.WriteBoolean(true);
// 写入日期值
DateTime dt = new DateTime(2010, 8, 21);
dw.WriteDateTime(dt);
// 写入字符串
string str = "测试文本";
// 计算字符串长度
uint len = dw.MeasureString(str);
// 先写入字符串的长的
dw.WriteUInt32(len);
// 再写入字符串
dw.WriteString(str);
// 以下方法必须调用
await dw.StoreAsync();
// 解除 DataWriter 与流的关联
dw.DetachStream();
dw.Dispose();
}
MessageDialog msgDlg = new MessageDialog("保存成功。");
await msgDlg.ShowAsync();
}
示例5: WriteToSerialPort
public async void WriteToSerialPort(string message)
{
var writer = new DataWriter(_realDevice.OutputStream);
writer.WriteString(AddNewLine(message));
await writer.StoreAsync().AsTask();
writer.DetachStream();
}
示例6: init
public static async void init(TiSettings settings) {
if (!settings.ContainsKey("logToken") || settings["logToken"].Length == 0) {
return;
}
logToken = settings["logToken"];
multicastSocket = new DatagramSocket();
multicastSocket.MessageReceived += multicastSocket_MessageReceived;
HostName hostname = new HostName("239.6.6.6");
try {
await multicastSocket.BindServiceNameAsync("8666");
multicastSocket.JoinMulticastGroup(hostname);
IOutputStream stream = await multicastSocket.GetOutputStreamAsync(hostname, "8666");
DataWriter writer = new DataWriter(stream);
writer.WriteString("TI_WP8_LOGGER");
await writer.StoreAsync();
writer.DetachStream();
stream.Dispose();
} catch (Exception ex) {
if (SocketError.GetStatus(ex.HResult) == SocketErrorStatus.Unknown) {
throw;
}
Debug.WriteLine(ex.ToString());
}
}
示例7: WriteFileAsync
public static async Task WriteFileAsync(string fileName, byte[] content)
{
try
{
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outputStream = fileStream.GetOutputStreamAt(0))
{
using (var dataWriter = new DataWriter(outputStream))
{
dataWriter.WriteBytes(content);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
await outputStream.FlushAsync();
}
}
}
catch (Exception exception)
{
SnazzyDebug.WriteLine(exception);
}
}
示例8: WriteToOutputStreamAsync
private async Task WriteToOutputStreamAsync(byte[] bytes)
{
if (_socket == null) return;
_writer = new DataWriter(_socket.OutputStream);
_writer.WriteBytes(bytes);
var debugString = UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length);
try
{
await _writer.StoreAsync();
await _socket.OutputStream.FlushAsync();
_writer.DetachStream();
_writer.Dispose();
}
catch (Exception exception)
{
// If this is an unknown status it means that the error if fatal and retry will likely fail.
if (global::Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == global::Windows.Networking.Sockets.SocketErrorStatus.Unknown)
{
// TODO abort any retry attempts on Unity side
throw;
}
}
}
示例9: SendAsync
public async Task SendAsync(string text)
{
var dataWriteObject = new DataWriter(_serialPort.OutputStream);
dataWriteObject.WriteString(text);
var task = dataWriteObject.StoreAsync().AsTask(_cancellation.Token);
await task;
dataWriteObject.DetachStream();
}
示例10: WriteLine
public static async Task WriteLine(this IOutputStream outputStream, string line)
{
using (var dataWriter = new DataWriter(outputStream))
{
dataWriter.WriteString(line);
await dataWriter.StoreAsync();
dataWriter.DetachStream();
}
}
示例11: InitStreamInfo
async void InitStreamInfo()
{
_stream = new InMemoryRandomAccessStream();
DataWriter writer = new DataWriter(_stream);
writer.WriteString(_jsonValue);
var data = await writer.StoreAsync();
writer.DetachStream();
}
示例12: SendAsync
private async Task<bool> SendAsync(string data, string ok = "", string err = "")
{
if (serialPort == null)
{
if (!await Open())
return false;
}
try
{
using (var writer = new DataWriter(serialPort.OutputStream))
{
writer.WriteString(data);
await writer.StoreAsync();
writer.DetachStream();
}
}
catch (OperationCanceledException ex)
{
return false;
}
catch (Exception ex)
{
return false;
}
try
{
var buffer = string.Empty;
using (var reader = new DataReader(serialPort.InputStream))
{
reader.InputStreamOptions = InputStreamOptions.ReadAhead;
if (await reader.LoadAsync(1024) > 0)
buffer = reader.ReadString(reader.UnconsumedBufferLength);
reader.DetachStream();
}
if (!string.IsNullOrEmpty(ok) && buffer.Contains(ok))
{
return true;
}
else if (!string.IsNullOrEmpty(err) && buffer.Contains(err))
{
return false;
}
}
catch (OperationCanceledException ex)
{
return false;
}
catch (Exception ex)
{
return false;
}
return false;
}
示例13: Send
async Task Send(FayeObject o) {
if (o is FayeRequest)
(o as FayeRequest).ClientID = ClientID;
using (var writer = new DataWriter(_socket.OutputStream)) {
writer.UnicodeEncoding = UnicodeEncoding.Utf8;
var stringd = await Helpers.SerializeAsync(o);
writer.WriteString(stringd);
await writer.StoreAsync();
await writer.FlushAsync();
writer.DetachStream();
}
}
示例14: Run
public async void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
try
{
var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
var socketInformation = details.SocketInformation;
switch (details.Reason)
{
case SocketActivityTriggerReason.SocketActivity:
var socket = socketInformation.StreamSocket;
DataReader reader = new DataReader(socket.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
await reader.LoadAsync(250);
var dataString = reader.ReadString(reader.UnconsumedBufferLength);
ShowToast(dataString);
socket.TransferOwnership(socketInformation.Id);
break;
case SocketActivityTriggerReason.KeepAliveTimerExpired:
socket = socketInformation.StreamSocket;
DataWriter writer = new DataWriter(socket.OutputStream);
writer.WriteBytes(Encoding.UTF8.GetBytes("Keep alive"));
await writer.StoreAsync();
writer.DetachStream();
writer.Dispose();
socket.TransferOwnership(socketInformation.Id);
break;
case SocketActivityTriggerReason.SocketClosed:
socket = new StreamSocket();
socket.EnableTransferOwnership(taskInstance.Task.TaskId, SocketActivityConnectedStandbyAction.Wake);
if (ApplicationData.Current.LocalSettings.Values["hostname"] == null)
{
break;
}
var hostname = (String)ApplicationData.Current.LocalSettings.Values["hostname"];
var port = (String)ApplicationData.Current.LocalSettings.Values["port"];
await socket.ConnectAsync(new HostName(hostname), port);
socket.TransferOwnership(socketId);
break;
default:
break;
}
deferral.Complete();
}
catch (Exception exception)
{
ShowToast(exception.Message);
deferral.Complete();
}
}
示例15: SendButton_Click
private async void SendButton_Click(object sender, RoutedEventArgs e)
{
try
{
DataWriter writer = new DataWriter(connectedSocket.OutputStream);
writer.WriteBytes(Encoding.UTF8.GetBytes(SendMessageTextBox.Text));
await writer.StoreAsync();
writer.DetachStream();
writer.Dispose();
}
catch(Exception exception)
{
rootPage.NotifyUser(exception.Message, NotifyType.ErrorMessage);
}
}