'method'没有重载匹配委托'RoutedEventHandler'

时间:2017-03-07 19:21:02

标签: c# .net wpf

我已经尝试过寻找与此类似的问题以获得答案,但它们似乎略有不同,介意是'RoutedEventHandler',而其他问题只是'eventhandler'并且似乎与我的问题不相符。

为了帮助您理解,我制作了一个基本的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>

1 个答案:

答案 0 :(得分:1)

您的send_button_Click方法必须与RoutedEventHandler具有相同的签名。但是你的有一个额外的StreamWriter参数必须删除。