0
我想在第一頁中開發wp8.1中的Windows聊天應用程序,我在單例類中創建了一個服務器連接。我創建了另一個用於發送消息的窗口。使用單例我怎麼能維護下一頁的服務器連接呢? 我的發送按鈕是在下一頁page.so我怎麼能保持我的連接在第二頁也使用單身。 由於事先如何在下一頁中保持連接(在單例類中創建)
這是用我的服務器連接第一頁碼單
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 volatile Singleton instance;
private static object syncRoot = new Object();
public static XMPPConnection ObjXmppCon;
public static XMPPClient ObjXmppClient;
public static Boolean IsXmppSuccess { get; set; }
public String UserName { get; set; }
public String PassWord { get; set; }
public readonly String Server = "taurus";
public readonly String ServerIPAddress = "127.0.0.1:9090";
public sealed class Singleton
{
private Singleton() { }
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (ObjXmppCon == null)
instance = new Singleton();
}
}
return instance;
}
}
}
public void IsXmppValid()
{
ObjXmppClient = new XMPPClient();
ObjXmppClient.JID = UserName + "@" + Server;
ObjXmppClient.Password = PassWord;
ObjXmppClient.Server = 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();
//initializing the xmpp connection
ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged += new EventHandler(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 (IsXmppSuccess)// the name isxmpp does not contain in the current context
{
this.Dispatcher.BeginInvoke(() =>
{
NavigationService.Navigate((new Uri("/Output.xaml? key=success", UriKind.Relative)));//error
});
}
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();
}
}
}
我不明白。您可以使用'MainPage.Singleton'輕鬆訪問您的單身人士。也就是說,最好把它放在它自己的類中,而不是在MainPage –
中,你能解釋一下嗎? – rose