如何在c#wpf中编写登录空洞的代码? [英] how to code log in voids in c# wpf?

查看:77
本文介绍了如何在c#wpf中编写登录空洞的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在纯c中的表现#

here is how i do it in pure c#

private void button6_Click(object sender, EventArgs e)
       {
           saeid.Server = "nimbuzz.com";
           saeid.ConnectServer = "o.nimbuzz.com";
           saeid.Open(textBox5.Text, textBox2.Text, textBox3.Text, 50);
           saeid.OnAuthError += new XmppElementHandler(failed);
           saeid.OnLogin += new ObjectHandler(connected);
           saeid.OnClose += new ObjectHandler(dc);
           saeid.OnMessage += new MessageHandler(OnMessage);
       }

       private void failed(object sender, Element e)
       {
           if (base.InvokeRequired)
           {
               base.BeginInvoke(new XmppElementHandler(failed), new object[] { sender, e });
           }
           else
           {
               MessageBox.Show("PassWord Or User Name Eroor", "alone_051");
           }
       }
       private void connected(object sender)
       {
           if (base.InvokeRequired)
           {
               base.BeginInvoke(new ObjectHandler(connected), new object[] { sender });
           }
           else
           {
               textBox2.BackColor = Color.Green;
               textBox5.BackColor = Color.Green;
               textBox3.BackColor = Color.Green;
           }
       }
       private void dc(object sender)
       {
           if (base.InvokeRequired)
           {
               base.BeginInvoke(new ObjectHandler(dc), new object[] { sender });
           }
           else
           {
               textBox2.BackColor = Color.Yellow;
               textBox5.BackColor = Color.Yellow;
               textBox3.BackColor = Color.Yellow;
           }
       }
       private void OnMessage(object sender, agsXMPP.protocol.client.Message msg)
       {
           if (base.InvokeRequired)
           {
               base.BeginInvoke(new MessageHandler(OnMessage), new object[] { sender, msg });
           }
           else
           {
               if (msg.Type == MessageType.groupchat)
               {
                   richTextBox1.SelectionStart = richTextBox1.Text.Length;
                   richTextBox1.SelectionColor = Color.Black;
                   richTextBox1.SelectedText = msg.From.Resource + " : ";
                   richTextBox1.SelectionColor = Color.Black;
                   richTextBox1.SelectedText = msg.Body + '\n';
                   richTextBox1.SelectionStart = richTextBox1.Text.Length;
               }





现在当我尝试使用wpf进行相同的登录时,有很多错误。

调用未找到定义。如何解决这个问题?

不能造成空洞。奇怪的类错误

但是相同的代码可以正常使用c#

可以编辑上面提到的代码以使其在wpf应用程序中工作吗?



now when i try to make the same log in using wpf there are many errors.
invoke defination not found. how to fix that?
can not create voids. weird class error
but the same code works just fine with c#
can any one edit the above mentioned code to make it work in a wpf application?

推荐答案

你没有显示什么是 base ,什么是这个。最有可能的是,您的班级是 Window 。为什么你会期望它有调用 BeginInvoke (我怀疑是因为你在<$ c $中使用了类似的方法c> System.Windows.Forms ...)?



这不是它在WPF中的工作原理,你应该使用 System.Windows.Threading.Dispatcher

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher%28v=vs.110%29.aspx [ ^ ]。



如果您的类来自 Window ,那么您应该看到它是一个 System.Windows.Threading.DispatcherObject ,它的属性为 Dispatcher

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherobject.dispatcher%28v=vs.110%29.aspx [ ^ ]。



另外,你没有 InvokeRequired 。即使使用Forms,您通常也不需要它。以下是它的工作原理:当你启动一些WPF UI时,调用线程成为一个UI线程。这就是我所说的。您添加到此应用程序的所有UI元素都与此线程关联,并且只能从此线程控制。如果调用线程是某个其他线程(它甚至可能是另一个UI线程,因为在非常变态的设置中,您可以有另一个执行 Application 的实例,或者UI如果该UI元素将抛出跨线程异常,则尝试调用任何方法或属性。在所有这些情况下,始终需要调用。即使您在实际不需要调用时在UI线程中使用调用 BeginInvoke ,您仍然可以执行此操作,没有什么可怕的事情发生,除了一些较小的性能成本。



在一些非常罕见的情况下你需要像 InvokeRequired 这样的东西,当你需要编写一些有时从UI线程调用的方法时,有时候从其他一些线程调用。可能需要多久一次?这不是一个不完美的代码设计的标志吗?如果您确实需要实现此类行为,本文将介绍如何执行此操作:

http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired [ ^ ]。



我会推荐你:你应该真正理解你写的每一行。您不应该尝试通过踢它来修复电视机,直到它开始显示图片。



-SA
You are not showing what is base and what is this. Most likely, your class is Window. Why would you expect that it has Invoke or BeginInvoke (I suspect just because you used similar methods in System.Windows.Forms…)?

This is not how it works in WPF, where you should use System.Windows.Threading.Dispatcher:
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher%28v=vs.110%29.aspx[^].

If your class is derived from Window, you should see that it is a System.Windows.Threading.DispatcherObject, which has the property Dispatcher:
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherobject.dispatcher%28v=vs.110%29.aspx[^].

Also, you did not have InvokeRequired. Even with Forms, you usually don't need it. Here is how it works: when you start some WPF UI, the calling thread becomes a UI thread. This is just how I call it. All UI elements you add to this Application becomes associated with this thread and can be controlled only from this thread. If the calling thread is some other thread (it could be even another UI thread, because, in a very perverted settings, you can have another instance of executing Application, or UI of some other kind), attempt to call any methods or properties if that UI element will throw the cross-thread exception. In all such cases, invoke is always required. Even if you use Invoke or BeginInvoke in the UI thread when invocation is not really needed, you still can do it, nothing terrible will happen, except some minor performance cost.

You need something like InvokeRequired in some really rare cases, when you need to write some method which is sometimes called from UI thread, and sometimes from some other threads. How often it might be needed? Wouldn't that be a sign of less then perfect code design? If you really need to implement such behavior, this article explains how you can do it:
http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired[^].

And I would recommend you: you should really understand each line you are writing. You should not try to repair a TV set by kicking it until it starts showing the picture.

—SA


你好!感谢提供的链接和解释的内容。

但是你能为wpf改写代码吗?

没有冒犯。这只是我学习的方式。如果我看到一些行动/构建。我不仅可以更快地理解它,而且还可以快速重建。
Hi there! thanks for the links provided and things explained.
But can you just rewrite that code for wpf?
No offense. It is just the way i learn. If i see something in action/constructed. I can not only understand that quicker but also rebuild that in a flash.


我只是一个初学者,我有兴趣使用c#和wpf开发聊天客户端和机器人。我非常感谢,除了重新编写课程代码之外,是否有人可以为我想要学习的东西推荐一本书。
i am just a beginner yet and i am interested in developing chat clients and robots using c# with wpf. I'd greatly appreciate if anyone could suggest a book exactly for what i am trying to learn, apart from re-writing the code ofcourse.


这篇关于如何在c#wpf中编写登录空洞的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