Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have this problem that My WPF App in its MainWindow.
I am trying to call the below wrapevent in my window.

__browseInput.Click += MainModule.WrapEvent(__browse_Click).EventHandler;

And i am implementing the wrapping functionality in another class.This class is dependent for class for my window.The code is below

public static EventInvoker WrapEvent(EventHandler handler) where TEventArgs : EventArgs
{
if (_eventWrapper == null)
{
_eventWrapper = new EventWrapper();
_eventWrapper.CaughtException += ShowUnhandledException;
}
if (_eventWrapper.Sync == null && _mainForm != null)
{
_eventWrapper.Sync =(ISynchronizeInvoke) _mainForm;
}
return _eventWrapper.Wrap(handler);
}

In this _eventWrapper.Sync =(ISynchronizeInvoke) _mainForm; i am getting the error.
Could you please provide solution to me.

What I have tried:

I was tried using
Dispatcher.BeginInvoke
,But it's not working.
Posted
Updated 8-Aug-19 6:19am
Comments
jimmson 8-Aug-19 3:57am    
What does the error says?
Ram349 8-Aug-19 4:31am    
Hi Jimmson,
Thanks for responding to my post.I am getting the error is Unable to cast object of type'mainwindow to type system.componentmodel.isynchronizeinvoke
Ram349 8-Aug-19 4:33am    
Actually i am converting winforms application into WPF

You cannot randomly cast objects of different types. See ISynchronizeInvoke Interface (System.ComponentModel) | Microsoft Docs[^].
 
Share this answer
 
Comments
Ram349 8-Aug-19 4:48am    
Hi Richard,
Thanks for responding my post.In this line _eventWrapper.Sync =(ISynchronizeInvoke) _mainForm; for casting only i added ISynchronizeInvoke.
Could you please suggest how to can i proceed.
Richard MacCutchan 8-Aug-19 5:44am    
Proceed with what? As I already told you, you cannot randomly cast one type to another.
ISynchronizeInvoke is for WinForms, not for WPF. You need to use a bit different approach.

You can find more information on this here:

winforms - WPF window doesn't implement ISynchronizeInvoke.InvokeRequired ERROR - Stack Overflow[^]
c# - How to Use ISynchronizeInvoke interface? - Stack Overflow[^]
 
Share this answer
 
If you want to write code that will work in both Windows Forms and WPF, ignore the ISynchronizeInvoke interface, and use the SynchronizationContext instead.

SynchronizationContext Class (System.Threading) | Microsoft Docs[^]
MSDN Magazine: Parallel Computing - It's All About the SynchronizationContext[^]
Understanding SynchronizationContext (Part I)[^]
 
Share this answer
 
Review the code "where used".

The code was perhaps over-engineered; or, is not even being used.

A simple public Action<> or Func<> property that can be "attached to" is often all that is needed ... and a lot more understandable.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900