Windows phone 8.1 如何在下一页维护连接(在singleton类中创建)

Windows phone 8.1 如何在下一页维护连接(在singleton类中创建),windows-phone-8.1,xmpp,c#-3.0,openfire,singleton-methods,Windows Phone 8.1,Xmpp,C# 3.0,Openfire,Singleton Methods,我正在尝试在wp8.1中开发windows聊天应用程序。在我的第一页中,我在singleton类中创建了一个服务器连接。我创建了另一个用于发送消息的窗口。使用singleton如何在下一页中维护服务器连接? 我的发送按钮在下一页中。那么,我如何在第二页中使用singleton保持连接呢。?? 提前谢谢 这是我使用singleton进行服务器连接的第一页代码 using System; using System.Collections.Generic; using System.Li

我正在尝试在wp8.1中开发windows聊天应用程序。在我的第一页中,我在singleton类中创建了一个服务器连接。我创建了另一个用于发送消息的窗口。使用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();
    }



   }
}

在我看来,最好的方法是使用依赖注入(这很好)。通过这种方式,您可以在接口后面开发ServerConnection,并将其插入页面用于其数据上下文的视图模型中。像下面这样的东西会有用的

单例绑定

Bind<IServerConnection>().To<ServerConnection>().InSingletonScope();
Bind().To().InSingletonScope();
在使用InSingletonScope将接口绑定到实现之后,您将只运行一个实例。因此,您可以使用构造函数注入将其注入到视图模型中,并在需要时使用它


这将使聊天应用程序的开发更加容易。我希望这有帮助。

我不明白。您可以使用
MainPage.singleton
从另一个页面轻松访问您的singleton。也就是说,最好把它放在自己的类中,而不是放在主页上。你能解释一下吗?