简体   繁体   English

在WPF中托管WCF,我如何在wcf中更改MainWindow UI中的控件?

[英]WCF hosted in WPF and how can i change control in MainWindow UI from wcf?

I write WCF code and hosted in my WPF app. 我编写WCF代码并托管在我的WPF应用程序中。 I write class to switch my MainWindow to show other page in my project 我编写类来切换我的MainWindow以显示我的项目中的其他页面

public static class Switcher
    {
        public static MainWindow pageSwitcher;

        public static void Switch(Page newPage)
        {
            pageSwitcher.Navigate(newPage);
        }       
    }

and I write my wcf service like this: 我写这样的wcf服务:

[ServiceContract]
    public interface IAppManager
    {
        [OperationContract]
        void DoWork();
        [OperationContract]
        void Page1();
        [OperationContract]
        void Page2();
    }
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class AppManager : IAppManager
    {

        public void DoWork()
        {
        }
        public void Page1()
        {
            MainWindow.pageSwitcher = new MainWindow();
            MainWindow.Switch(new Page1());
        }
        public void Page2()
        {
            MainWindow.pageSwitcher = new MainWindow();
            MainWindow.Switch(new Page2());
        }
    }

I want change page remotely from another computer with WCF but it not work and I trace code the wcf is run and response but do not do anything how can access to main thread to change ui page or other element? 我希望从另一台使用WCF的计算机远程更改页面,但它不起作用,我跟踪代码运行wcf和响应但是没有做任何事情如何访问主线程来更改ui页面或其他元素?

I suggest you start over and this time separate the WCF from your WPF app. 我建议你重新开始,这次将WCF与你的WPF应用程序分开。

You need to: 你需要:

1) Separate WCF from WPF - should be in different layers. 1)从WPF中分离WCF - 应该在不同的层中。

2) Use WCF with duplex binding - this way your WCF service will be able to communicate with clients when it needs to. 2)使用WCF与双工绑定 - 这样您的WCF服务将能够在需要时与客户端通信。

3) In your WCF callback contract (implemented by the client) - you should prepare a method that will be used to change the local UI mode. 3)在您的WCF回调契约中(由客户端实现) - 您应该准备一个用于更改本地UI模式的方法。

Duplex binding is the perfect solution for your needs. 双面装订是满足您需求的完美解决方案。

You can reed about Duplex here 你可以在这里了解Duplex

Hope I helped! 希望我帮忙! Eking. EKING。

Your current solution is unusual, but WCF can be hosted in a WPF application. 您当前的解决方案不常见,但WCF可以托管在WPF应用程序中。 However you should never try to directly manipulate the UI from the WCF service - you'll have cross thread issues to begin with. 但是,您永远不应该尝试从WCF服务直接操作UI - 您将首先遇到跨线程问题。

What you should consider doing is using messaging via a pub-sub broker (image linked from MSDN): 你应该考虑做的是通过pub-sub代理使用消息传递(从MSDN链接的图像):

在此输入图像描述

Something that fits this bill nicely is Prism's EventAggregator . Prism的EventAggregator很适合这个法案。 A couple of samples I cherry picked are Simplifying PRISM's EventAggregator and Prism EventAggregator Sample . 我挑选的几个样本是Simplifying PRISM的EventAggregatorPrism EventAggregator Sample

The way you use this is the service registers events and then raises them, the WPF subscribes to those events and processes them. 您使用它的方式是服务注册事件然后引发它们,WPF订阅这些事件并处理它们。 With this you can also specify whether to receive the events on the UI thread or a background thread . 使用此功能,您还可以指定是在UI线程还是后台线程上接收事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM