C#WPF获取错误 –System.InvalidOperationException:

C#WPF获取错误 –System.InvalidOperationException:,第1张

概述C#WPF获取错误 – System.InvalidOperationException:

我刚刚开始学习使用C#WPF开发windows应用程序,并且我们已经给自学项目开发​​了一个windows应用程序。 我试图创build应用程序聊天客户端到客户端。 我创build了一个类MainWindow.xaml和MainWindow.xaml.cs来处理。 当我从主窗体调用该类时,我收到以下错误

这是两个阶级:

<Window x:Class="Chat_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:Chat_ClIEnt" mc:Ignorable="d" title="Chat: ClIEnt-to-ClIEnt" Height="450" WIDth="625"> <GrID> <Label x:name="label" Content="IP" HorizontalAlignment="left" margin="39,52,0" VerticalAlignment="top"/> <Label x:name="label1" Content="Port" HorizontalAlignment="left" margin="39,100,0" VerticalAlignment="top"/> <Label x:name="label2" Content="IP" HorizontalAlignment="left" margin="287,59,0" VerticalAlignment="top"/> <Label x:name="label3" Content="Port" HorizontalAlignment="left" margin="287,0" VerticalAlignment="top" RendertransformOrigin="-0.037,-0.343"/> <TextBox x:name="textLocalip" HorizontalAlignment="left" Height="23" margin="98,0" textwrapPing="Wrap" VerticalAlignment="top" WIDth="120"/> <TextBox x:name="textLocalPort" HorizontalAlignment="left" Height="23" margin="98,104,0" textwrapPing="Wrap" VerticalAlignment="top" WIDth="120"/> <TextBox x:name="textFrIEndsIp" HorizontalAlignment="left" Height="23" margin="342,0" textwrapPing="Wrap" VerticalAlignment="top" WIDth="120" TextChanged="textBox2_TextChanged"/> <TextBox x:name="textFrIEndsPort" HorizontalAlignment="left" Height="23" margin="342,0" textwrapPing="Wrap" VerticalAlignment="top" WIDth="120"/> <button x:name="button" Content="Start" HorizontalAlignment="left" margin="488,58,0" VerticalAlignment="top" WIDth="96" Click="button_Click"/> <ListBox x:name="ListMessage" HorizontalAlignment="left" Height="156" margin="39,151,0" VerticalAlignment="top" WIDth="423"/> <TextBox x:name="textMessage" HorizontalAlignment="left" Height="39" margin="39,343,0" textwrapPing="Wrap" VerticalAlignment="top" WIDth="423"/> <button x:name="button1" Content="Send" HorizontalAlignment="left" margin="488,362,0" VerticalAlignment="top" WIDth="96" Click="button1_Click"/> <Label x:name="label4" Content="C1" HorizontalAlignment="left" margin="98,28,0" VerticalAlignment="top" RendertransformOrigin="-0.585,0.964"/> <Label x:name="label5" Content="C2" HorizontalAlignment="left" margin="342,0" VerticalAlignment="top" RendertransformOrigin="0.105,1.107"/> </GrID>

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; namespace Chat_ClIEnt { public partial class MainWindow : Window { Socket sck; EndPoint epLocal,epRemote; public MainWindow() { InitializeComponent(); sck = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp); sck.SetSocketoption(SocketoptionLevel.socket,Socketoptionname.ReuseAddress,true); textLocalip.Text = GetLocaliP(); textFrIEndsIp.Text = GetLocaliP(); } private string GetLocaliP() { IPHostEntry host; host = Dns.GetHostEntry(Dns.GetHostname()); foreach(IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip.ToString(); } } return "127.0.0.1"; } private voID MessageCallBack(IAsyncResult aResult) { try { int size = sck.EndReceiveFrom(aResult,ref epRemote); if (size > 0) { byte[] recIEvedData = new byte[1464]; recIEvedData = (byte[])aResult.AsyncState; ASCIIEnCoding eEnCoding = new ASCIIEnCoding(); string recIEvedMessage = eEnCoding.GetString(recIEvedData); ListMessage.Items.Add("MerID: " + recIEvedMessage); } byte[] buffer = new byte[1500]; sck.BeginReceiveFrom(buffer,buffer.Length,SocketFlags.None,ref epRemote,new AsyncCallback(MessageCallBack),buffer); } catch(Exception exp) { MessageBox.Show(exp.ToString()); } } private voID textBox2_TextChanged(object sender,TextChangedEventArgs e) { } private voID button_Click(object sender,RoutedEventArgs e) { try { epLocal = new IPEndPoint(IPAddress.Parse(textLocalip.Text),Convert.ToInt32(textLocalPort.Text)); sck.Bind(epLocal); epRemote = new IPEndPoint(IPAddress.Parse(textFrIEndsIp.Text),Convert.ToInt32(textFrIEndsPort.Text)); sck.Connect(epRemote); byte[] buffer = new byte[1500]; sck.BeginReceiveFrom(buffer,buffer); button.Content = "Connected"; button.IsEnabled = false; button1.IsEnabled = true; textMessage.Focus(); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } private voID button1_Click(object sender,RoutedEventArgs e) { try { System.Text.ASCIIEnCoding enc = new System.Text.ASCIIEnCoding(); byte[] msg = new byte[1500]; msg = enc.GetBytes(textMessage.Text); sck.Send(msg); ListMessage.Items.Add("Bod: "+textMessage); textMessage.Clear(); } catch(Exception excp) { MessageBox.Show(excp.ToString()); } } }

}

是否可以使用Visual Studio为.Net程序集设置Originalfilename属性?

创build一个windows钩子来检测菜单点击

有没有办法检索设备的DPI而不使用GDI + / WinForms“graphics”类?

windows播放audio有多准确(按时间)?

如何将windowsauthentication信息存储在数据库中

如何开发windows应用程序

“添加成员”到一个types,而不是在PowerShell中只有一个types的对象

问题与WPF C#应用程序产生两个主要的windows

使用C#如何检测是否安装了windows Installer 4.5

在辅助显示器上全屏显示

您正尝试从background thread访问GUI item 。 您可以使用dispatcher在main thread上执行此 *** 作:

dispatcher.Invoke(new Action(() => ListMessage.Items.Add("MerID: " + recIEvedMessage)));

无论如何,WPF中推荐的与GUI交互的方式是DataBinding并将关注点与MVVM模式分离。

总结

以上是内存溢出为你收集整理的C#WPF获取错误 – System.InvalidOperationException:全部内容,希望文章能够帮你解决C#WPF获取错误 – System.InvalidOperationException:所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1292828.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存