单例类抛出名称不存在,错误

编程入门 行业动态 更新时间:2024-10-25 14:35:23
本文介绍了单例类抛出名称不存在,错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨我正在尝试使用单例类的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参数错误 }

解决方案

所以有几件事。你的单身人士不是线程安全的,并且可以按照现在的方式进行比赛。关于它的文章非常好: csharpindepth/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(&quot;Successfully logged in&quot;); NavigationService.Navigate((new Uri(&quot;/Output.xaml?key=success&quot;, UriKind.Relative)));//error //return; }); } else { this.Dispatcher.BeginInvoke(() =&gt;//error { MessageBox.Show(&quot;Check server name/IpAddress&quot;); return; }); } break; case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =&gt;//error { MessageBox.Show(&quot;Enter valid username and password&quot;); 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: csharpindepth/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.

更多推荐

单例类抛出名称不存在,错误

本文发布于:2023-11-16 21:14:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不存在   抛出   错误   名称   单例类

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!