gpt4 book ai didi

c# - 'method' 匹配委托(delegate) 'RoutedEventHandler' 没有重载

转载 作者:行者123 更新时间:2023-12-03 09:10:51 26 4
gpt4 key购买 nike

我尝试过查看与此类似的问题来寻找答案,但它们似乎略有不同,头脑是“RoatedEventHandler”,而其他问题只是关于“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>

最佳答案

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

关于c# - 'method' 匹配委托(delegate) 'RoutedEventHandler' 没有重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42656571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com