WCF, Accessing a windows forms controls from a service

后端 未结 5 806
南旧
南旧 2021-01-05 22:17

I have a WCF service that is hosted inside a Windows Form.

How can I access the controls of the form from the methods in my service?

for example I have

5条回答
  •  自闭症患者
    2021-01-05 23:00

    The service will need a reference to the instance of the form, not just the type of the form.

    Further you will need a way to send values to the form controls from your service class. This can either be done by making the Controls themselves public or protected, or you can create properties on your form class that will set the properties on the Controls.

    A simple approach would be the following:

    1. Add a constructor to your service class that takes a reference to your form instance. If you are hosting your service inside the form this should be easy.
    2. Add public properties for the control properties you want to set from the service. The properties should be public or internal and will set the values of the controls properties.
    3. In your service you set the values of the newly added properties.

提交回复
热议问题