为什么在Windows Phone 8.1 xmpp连接到openfire时开始调用显示错误 [英] why begin invoke showing error in windows phone 8.1 xmpp connection to openfire
问题描述
hai所有请帮助我尝试将我的应用程序与openfire服务器连接在这里。
[忽略我的按钮点击事件]
我遇到了麻烦:
1. Thread.Sleep(2000); //当前上下文中不存在名称线程
2.this.Dispatcher.BeginInvoke(()=> //错误!windows.ui.core .core调度程序
//不包含begin invoke的定义,也没有扩展名methode
//'begin invoke接受第一个参数类型windows.ui.core.coredispatcher'
{//可以找到(你错过了使用指令或汇编参考吗?)
& lt; pre& gt;我的代码:
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用Windows.Foundation;
使用Windows.Foundation。收藏;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls .Primitives;
使用Windows.UI.Xaml.Data;
使用Windows.UI.Xaml.Input;
使用Windows.UI。 Xaml.Media;
使用Windows.UI.Xaml.Navigation;
使用System.Net.XMPP;
使用System.Threading; < br $>
//空白页项目模板记录在http://go.microsoft.com/fwlink/?LinkId=391641 < br $>
命名空间chat_tral
{
///& amp; lt; summary& amp; gt;
///一个空白页面,可单独使用或导航到一个帧内。
///& amp; lt; / summary& amp; gt ;
公共密封分类主页:页面
{
///& amp; lt; summary& amp; gt;
/// Xmpp客户端
///& amp; LT; /总和mary& amp; gt;
public XMPPClient ObjXmppClient {get;组; }
///& amp; lt; summary& amp; gt;
/// XMPP连接
///& amp; lt; / summary& amp; gt;
public XMPPConnection ObjXmppCon {get;组; }
公共字符串用户名;
公共字符串密码;
public readonly string server =taurus;
private Boolean IsXmppSuccess {get;组; }
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
private void IsXmppValid()
{
ObjXmppClient = new XMPPClient();
//初始化带凭证的xmpp客户端
ObjXmppClient.JID =用户名+@+服务器;
ObjXmppClient.Password =密码;
ObjXmppClient.Server =服务器; //模拟器的用户服务器和设备的IP地址。
ObjXmppClient.AutoReconnect = true;
ObjXmppClient.RetrieveRoster = true;
ObjXmppClient。 PresenceStatus = new PresenceStatus(){PresenceType = PresenceType.available,IsOnline = true};
ObjXmppClient.AutoAcceptPresenceSubscribe = true;
ObjXmppClient.AttemptReconnectOnBadPing = true;
XMPPConnection ObjXmppCon = new XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();
//初始化xmpp连接
ObjXmppCon.OnAsyncConnectFinished + = ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged + = new EventHandler(xMPPClient_OnStateChanged);
Thread.Sleep(2000); //当前上下文中不存在名称线程
}
void ObjXmppCon_OnAsyncConnectFinished(xmedianet。 socketserver.SocketClient client,bool bSuccess,string strErrors)
{
IsXmppSuccess = client.Connected;
}
void xMPPClient_OnStateChanged(object sender,EventArgs e)
{
开关(ObjXmppClient.XMPPState)
{
案例XMPPState.Ready:
if(IsXmppSuccess)
{
this.Dispatcher.BeginInvoke(()=& amp; gt; //错误!windows.ui.core.core调度员
//不包含begin invoke的定义,也没有扩展名methode
//'开始调用接受第一个参数类型windows.ui.core.coredispatcher'
{//可以找到(你是否错过了使用指令或汇编参考?)
NavigationService.Navigate((新的Uri(/ Output.xaml?key = success,UriKind.Relative)) );
//返回;
});
}
其他
{
this.Dispatcher.BeginInvoke(()=& amp; gt; //错误! windows.ui.core.core调度程序
//不包含begin invoke的定义,没有扩展名methode
//'begin invoke接受第一个参数类型windows.ui .core.coredispatcher'
//可以找到(你是否错过了使用指令或汇编参考?)
{
MessageBox.Show (检查服务器名称/ IpAddress);
//NavigationService.Navigate((new Uri(/ Output.xaml?key = failed,UriKind.Relative)));
返回;
});
}
休息;
case XMPPState.AuthenticationFailed:this.Dispatcher.BeginInvoke(()=& amp; gt; // er ROR! windows.ui.core.core调度程序
//不包含begin invoke的定义,没有扩展名methode
//'begin invoke接受第一个参数类型windows.ui .core.coredispatcher'
//可以找到(你是否错过了使用指令或汇编参考?)
{
MessageBox.Show (输入有效的用户名和密码);
//NavigationService.Navigate((new Uri(/ Output.xaml?key = wrong,UriKind.Relative)));
返回;
});休息;
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click(对象发送者,RoutedEventArgs e)
{
}
}
}& lt; / pre& gt;< / pre>
hai all please help me am trying to connect my app with openfire server here.
[ignore my button click event]
im having trouble in:
1. Thread.Sleep(2000); //the name thread does not exist in the current context
2.this.Dispatcher.BeginInvoke(() =>//error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
{ //coul be found( are you missing a using directive or assembly refference?)
<pre>my code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Net.XMPP;
using System.Threading;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641
namespace chat_tral
{
/// &lt;summary&gt;
/// An empty page that can be used on its own or navigated to within a Frame.
/// &lt;/summary&gt;
public sealed partial class MainPage : Page
{
/// &lt;summary&gt;
/// Xmpp Client
/// &lt;/summary&gt;
public XMPPClient ObjXmppClient { get; set; }
/// &lt;summary&gt;
/// XMPP Connection
/// &lt;/summary&gt;
public XMPPConnection ObjXmppCon { get; set; }
public string username;
public string password;
public readonly string server = "taurus";
private Boolean IsXmppSuccess { get; set; }
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
private void IsXmppValid()
{
ObjXmppClient = new XMPPClient();
//initializing the xmpp client with credentials
ObjXmppClient.JID = username + "@" + server;
ObjXmppClient.Password = password ;
ObjXmppClient.Server = server; //user server for emulator and ip address for device.
ObjXmppClient.AutoReconnect = true;
ObjXmppClient.RetrieveRoster = true;
ObjXmppClient.PresenceStatus = new PresenceStatus() { PresenceType = PresenceType.available, IsOnline = true };
ObjXmppClient.AutoAcceptPresenceSubscribe = true;
ObjXmppClient.AttemptReconnectOnBadPing = true;
XMPPConnection ObjXmppCon = new XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();
//initializing the xmpp connection
ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);
Thread.Sleep(2000); //the name thread does not exist in the current context
}
void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient client, bool bSuccess, string strErrors)
{
IsXmppSuccess = client.Connected;
}
void xMPPClient_OnStateChanged(object sender, EventArgs e)
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSuccess)
{
this.Dispatcher.BeginInvoke(() =&gt;//error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
{ //coul be found( are you missing a using directive or assembly refference?)
NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));
//return;
});
}
else
{
this.Dispatcher.BeginInvoke(() =&gt;//error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
//coul be found( are you missing a using directive or assembly refference?)
{
MessageBox.Show("Check server name/IpAddress");
//NavigationService.Navigate((new Uri("/Output.xaml?key=failed", UriKind.Relative)));
return;
});
}
break;
case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =&gt; //error! windows.ui.core.core dispatcher
//doesnot contains the definition for begin invoke and no extension methode
//'begin invoke accepting a first argument type windows.ui.core.coredispatcher'
//coul be found( are you missing a using directive or assembly refference?)
{
MessageBox.Show("Enter valid username and password");
//NavigationService.Navigate((new Uri("/Output.xaml?key=wrong", UriKind.Relative)));
return;
}); break;
}
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}</pre></pre>
推荐答案
这很简单,但需要很长时间来解释所有细节,所以这里有一个例子:
假设您有一些代码,例如
This is simple, but it would take long time to explain all the detail, so here is an example for you:
Suppose you have some code like
void MyFunction(A a, B b, C c) {/* ... */}
//...
A aValue = //...
B bValue = //...
C cValue = //...
MyFunction(aValue, bValue, cValue);
如何使用将其委托给UI线程this.Dispatcher.BeginInvoke
?您应该传递适当委托类型的委托实例,然后传递要传递给委托实例的参数集。具体如下:
How to delegate it to the UI thread with this.Dispatcher.BeginInvoke
? You should pass a delegate instance of appropriate delegate type, followed by the set of parameters to be passed to the delegate instance. This is how:
this.Dispatcher.BeginInvoke(new System.Action<A, B, C>((a, b, c)=>{
// some code using a, b and c
}), aValue, bValue, cValue);
方法 BeginInvoke
将传递 aValue
, bValue
和 cValue
- 实际参数 - 调用委托实例作为参数 a
, b
和 c
。请注意,在这种委托描述形式中,编译器使用类型推断。我建议你自己学习它是如何工作的,并分析所有类型的角色( Action
,委托类型,委托实例对象类型等等)。
Method BeginInvoke
will pass aValue
, bValue
and cValue
— actual parameters — to the invocation of the delegate instance as the parameters a
, b
and c
. Note, that in this form of delegate description, the compiler uses type inference. I suggest you learn how it works and analyze the role of all types (Action
, delegate type, delegate instance object type and so on) by yourself.
这篇关于为什么在Windows Phone 8.1 xmpp连接到openfire时开始调用显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!