[英]How to communicate between UserControl in MVVM - WPF Application
[英]communicate between the instances of an application
我有一个winforms应用程序,
在关闭按钮单击,我将该应用程序隐藏到系统托盘中,就像Skype的工作方式一样。
如果再次实例化应用程序并且该应用程序的实例已经在运行,那么我想将已经运行的应用程序(可能在托盘中)带到前面并退出新应用程序。
我想做的就是在使用WCF的Main
mathod中这样做
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcesses();
bool running = false;
foreach (var process in processes)
{
if (process.ProcessName == currentProcess.ProcessName
&& process.Id != currentProcess.Id)
{
running = true;
}
}
if (running)
{
ChannelFactory<IService1> pipeFactory = new
ChannelFactory<IService1>(new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/PipeActivate"));
IService1 pipeProxy = pipeFactory.CreateChannel();
pipeProxy.ActivateThisWindow();
Application.Exit();
}
else
{
using (ServiceHost host = new
ServiceHost(typeof(Form1),
new Uri[] { new Uri("net.pipe://localhost") }))
{
host.AddServiceEndpoint(typeof(IService1),
new NetNamedPipeBinding(), "PipeActivate");
host.Open();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
host.Close();
}
}
哪里
void IService1.ActivateThisWindow()
{
this.Show();
if (this.WindowState != FormWindowState.Normal)
this.WindowState = FormWindowState.Normal;
this.Activate();
}
现在的问题是,它将运行的实例带到前面,但是当新实例退出时,它将进入其先前的状态。
问题是什么? 我怎么解决这个问题?
我可以用什么方法来达到这个要求?
虽然这是一种非常新颖的方法,但它有点矫枉过正。 如此处所示,有一种更容易和更广泛使用的方法来处理这个问题。
从个人经验来看,我将这个资源用于我的所有单实例应用程序,它就像一个魅力。
我假设你已经在Form 1类中实现了IService1.ActivateThisWindow,所以这种行为是因为在主机app上为每个请求创建了form1的新实例,并在请求结束时被销毁。 要在单独的类中解决问题因素IService1.ActivateThisWindow,因此form1不是主机对象并使form1单例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.