0

最初のページで wp8.1 で Windows チャット アプリを開発しようとしています。シングルトン クラスでサーバー接続を作成しました。メッセージを送信するための別のウィンドウを作成しました。シングルトンを使用して、次のページでもサーバー接続を維持するにはどうすればよいですか? 私の送信ボタンは次のページにあります.singletonを使用して2番目のページで接続を維持するにはどうすればよいですか?? 前もって感謝します

これは、シングルトンを使用したサーバー接続の最初のページ コードです。

  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();
    }



   }
}
4

1 に答える 1

0

私の意見では、これを行う最善の方法は、依存性注入を使用することです ( Ninjectが良いでしょう)。このようにして、インターフェイスの背後で ServerConnection を開発し、それをページがデータ コンテキストに使用しているビュー モデルに挿入するだけです。以下のようなものが機能します。

シングルトンバインディング

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

InSingletonScope を使用してインターフェイスを実装にバインドすると、その 1 つのインスタンスのみが実行されます。そのため、コンストラクター注入を使用してビュー モデルに注入し、必要な場所で使用できます。

これにより、チャット アプリケーションの開発が非常に簡単になります。これが役立つことを願っています。

于 2015-10-28T11:17:33.977 に答える