Notifications
Clear all

[Closed] Event Handler via WPF Button, hosted in WinForm?

Hello,

I’m not sure if the title explains it well enough, but it’s basically what I’m trying to do.

I’ve found that in order to get WPF to work in Max from a .dll, I host my WPF controls, ect in a WinForm in an Elementhost. Normally I would create a public eventhandler in C#, and then in max I would do,

fn listenerTest  = 
 (
 	print "wee"
 )
 
 dotnet.loadassembly ((pathConfig.GetDir #Maxroot)+"\\WPF_MaxUserControl.dll")
 wpf_form = dotNetObject "WPF_MaxUserControl.Form1"
 wpf_form.show()
 dotNet.addEventHandler wpf_form  "wpf_Test_Event" listenerTest 

However, with WPF this doesn’t work. Which I kinda of figured, because my EventHandler right now seems to have to be in the WPF section, and not in the WinForm section, where max can access it.
I tried do make another dotnetObject in Max, and call the WPF userControl for the Event, but that didn’t work either.

Anyone have any experience with this or know how to get max to detect the button presses, ect?

Thanks!

4 Replies

why are you not adding the event directly to the control?

also it may be that you forgot to write it for your posted example but your function is wrong. it must accept 1 or 2 arguments: either sender and event_args or just event_args

Hm… ok, I will try that.

I didn’t write it because I never had to do that for my C# functions. Although, it may have helped me in some circumstances. But it worked just fine doing it that way otherwise.

Edit: Hmm, that didn’t seem to work… In C# I have:

public partial class UserControl1 : UserControl
	{
		public event EventHandler wpf_Test_Event;  //Event Handler for Refresh UI Button

		public UserControl1()
		{
			InitializeComponent();
		}
		 .............

		 private void button1_Click_1(object sender, RoutedEventArgs e)
		{
			MessageBox.Show("wtf");

			if (wpf_Test_Event != null) wpf_Test_Event(this, e);
		}

Then in MaxScript I have

fn listenerTest sender event_args = 
(
	print "wee"
)

dotnet.loadassembly ((pathConfig.GetDir #Maxroot)+"\\WPF_MaxUserControl.dll")
wpf_form = dotNetObject "WPF_MaxUserControl.Form1"
test = dotNetObject "WPF_MaxUserControl.UserControl1"
wpf_form.show()
	
dotNet.addEventHandler test "wpf_Test_Event" listenerTest  --Refresh Thumbnails/UI

I tried to create a DotNetObject of UserControl1.button1, but It won’t let me. Then I also tried just doing event_args in the function, or routedEventArgs, but it’s a no go…

Bumping this. I haven’t touched this since I posted, but Im still curious as to what else I could do to get this working? I’ll have to mess around with it more yet.

Well for anyone who is interested, I hit up James Haywood on the matter, and he suggested trying/messing with ManagedServices, as that’s what he uses.

After including the .Dll to my C# Project, and adding the following code inside a button press function, it can call a Function in Max that I declared before I loading in my UI. Basically doing the same thing that my EventHandler would have done.

  ManagedServices.MaxscriptSDK.ExecuteMaxscriptCommand("wpf_test()");