2014-03-14 132 views
0

我目前正在轉換別人的代碼。它是用C#Windows FORM編寫的。我希望它在C#WPF中。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 

回答

1

這意味着你的主窗體繼承(可能是窗口或用戶控件)的類沒有名爲「OnShown」虛方法,你可以重寫。

根據Chango V.Window.ContentRendered是你正在尋找的事件。

+0

爲了將來的參考,請嘗試在您的文章中包含鏈接的相關部分。這樣,即使鏈接被破壞,內容也會被保留。 – dcastro

+0

感謝您的建議,剛開始:) –

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. 
} 
0

在WPF,ContentRendered也有類似的行爲。

bool _hasShown; 

    protected override void OnContentRendered(EventArgs e) 
    { 
     base.OnContentRendered(e); 

     if (!_hasShown) 
     { 
      _hasShown = true; 
      // void OnShown() code here! 
     } 
    }