一个解决方案中两个项目和事件处理程序之间的接口事件

Interface Event between two projects in one solution and Event Handlers

我在我的解决方案中分离了两个项目,因为它们每个都需要针对不同 CPU 的库。 在我的一个项目中,我只有 类 响应点击(我们称之为 ProjectClick 64 位库),另一个是一种 UI 使用 MVVM 实现(ProjectUser 32 位库)。

我正在寻找的是一种让 ProjectUser 知道点击是由 ProjectClick 执行的方法,不需要 Project Click 知道任何其他事情。

到目前为止我尝试了什么

我一直在浏览网页和书籍,以更多地了解 C#。据我了解,要进行交流,最好的方法是创建一个界面。我一直在寻找这个 subject 的答案,并一直在尝试实现第三个项目,该项目具有两者之间的接口。

好的,下面是代码,(这是一个有意简化的代码,我希望它足够清楚)

首先是界面(在控制台应用程序中)

namespace LinkApplication
{
    public interface IEvent
    {

        bool CompareClick { get; set; }
    }

 }

然后,点击其中的项目是一个wpf

namespace ProjectClick

public partial class MainWindow : Window, IEvent

{

    public MainWindow()
    {

        try { InitializeComponent(); }
        catch (Exception e)
        {
            Console.WriteLine(e.InnerException);
        }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
         CompareClick = true;
    }
    private void Button_Leave(object sender, RoutedEventArgs e)
    {
         CompareClick = false;
    }


}

终于UI

namespace ProjectUser
{


public partial class MainWindow : Window, IEvent, INotifyPropertyChanged
{

    public MainWindow()
    {
        InitializeComponent();

        this.WindowStartupLocation = WindowStartupLocation.CenterScreen;    //start the window at the centre of the screen
        DataContext = this;

    }

    public bool CompareClick { get; set; }

    public bool ClickCheck
    {
        get { return CompareClick; }
        set
        {
            if (value != CompareClick)
            {
                CompareClick = value;
                OnPropertyChanged("ClickCheck");
            }
        }
    }

您可以在 Window

中看到真实的标签
<Label Content="{Binding ClickCheck}" HorizontalAlignment="Left" Margin="690,358,0,0" VerticalAlignment="Top"/>

这里的值一直是false,我不是很明白这个值变化的逻辑。我还在学习,我在网上看到了其他几个想法,比如 custom EventHandler,但我不太了解两个项目之间的实现,因为它们彼此不了解。如果有人可以引导我找到可能的解决方案或更好的执行方式,我将很高兴。

编辑 我最好避免在 ProjectUser 中引用 Project Click 以保留不同 CPU 目标的特权。反过来也没问题。

感谢您的热心回答。

在这里,你误解了什么是接口。接口的每个实现都是不同的。当您单击该按钮时,ProjectClick 项目的 MainWindowCompareClick 属性 会更改值。但这并没有改变 ProjectUser 项目的 MainWindow。他们是两个完全不同的对象!我现在能想到的最好的方法是制作按钮public。或者,您可以在 ProjectClickMainWindow class 中创建一个方法。使用此方法订阅点击事件。像这样:

public void SubscribeToClickEvent (EventHandler handler) {
    this.Button.Click += handler //whatever your button is called
}

如果要封装Button,用上面的方法。如果你不这样做,那就把它 public.

你会问,我怎样才能访问 MainWindow 的实例来使用该方法?我能想到的唯一方法是让 MainWindow 成为单身人士。

正如其他人所指出的,您的接口概念仍然是错误的。但是我明白你想做什么。

试试这个:

namespace LinkApplication
{
    public interface IEventReceiver
    {
        void Receive<T>(T arg) where T : EventArgs;
    }

    public class SomeUniqueEvent : EventArgs
    {
        public bool Clicked { get; set; }

        public SomeUniqueEvent(bool clicked)
        {
            Clicked = clicked;
        }
    }

    public static class EventTunnel
    {
        private static readonly List<IEventReceiver> _receivers = new List<IEventReceiver>();
        public static void Publish<T>(T arg) where T : EventArgs
        {
            foreach (var receiver in _receivers)
            {
                receiver.Receive(arg);
            }
        }

        public static void Subscribe(IEventReceiver subscriber)
        {
            _receivers.Add(subscriber);
        }
    }
}

namespace ProjectClick
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {

            try { InitializeComponent(); }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException);
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LinkApplication.EventTunnel.Publish(new LinkApplication.SomeUniqueEvent(true));
        }
        private void Button_Leave(object sender, RoutedEventArgs e)
        {
            LinkApplication.EventTunnel.Publish(new LinkApplication.SomeUniqueEvent(false));
        }
    }
}

