2017-03-07 90 views
-1

我試着尋找類似於這個答案的問題,但他們似乎有點不同,頭腦是'RoutedEventHandler',而其他問題只是'eventhandler',而不是'這似乎與我的問題相符。沒有重載'方法'匹配代理'RoutedEventHandler'

爲了幫助你理解,我做了一個基本的wpf c#TCP客戶端,它在讀回消息流的同時發送一條消息(文本框中的內容),然後將它放入一個文本文件中。

嘗試編譯時發生此錯誤。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Net; 
using System.Net.Sockets; 
using System.IO; 

namespace client 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     // Ip and port variables used for connect and gui display 
     string ipaddress = "127.0.0.1"; 
     int port = 8888; 

     /// <summary> 
     /// Set new TCP client, stream writer and reader, connect and gui show ip and port 
     /// </summary> 
     public void MainWindow_Connect(object sender, EventArgs e) 
     { 
      TcpClient client = new TcpClient(); // New TcpClient 
      client.Connect(ipaddress, port); // IP, Port to connect   
      StreamWriter sw = new StreamWriter(client.GetStream()); // New StreamWriter instance 
      StreamReader sr = new StreamReader(client.GetStream()); // New StreamReader instance 
      // Interface label show ip and port 
      serverip_lbl.Content = ipaddress; 
      portno_lbl.Content = port; 
     } 

     /// <summary> 
     /// Send message inside message_txt textbox, write to network stream and send 
     /// </summary> 
     private void send_button_Click(object sender, RoutedEventArgs e, StreamWriter sw) 
     { 
      sw.WriteLine(message_txt.Text); 
      sw.Flush(); 
     } 

     /// <summary> 
     /// Read from message from the server, write to textfile 
     /// </summary> 
     public void serverStream(StreamReader sr) 
     { 
      // Create a string array 
      string[] message = { "\n", sr.ReadToEnd() }; 
      // WriteAllLines creates a file, writes a collection of strings to the file and then closes the file 
      File.WriteAllLines(@"C:\Users\Public\Documents\Messages.txt", message); 
     } 
    } 
} 

XAML:

<Window x:Class="client.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:client" 
    Loaded = "MainWindow_Connect" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="500" Width="400"> 
<Grid> 
    <Label x:Name="titleclient_txt" Content="Client" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18.667" HorizontalAlignment="Left" FontStyle="Italic" d:IsLocked="True"/> 
    <Label x:Name="demotitle_txt" Content="Sending a message" HorizontalAlignment="Right" Margin="0,50,87,0" VerticalAlignment="Top" FontSize="24" FontWeight="Bold" d:IsLocked="True"/> 
    <Label x:Name="server_lbl" Content="Server IP:" HorizontalAlignment="Left" Margin="74,106,0,0" VerticalAlignment="Top" d:IsLocked="True"/> 
    <Label x:Name="serverip_lbl" Content="" HorizontalAlignment="Left" Margin="155,106,0,0" VerticalAlignment="Top" Width="150" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/> 
    <Label x:Name="port_lbl" Content="Port:" HorizontalAlignment="Left" Margin="74,146,0,0" VerticalAlignment="Top" d:IsLocked="True"/> 
    <Label x:Name="portno_lbl" Content="" HorizontalAlignment="Left" Margin="155,146,0,0" VerticalAlignment="Top" Width="150" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/> 
    <Label x:Name="message_lbl" Content="Message:" HorizontalAlignment="Left" Margin="74,199,0,0" VerticalAlignment="Top" d:IsLocked="True"/> 
    <TextBox x:Name="message_txt" HorizontalAlignment="Left" Height="70" Margin="74,230,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="250"/> 
    <Button x:Name="send_button" Content="Send" HorizontalAlignment="Left" Margin="155,344,0,0" VerticalAlignment="Top" Width="75" Click="send_button_Click"/> 

</Grid> 

+0

這段代碼應該做什麼?你的Connect()方法實際上並沒有做任何事情,因爲在完成方法之後,兩個'StreamWriter'和TcpClient都將被垃圾收集,因爲你沒有保留對它們的引用。 –

+0

建立到服務器的連接,並使gui標籤顯示正在使用的ip和端口。 – HJagger95

+0

也不需要發佈mainwindow.g.i.cs代碼,因爲這是設計器生成的代碼,不應該被修改。 –

回答

1

你send_button_Click方法必須具有相同的簽名RoutedEventHandler。但是你有一個額外的StreamWriter參數必須刪除。

+0

它也可以讓streamwriter的參數使它們相同嗎?如果沒有streamwriter參數,sw變量會給出錯誤信息,說明'sw在當前上下文中不存在'? – HJagger95

+0

只需在您的主窗口類中定義StreamWriter sw,並在其中擁有您的ip和端口變量。當然,初始化應該保留在Connect方法中,但只是變成sw = new StreamWriter(client.GetStream()); – ckuri