0
我創建了這個對象,以便將使用TcpClient的API從.net移植到silverlight。它是使用流中的套接字和讀寫器寫入的。正如它發生的API使用二進制讀/寫器,所以一切工作正常。在測試時,看起來StreamWriter在編寫時並不會調用OnWrite委託。我很好奇,如果任何人都可以闡明這一點。c#繼承自Stream,Binary Writer Works,Stream Writer沒有
真的,這個類的唯一的事情是OnRead & OnWrite當讀或寫流時調用。就我而言,他們將工作交給了一個Socket。
public class NotifyStream:Stream
{
public delegate int ReadDelegate (byte[] buffer, int offset, int count);
public delegate void WriteDelegate(byte[] buffer, int offset, int count);
public ReadDelegate OnRead;
public WriteDelegate OnWrite;
public override void Flush()
{
// TODO: Implement this method
//throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
// TODO: Implement this method
return 0;
}
public override void SetLength(long value)
{
// TODO: Implement this method
// throw new NotImplementedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
// TODO: Implement this method
if (OnRead != null)
{
return OnRead(buffer, offset, count);
}
else
{
return 0;
}
}
public override void Write(byte[] buffer, int offset, int count)
{
// TODO: Implement this method
if (OnWrite != null)
{
OnWrite(buffer, offset, count);
}
}
public override bool CanRead
{
get
{
// TODO: Implement this property getter
return true;
}
}
public override bool CanSeek
{
get
{
// TODO: Implement this property getter
return false;
}
}
public override bool CanWrite
{
get
{
// TODO: Implement this property getter
return true;
}
}
public override long Length
{
get
{
// TODO: Implement this property getter
return 1024;
}
}
public override long Position
{
get
{
// TODO: Implement this property getter
return 0;
}
set
{
// TODO: Implement this property setter
//throw new NotImplementedException();
}
}
}
請顯示一個簡短但完整的程序來演示問題。另外*強烈*考慮使用事件而不是公共委託字段。 –
StreamWriter具有AutoFlush屬性。它默認爲* false *。這意味着你必須自己清除它,而不是關閉或處理它。將其設置爲* true *並重試。 –