我正在转换其他人的代码。它是用C#Windows FORM编写的。我希望它在C#WPF中。
这是原始代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace FileReceiver
{
public partial class MainForm : Form
{
const int PORT = 1723;
public MainForm()
{
InitializeComponent();
}
private void resetControls()
{
progressBar1.Style = ProgressBarStyle.Marquee;
textBox1.Text = "Waiting for connection...";
}
protected override async void OnShown(EventArgs e)
{
// Listen
TcpListener listener = TcpListener.Create(PORT);
listener.Start();
textBox1.Text = "Waiting for connection...";
TcpClient client = await listener.AcceptTcpClientAsync();
NetworkStream ns = client.GetStream();
// Get file info
long fileLength;
string fileName;
{
byte[] fileNameBytes;
byte[] fileNameLengthBytes = new byte[4]; //int32
byte[] fileLengthBytes = new byte[8]; //int64
await ns.ReadAsync(fileLengthBytes, 0, 8); // int64
await ns.ReadAsync(fileNameLengthBytes, 0, 4); // int32
fileNameBytes = new byte[BitConverter.ToInt32(fileNameLengthBytes, 0)];
await ns.ReadAsync(fileNameBytes, 0, fileNameBytes.Length);
fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
fileName = ASCIIEncoding.ASCII.GetString(fileNameBytes);
}
// Get permission
if (MessageBox.Show(String.Format("Requesting permission to receive file:\r\n\r\n{0}\r\n{1} bytes long", fileName, fileLength), "", MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
// Set save location
SaveFileDialog sfd = new SaveFileDialog();
sfd.CreatePrompt = false;
sfd.OverwritePrompt = true;
sfd.FileName = fileName;
if (sfd.ShowDialog() != DialogResult.OK)
{
ns.WriteByte(0); // Permission denied
return;
}
ns.WriteByte(1); // Permission grantedd
FileStream fileStream = File.Open(sfd.FileName, FileMode.Create);
// Receive
textBox1.Text = "Receiving...";
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Value = 0;
int read;
int totalRead = 0;
byte[] buffer = new byte[32 * 1024]; // 32k chunks
while ((read = await ns.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, read);
totalRead += read;
progressBar1.Value = (int)((100d * totalRead) / fileLength);
}
fileStream.Dispose();
client.Close();
MessageBox.Show("File successfully received");
resetControls();
}
}
}
我已成功将此代码中的所有内容转换为WPF。 我在WPF中遇到的唯一问题是:
protected override async void OnShown(EventArgs e)
Error 8 'EmployeeLocatorv2.receiver.OnShown(System.EventArgs)': no suitable method found to override
答案 0 :(得分:1)
这意味着您的主窗体继承的类(可能是Window或UserControl)没有可以覆盖的名为“OnShown”的虚拟方法。
根据Chango V.,Window.ContentRendered
是您正在寻找的事件。
答案 1 :(得分:0)
您可以使用Window.ContentRendered事件来了解何时呈现WPF表单。
您可以在XAML中声明事件:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered_1">
以及相应的代码:
private void Window_ContentRendered_1(object sender, EventArgs e)
{
// Place your code here.
}
答案 2 :(得分:0)
在WPF中,ContentRendered
有类似的行为。
bool _hasShown;
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
if (!_hasShown)
{
_hasShown = true;
// void OnShown() code here!
}
}