我正在尝试在我的第一页中在wp8.1中开发一个Windows聊天应用程序我在singleton类中创建了一个服务器连接。我创建了另一个用于发送消息的窗口。使用singleton如何在下一页中维护服务器连接也? 我的发送按钮在下一页。所以如何在第二页中使用单例来维护我的连接。 提前谢谢
这是我使用singleton
进行服务器连接的第一页代码 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();
}
}
}
答案 0 :(得分:0)
在我看来,最好的方法是使用依赖注入(Ninject会很好)。这样,您可以在界面后面开发ServerConnection,并将其注入您的页面用于其数据上下文的View Models中。像下面的东西会起作用。
单身人士绑定
Bind<IServerConnection>().To<ServerConnection>().InSingletonScope();
使用InSingletonScope将接口绑定到实现后,您将只运行该实例。因此,您可以使用构造函数注入将其注入视图模型,并在需要时使用它。
这应该使聊天应用程序的开发变得更加容易。我希望这会有所帮助。