Home > Software design >  Reference WPF Main window variables from a WCF Callback method
Reference WPF Main window variables from a WCF Callback method

Time:11-01

I'm using a WCF duplex contract which appears to be working correctly, the callback method in the client is called successfully from the service.

How do I call methods or reference objects which are defined in the WPF main window class from within the callback? Since the callbackhandler class is defined outside the main window class the methods are not known. If I move the callbackhandler class inside the main window class then the main window class methods are known but a compiler error "An object reference is required for the nonstatic field, method, or property 'member'" is created.

Thanks

I tried using Invoke() to call the main window methods (when the callback was moved inside the main window class) but still I get the error "An object reference.."

CodePudding user response:

You need to get a reference to the window one way or another. You could for example use the static Application.Current.Windows property:

var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
  • Related