0

SetLogText 以下のメソッドでメイン GUI を更新したいのですが、うまくいきません。エラーはありません。GUI を更新するにはどうすればよいですか?

C# コード

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.PeerResolvers;

namespace Client
{
    public partial class Form1 : Form
    {

        IChatChannel participant;
        DuplexChannelFactory<IChatChannel> factory;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {}



        // Chat service contract
        // Applying [PeerBehavior] attribute on the service contract enables retrieval of PeerNode from IClientChannel.
        [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", CallbackContract = typeof(IChat))]
        public interface IChat
        {
            [OperationContract(IsOneWay = true)]
            void Join(string member);

            [OperationContract(IsOneWay = true)]
            void Chat(string member, string msg);

            [OperationContract(IsOneWay = true)]
            void Leave(string member);
        }

        public interface IChatChannel : IChat, IClientChannel
        {}




        public class ChatApp : IChat

        {
            // member id for this instance
            string member;

            public ChatApp(string member)
            {
                this.member = member;
            }


            //IChat implementation
            public void Join(string member)
            {
                Console.WriteLine("[{0} joined]", member);
            }



            public void Chat(string member, string msg)
            {

                try
                {                      
                    Form1 myForm = new Form1();
                    myForm.SetLogText("eureka");
                }
                catch (Exception ex)
                {    
                    MessageBox.Show(ex.Message.ToString());    
                }



            }

            public void Leave(string member)
            {
                Console.WriteLine("[{0} left]", member);
            }

        }


        // PeerNode event handlers
        public void OnOnline(object sender, EventArgs e)
        {
            radListViewChats.Items.Add("Another user went  Online");
        }
        public void OnOffline(object sender, EventArgs e)
        {
            radListViewChats.Items.Add("Another user went  Offline");
        }

        private void radButtonConnect_Click(object sender, EventArgs e)
        {
            try
            {   

                // Construct InstanceContext to handle messages on callback interface. 
                // An instance of ChatApp is created and passed to the InstanceContext.
                InstanceContext instanceContext = new InstanceContext(new ChatApp(radTextBoxusername.Text));

                // Create the participant with the given endpoint configuration
                // Each participant opens a duplex channel to the mesh
                // participant is an instance of the chat application that has opened a channel to the mesh
                factory = new DuplexChannelFactory<IChatChannel>(instanceContext, "ChatEndpoint");


                participant = factory.CreateChannel();

                // Retrieve the PeerNode associated with the participant and register for online/offline events
                // PeerNode represents a node in the mesh. Mesh is the named collection of connected nodes.  



                IOnlineStatus ostat = participant.GetProperty<IOnlineStatus>();
                ostat.Online += new EventHandler(OnOnline);
                ostat.Offline += new EventHandler(OnOffline);

                try
                {
                    participant.Open();
                }
                catch (CommunicationException)
                {


                    radListViewChats.Items.Add("Could not find resolver.  If you are using a custom resolver, please ensure");
                    radListViewChats.Items.Add("that the service is running before executing this sample.  Refer to the readme");
                    radListViewChats.Items.Add("for more details.");
                    return;
                }


                radListViewChats.Items.Add("You are connected: ", radTextBoxusername.Text);


                // Announce self to other participants
                participant.Join(radTextBoxusername.Text);

            }


            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
        }
        private void radButtonLeave_Click(object sender, EventArgs e)
        {
            try
            {
                participant.Leave(radTextBoxusername.Text);
                participant.Close();
                factory.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());


            }
        }
        private void radButtonChat_Click(object sender, EventArgs e)
        {
            participant.Chat(radTextBoxusername.Text, radTextBoxText2Send.Text);
        }


        public void SetLogText(String text)
        {   

                if (this.InvokeRequired)
                {
                    this.Invoke(new EventHandler(delegate
                    {
                        textBox1.Text = text;
                    }));
                }
                else
                    textBox1.Text = text;

        }

    }
}
4

1 に答える 1

2

Form1 の新しいインスタンスで SetLogText を呼び出しています。そのインスタンスに影響を与えるには、既存のインスタンスで呼び出す必要があります。プロジェクトを構成するためにコードを複数のファイルに分割すると役立つ場合があります。そうすると、Chat は Form1 について何も知らず、プログラムのある時点でそのインスタンスを与える必要があることが明らかになるかもしれません。

例:

public class ChatApp : IChat
{
   string member;
   Form1 form;

   public ChatApp(string member, Form1 form)
   {
        this.member = member;
        this.form = form;
   }

   public void Join(string member)
   {
       Console.WriteLine("[{0} joined]", member);
   }


   public void Chat(string member, string msg)
   {
       try
       {                      
            form.SetLogText("eureka");
       }
       catch (Exception ex)
       {    
           MessageBox.Show(ex.Message.ToString());    
       }
   }

   public void Leave(string member)
   {
       Console.WriteLine("[{0} left]", member);
   }
}
于 2013-03-07T18:01:37.157 に答える