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";
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;
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();
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)
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSucces)
{
this.Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));
});
}
else
{
this.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Check server name/IpAddress");
return;
});
}
break;
case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Enter valid username and password");
return;
}); break;
}
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
if (txtUserName.Text.Trim() == string.Empty)
{
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)
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Ready:
if (IsXmppSucces)
{
this.Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate((new Uri("/Output.xaml?key=success", UriKind.Relative)));
});
}
else
{
this.Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Check server name/IpAddress");
return;
});
}
break;
case XMPPState.AuthenticationFailed: this.Dispatcher.BeginInvoke(() =>
{
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
}