单例类抛出名称不存在,错误 [英] singleton class throwing name doesn't existing, error
问题描述
嗨我正在尝试使用单例类的xmpp连接,但是一个方法和一个变量(isxmppsuccess)显示错误。我的文本框也会抛出错误!
如何在下一页中维护连接?
i是单身人士的新手
下面提到的
是我的完整代码,第二个是错误行:
我的完整代码:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Net;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Navigation;
使用 Microsoft.Phone.Controls;
使用 Microsoft.Phone.Shell;
使用 WP8Xmpp.Resources;
使用 System.Net.XMPP;
使用 System.Threading;
命名空间 WP8Xmpp
{
public partial class MainPage:PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private static XMPPClient ObjXmppClient;
public class singleton
{
private static 单例实例;
private static XMPPConnection ObjXmppCon;
private 布尔 IsXmppSuccess {获得跨度>; set ; }
public 字符串用户名{获得跨度>; set ; }
public 字符串密码{获得跨度>; set ; }
private readonly String Server = taurus;
private readonly 字符串 ServerIPAddress = 127.0.0.1:9090; // 127.0.0.1:9090 /
私有 singleton()
{
}
public static singleton GetInstance()
{
if ( instance == null )
{
instance = new singleton ();
}
return instance;
}
public static void IsXmppValid( string UserName, string PassWord)
{
ObjXmppClient = new XMPPClient();
ObjXmppClient.JID = UserName + @ + instance.Server;
ObjXmppClient.Password = PassWord;
ObjXmppClient.Server = instance.ServerIPAddress; // 模拟器的用户服务器和设备的IP地址。
ObjXmppClient.AutoReconnect = 真跨度>;
ObjXmppClient.RetrieveRoster = true ;
ObjXmppClient.PresenceStatus = new PresenceStatus(){PresenceType = PresenceType.available,IsOnline = true };
ObjXmppClient.AutoAcceptPresenceSubscribe = true ;
ObjXmppClient.AttemptReconnectOnBadPing = true ;
ObjXmppCon = new XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();
// 初始化xmpp连接
ObjXmppCon.OnAsyncConnectFinished + = instance.ObjXmppCon_OnAsyncConnectFinished ;
ObjXmppClient.OnStateChanged + = new EventHandler(instance.XMPPClient_OnStateChanged);
Thread.Sleep( 2000 );
}
public void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient客户端, bool bSuccess, string strErrors)
{
IsXmppSuccess = client.Connected;
}
public void XMPPClient_OnStateChanged( object sender,EventArgs e) // 错误
{
开关(ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSucces) // 错误
{
此 .Dispatcher.BeginInvoke (()= >
{
// CloseOverLay();
// MessageBox.Show(成功登录);
NavigationService .Navigate(( new Uri( / Output.xaml ?key = success,UriKind.Relative))); // 错误
// return;
});
}
其他
{
此 .Dispatcher.BeginInvoke(()= > // 错误
{
MessageBox.Show( 检查服务器名称/ IpAddress );
return ;
});
}
break ;
case XMPPState.AuthenticationFailed: this .Dispatcher.BeginInvoke(()= > // 错误
{
MessageBox.Show( 输入有效的用户名和密码);
return ;
}); break ;
}
}
private void btnLogin_Click( object sender,RoutedEventArgs e) // 错误
{
if ( txtUserName.Text.Trim()== string .Empty) // 错误
{
MessageBox.Show( 输入用户名< /跨度>);
return ;
}
if (txtPassword.Password.Trim()== string 。空)
{
MessageBox.Show( 输入密码);
return ;
}
UserName = txtUserName.Text.Trim();
PassWord = txtPassword.Password.Trim();
IsXmppValid();
}
}
}
}
2>我在这些方法中遇到错误:
public void XMPPClient_OnStateChanged( object sender,EventArgs e) // 错误
{
开关(ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSucces) // 不存在错误
{
this .Dispatcher.BeginInvoke(()= > // < pre> Dispatcher - 不包含定义错误
{
// CloseOverLay();
// MessageBox.Show(& quot;成功登录& quot;);
NavigationService.Navigate (( new Uri(& quot; /Output.xaml?key = success& quot;,UriKind.Relative))); // 错误
// 返回;
});
}
其他
{
此 .Dispatcher.BeginInvoke(()=& gt; // 错误
{
MessageBox.Show(& quot; Check server name / IpAddress& quot;);
return ;
});
}
break ;
case XMPPState.AuthenticationFailed: this .Dispatcher.BeginInvoke(()= & gt; // 错误
{
MessageBox.Show(& 输入有效的用户名和密码& quot;);
return ;
}); break ;
}
}
< / pre >
2.
private void btnLogin_Click(object sender,RoutedEventArgs e)//错误
{
if(txtUserName.Text.Trim()== string.Empty)//无法访问非静态成员
{
MessageBox.Show(输入用户名);
返回;
}
if(txtPassword.Password.Trim() == string.Empty)
{
MessageBox.Show(输入密码);
返回;
}
UserName = txtUserName.Text.Trim();
PassWord = txtP assword.Password.Trim();
IsXmppValid(); // 0参数错误
}
所以有几件事。你的单身人士不是线程安全的,并且可以按照现在的方式进行比赛。关于它的文章非常好:
http://csharpindepth.com/Articles/General/Singleton.aspx
最简单修复是将实例属性更改为:
private 静态懒惰< singleton> instance = new Lazy< singleton>(()= > new singleton());
public static singleton GetInstance { get { return instance.Value;现在,您所看到的问题与将事件传递给调用私有变量的其他对象(IsXmppSuccess)有关。现在,您需要看到的问题是什么?$ / p $ p>
。将该变量公之于众。
Hi iam trying an xmpp connection using singleton class,but a methode and a variable(isxmppsuccess) showing errors.My textboxes also throwing error!
how i can maintain the connection in next page also?
i am new to the singleton
below mentioned is my full code and second one is error lines:
my full code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP8Xmpp.Resources;
using System.Net.XMPP;
using System.Threading;
namespace WP8Xmpp
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private static XMPPClient ObjXmppClient;
public class singleton
{
private static singleton instance;
private static XMPPConnection ObjXmppCon;
private Boolean IsXmppSuccess { get; set; }
public String UserName { get; set; }
public String PassWord { get; set; }
private readonly String Server = "taurus";
private readonly String ServerIPAddress = "127.0.0.1:9090";//127.0.0.1:9090/
private singleton()
{
}
public static singleton GetInstance()
{
if (instance == null)
{
instance = new singleton();
}
return instance;
}
public static void IsXmppValid(string UserName, string PassWord)
{
ObjXmppClient = new XMPPClient();
ObjXmppClient.JID = UserName + "@" + instance.Server;
ObjXmppClient.Password = PassWord;
ObjXmppClient.Server = instance.ServerIPAddress; //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;
ObjXmppCon = new XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();
//initializing the xmpp connection
ObjXmppCon.OnAsyncConnectFinished +=instance.ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged += new EventHandler(instance.XMPPClient_OnStateChanged);
Thread.Sleep(2000);
}
public void ObjXmppCon_OnAsyncConnectFinished(xmedianet.socketserver.SocketClient client, bool bSuccess, string strErrors)
{
IsXmppSuccess = client.Connected;
}
public void XMPPClient_OnStateChanged(object sender, EventArgs e)//error
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSucces)//error
{
this.Dispatcher.BeginInvoke(() =>
{
//CloseOverLay();
//MessageBox.Show("Successfully logged in");
NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));//error
//return;
});
}
else
{
this.Dispatcher.BeginInvoke(() =>//error
{
MessageBox.Show("Check server name/IpAddress");
return;
});
}
break;
case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =>//error
{
MessageBox.Show("Enter valid username and password");
return;
}); break;
}
}
private void btnLogin_Click(object sender, RoutedEventArgs e)//error
{
if (txtUserName.Text.Trim() == string.Empty)//error
{
MessageBox.Show("Enter Username");
return;
}
if (txtPassword.Password.Trim() == string.Empty)
{
MessageBox.Show("Enter Password");
return;
}
UserName = txtUserName.Text.Trim();
PassWord = txtPassword.Password.Trim();
IsXmppValid();
}
}
}
}
2> i am getting error in these methodes:
public void XMPPClient_OnStateChanged(object sender, EventArgs e)//error
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSucces) // doesnot exist error
{
this.Dispatcher.BeginInvoke(() =>//<pre>Dispatcher-doesnot contain the definition error
{
//CloseOverLay();
//MessageBox.Show("Successfully logged in");
NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));//error
//return;
});
}
else
{
this.Dispatcher.BeginInvoke(() =>//error
{
MessageBox.Show("Check server name/IpAddress");
return;
});
}
break;
case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =>//error
{
MessageBox.Show("Enter valid username and password");
return;
}); break;
}
}
</pre>
2.
private void btnLogin_Click(object sender, RoutedEventArgs e)//error
{
if (txtUserName.Text.Trim() == string.Empty)//cannot access non static member
{
MessageBox.Show("Enter Username");
return;
}
if (txtPassword.Password.Trim() == string.Empty)
{
MessageBox.Show("Enter Password");
return;
}
UserName = txtUserName.Text.Trim();
PassWord = txtPassword.Password.Trim();
IsXmppValid();//0 arguments error
}
So a couple of things. Your singleton isn't thread safe, and could race with the way it's implemented now. There is an excellent article about it at:
http://csharpindepth.com/Articles/General/Singleton.aspx
The easiest fix is to change the instance property to:
private static Lazy<singleton> instance = new Lazy<singleton>(() => new singleton()); public static singleton GetInstance { get { return instance.Value; } }
Now, the issue that you're seeing is related to passing events to other objects that call a private variable (IsXmppSuccess). Make that variable public.
这篇关于单例类抛出名称不存在,错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!