Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个解决方案中两个项目之间的接口事件和事件处理程序_C#_Wpf_Interface_Eventhandler - Fatal编程技术网

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

C# 一个解决方案中两个项目之间的接口事件和事件处理程序,c#,wpf,interface,eventhandler,C#,Wpf,Interface,Eventhandler,我在解决方案中分离了两个项目,因为它们都需要针对不同CPU的库。 在我的一个项目中,我只有响应点击的类(我们称之为ProjectClick 64位库),另一个是一种带有MVVM实现的UI(ProjectUser 32位库) 我要搜索的是一种让项目用户知道点击是由项目点击执行的方法,,而项目点击不知道任何其他内容 到目前为止我都试过了 我一直在散布网络和书籍,以便更多地了解C。据我所知,沟通的最佳方式是创建一个接口。我一直在寻找答案,并且一直在尝试实施第三个项目,在这两个项目之间有一个接口 好的,

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

我要搜索的是一种让项目用户知道点击是由项目点击执行的方法,,而项目点击不知道任何其他内容

到目前为止我都试过了 我一直在散布网络和书籍,以便更多地了解C。据我所知,沟通的最佳方式是创建一个接口。我一直在寻找答案,并且一直在尝试实施第三个项目,在这两个项目之间有一个接口

好的,代码在这里,(这是一个特意简化的代码,我希望它足够清晰)

首先是接口(在控制台应用程序中)

然后,单击项目,这是一个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");
            }
        }
    }
您可以在窗口中的此处看到不动产标签

<Label Content="{Binding ClickCheck}" HorizontalAlignment="Left" Margin="690,358,0,0" VerticalAlignment="Top"/>

在这里,值总是保持为false,我并不真正理解不断变化的值的逻辑。我还在学习,我在网上看到了一些其他的想法,比如定制事件处理程序,但我并不真正理解彼此不了解的两个项目之间的实现。如果有人能告诉我一个可能的解决方案,或者一个更好的执行方式,我会很高兴

编辑 我希望避免在
项目用户
中引用
项目点击
,以保留不同CPU目标的特权。另一方面也不是问题


感谢您友好的回答。

在这里,您误解了接口是什么。接口的每个实现都是不同的。单击按钮时,
CompareClick
项目属性单击项目的
主窗口
更改值。但这不会改变
ProjectUser
project的
main窗口。它们是两个完全不同的物体!我现在能想到的最好的办法是将按钮
公开
。或者,您可以在
ProjectClick
main窗口中创建一个方法。使用此方法订阅单击事件。大概是这样的:

public void SubscribeToClickEvent (EventHandler handler) {
    this.Button.Click += handler //whatever your button is called
}
如果要封装
按钮
,请使用上述方法。如果你不知道,那就把它公之于众


您会问,如何访问
MainWindow
的实例以使用该方法?我能想到的唯一方法是将
main窗口
设为一个单例。

在这里,您误解了什么是接口。接口的每个实现都是不同的。单击按钮时,
CompareClick
项目属性单击项目的
主窗口
更改值。但这不会改变
ProjectUser
project的
main窗口。它们是两个完全不同的物体!我现在能想到的最好的办法是将按钮
公开
。或者,您可以在
ProjectClick
main窗口中创建一个方法。使用此方法订阅单击事件。大概是这样的:

public void SubscribeToClickEvent (EventHandler handler) {
    this.Button.Click += handler //whatever your button is called
}
如果要封装
按钮
,请使用上述方法。如果你不知道,那就把它公之于众


您会问,如何访问
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;
    }
}
命名空间链接应用程序
{
公共接口IEventReceiver
{
void Receive(T arg),其中T:EventArgs;
}
公共类SomeUniqueEvent:EventArgs
{
公共bool单击{get;set;}
公共SomeUniqueEvent(布尔点击)
{
点击=点击;
}
}
公共静态类隧道
{
私有静态只读列表_receivers=new List();
公共静态无效发布(T arg),其中T:EventArgs
{
foreach(var接收器在_接收器中)
{
receiver.Receive(arg);
}
}
公共静态无效订阅(IEventReceiver订阅方)
{
_接收者。添加(订户);
}
}
}
命名空间项目单击
{
公共部分类主窗口:窗口
{
公共主窗口()
{
请尝试{InitializeComponent();}
捕获(例外e)
{
Console.WriteLine(例如InnerException);
}
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
LinkApplication.EventTunnel.Publish(newlinkapplication.someuniquevent(true));
}
私有无效按钮\离开(对象发送者,路由目标e)
{
LinkApplication.EventTunnel.Publish(newlinkapplication.SomeUniqueEvent(false));
}
}
}
命名空间ProjectUser
{
公共部分类主窗口:窗口,LinkApplication.IEventReceiver,INotifyPropertyChanged
{
公共主窗口()
{
初始化组件();
this.WindowStartupLocation=WindowStartupLocation.CenterScreen;//在屏幕中央启动窗口
DataContext=this;
LinkApplication.EventTunnel.Subscribe(此);
}
公共布尔比较单击{get;set;}
公共图书馆点击检查
{
获取{return CompareClick;}
设置
{
如果(值!=比较单击)
{
比较单击=值;
O
        //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;
        }));
    }
 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);
        }
    }
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
}