namespace ProjectUser
{

    public partial class MainWindow : Window, LinkApplication.IEventReceiver, INotifyPropertyChanged
    {

        public MainWindow()
        {
            InitializeComponent();

            this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
            DataContext = this;
            LinkApplication.EventTunnel.Subscribe(this);

        }

        public bool CompareClick { get; set; }

        public bool ClickCheck
        {
            get { return CompareClick; }
            set
            {
                if (value != CompareClick)
                {
                    CompareClick = value;
                    OnPropertyChanged("ClickCheck");
                }
            }
        }

        public void Receive<T>(T arg) where T : EventArgs
        {
            var casted = arg as SomeUniqueEvent;
            if (casted != null)
            {
                ClickCheck = casted.Clicked;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我得到了很大的建议,并研究了实例之间的进程间通信。我调查了不同的事情,但最令人满意的答案是 Omegaman's blog(感谢这个主题)。

所以基本上,我试图避免使用本地主机信息,认为会有更直接的解决方案。但由于我们没有想到更好的东西,我想这就是我一直在寻找的东西。

我发现了什么

所以现在,这里的解决方案是使用带有 NamedPipes 的 WCF 服务。通过创建 Sender 和 Receiver 操作,ProjectUserProjectClick 这两个进程永远不会直接相遇。您有一个由 WCF 控制的管道。你可以在博客上看到更多关于如何交流的细节,我只是通过改变传递的信息来改编(没有太大的改变)他所做的。

但是要注意一点

两个进程不能同时启动,接收者必须先启动才能监听通过的信息。基本上,发件人必须在之后开始。

我在 WPF 中创建了两个 windows 和一个 WCFServiceLibrary。单击按钮时,会增加,并在第二个屏幕上显示数字。

一点代码

Omegaman 的博客上可以看到很多,我就post 改一下吧。

ProjectUser这边,应该是收到了,标签更新如下 接收管道 = new Receiver(); public 主窗口() { InitializeComponent();

        //this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
        DataContext = this;
        pipe.Data += new PipeLink.PipeService.DataIsReady(DataBeingRecieved);
        if (pipe.ServiceOn() == false)
            MessageBox.Show(pipe.error.Message);

        label1.Content = "Listening to Pipe: " + pipe.CurrentPipeName + Environment.NewLine;
    }

    void DataBeingRecieved(int data)
    {
        Dispatcher.Invoke(new Action(delegate()
        {
            label1.Content += string.Join(Environment.NewLine, data);
            label1.Content += Environment.NewLine;
        }));
    }

ProjectClick这边,应该发送,按钮点击更新如下

 int i;
    public MainWindow()
    {
         try { InitializeComponent(); }
        catch (Exception e)
        {
            Console.WriteLine(e.InnerException);
        }
         i = 0;

    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        int messages;

        i++;

        Stopwatch stoop = new Stopwatch();
        stoop.Start();
        messages = i;
        try
        {
            PipeLink.Sender.SendMessage(messages);
            stoop.Stop();
            Console.WriteLine(stoop.ElapsedMilliseconds + " ms");

        }
        catch (Exception u)
        {
            Console.WriteLine(u);
        }
    }

代码的重要部分是管道本身的创建,使用 NetNamedPipeBinding。这是整个通信发生的地方

在PipeService代码中可以看到:

public class PipeService : IPipeService
{

    public static string URI
       = "net.pipe://localhost/Pipe";

    // This is when we used the HTTP bindings.
    // = "http://localhost:8000/Pipe";

    #region IPipeService Members

    public void PipeIn(int data)
    {
        if (DataReady != null)
            DataReady(data);
    }

    public delegate void DataIsReady(int hotData);
    public DataIsReady DataReady = null;

    #endregion
}

速度怎么样?

我担心简单的数据可能比简单的点击需要更长的时间才能到达。我错了:由于第一次连接,第一个数字比其他数字花费的时间更长,大约一秒钟。但在那之后,点击大约 100 次,我平均有 10 毫秒(我知道这不是重要数据,但我仍然认为测试几次是好的)。

我正在为任何可能感兴趣的人推送与 Andreas 一起使用的 GitHub 上的所有内容。

我仍然不知道代码是否优化。如果您有更好的解决方案,我会很乐意阅读。