2015-10-28 40 views
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(); 
    } 



    } 
} 
+0

我不明白。您可以使用'MainPage.Singleton'轻松访问您的单身人士。也就是说,最好把它放在它自己的类中,而不是在MainPage –

+0

中,你能解释一下吗? – rose

回答

0

最好的方式做到这一点,我认为是使用依赖注入(Ninject将是一件好事)。通过这种方式,您可以在界面后面开发您的ServerConnection,并将其注入到您的页面用于其数据上下文的视图模型中。像下面的东西会工作。

辛格尔顿结合

Bind<IServerConnection>().To<ServerConnection>().InSingletonScope(); 

使用InSingletonScope的界面结合实施后,你只会有一个实例运行。因此,您可以使用构造函数注入将其注入到视图模型中,并在需要时使用它。

这应该会使开发聊天应用程序变得更容易。我希望这有帮助。

相关问题