locked
Connect function not being called for user control

    Question

  • I have a user control that I'm working on and I discovered that none of the events that I've defined for it are being triggered. The one thing I discovered is that the Connect function, which is generated in the hpp file, is not being called. So, none of the event handlers are being registered. Why is this happening? The constructor of the control is being called and InitializeComponent is being called as well as LoadComponent. None of it's initialization code in the Connect function to bind it to the XAML definition is being invoked.

    I have other controls in this solution that work and react to events as they should. But for some reason this new control that I've created is exhibiting the above weird behavior.

    I'm using VS 2012 with update 3. I tried creating another simple user control in the same project with nothing more than a handler for PointerPressed and the same bug is being exhibited in this control too. This is a complete show stopper for me.


    Saturday, August 3, 2013 10:20 PM

Answers

  • I believe I have found the root cause of the problem.

    Every since I started work on this control library I have always had to add post build event steps that copies the XAML file for a control from $(OutputPath) to $(OutputPath)\<library namespace>. For example:

    copy $(OutputPath)\MyControl.xaml $(OutputPath)\MyControls

    Otherwise I would get an error like the following when building my solution:

    C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\AppxPackage\Microsoft.AppXPackage.Targets(926,9): error APPX0702: Payload file 'E:\Projects\Controls\MyControls\Debug\ MyControls \ MyControls \MyControl.xaml' does not exist.

    I've never understood why I have had to configure this post build step. My expectation is that this should happen automatically.

    Now the problem is that for the latest control in my library I added the file copy step for the post build event but I mistakenly copied the file from $(ProjectDir) instead of $(OutputPath). Looking at the differences between the XAML files in the two directories I discovered that the files in $(OutputPath) have the declarations for the event handlers removed. Figuring that this must have something to do with runtime initialization of the control, I changed the copy command to source from $(OutputPath).

    Problem solved! The Connect function is now being called and the automatic binding of the event handlers is working.

    I still don't understand why I need to configure the post build steps to copy the XAML files and the reason for the XAML file in the $(OutputPath) directory having it's event handlers removed. But I'll leave that as a question to solve another day.


    Friday, August 9, 2013 2:30 PM

All replies

  • HiJim Young

    Could you share the code with me so that i can continue work on this issue. And let us clarify the question together.

    You put the another simple user control which get the same bug is created by yourself? Have you tried a button? If you have tried a button and the issue is also exist, i think it maybe the UIElement tree design issue, like your behaviour triggers it's parent event instead of its, for example, you defined a Tunneling event, but you modify the handled as true in root, therefore, your event which defines on child element can't be triggered.

    If the issue is gone after you replace your control with button, i think the problem is in your control's creation. I'll try my best if you share your code here.

    Best Regard!

    Monday, August 5, 2013 12:14 PM
    Moderator
  • I replaced my control with a button and button events trigger like they should.

    I can trace the initialization of the control to the call of  Windows.UI.Xaml.dll!DirectUI::ApplicationFactory::LoadComponentWithResourceLocation() which should call the Connect function, but it does not. I've created another very simple control in the same library and it is exhibiting the same behavior.

    I made a separate solution with a control library of one control and a simple test page. This solution does not exhibit the behavior I've described.

    I have the project that displays the problem uploaded to Skydrive. How can I share this with you privately? Like I said, I'm really dead in the water until I can get this resolved.



    Monday, August 5, 2013 1:19 PM
  • Hi Jim,

    You can post the SkyDrive link here. The forums are community based help. If you need individual help please open a case at http://getsupport.microsoft.com

    --Rob

    Tuesday, August 6, 2013 2:06 PM
    Owner
  • Understood. I've decided to burn one of my support cases.
    Tuesday, August 6, 2013 3:54 PM
  • Sorry Jim: I looked at the last response about providing the repro without reading your actual question. Before you create a case take a look at this similar thread.

    --Rob

    Tuesday, August 6, 2013 9:51 PM
    Owner
  • Thank for the reply, Rob.

    I read the thread but I think it is a different use case. The one take away from the thread is that, yes, I can late bind my event handlers in code. But, that really defeats the purpose of XAML designing, especially given the verbosity of C++. And it does not address the problem that the auto-generated code that is supposed to do the binding is not being invoked.

    I explicitly bound my handlers in the constructor and they do get called now, but I don't consider it an optimal workaround.

    In any case I've created a case with support.

    Wednesday, August 7, 2013 2:09 AM
  • Hi, Jim Young

    I think you bound your handlers in the constructor(complete initialization in the compile phase) is the same as you set them in xaml. And it's not the late bind. The late bind is completed in runtime and it could always find the relevant value automatically.

    We can't give you a deterministic solution without your code which reflects your questions. I recommend you could paste the part of code or just a image which could reflects your questions as well.

    Best Regard!

    Friday, August 9, 2013 6:08 AM
    Moderator
  • I believe I have found the root cause of the problem.

    Every since I started work on this control library I have always had to add post build event steps that copies the XAML file for a control from $(OutputPath) to $(OutputPath)\<library namespace>. For example:

    copy $(OutputPath)\MyControl.xaml $(OutputPath)\MyControls

    Otherwise I would get an error like the following when building my solution:

    C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\AppxPackage\Microsoft.AppXPackage.Targets(926,9): error APPX0702: Payload file 'E:\Projects\Controls\MyControls\Debug\ MyControls \ MyControls \MyControl.xaml' does not exist.

    I've never understood why I have had to configure this post build step. My expectation is that this should happen automatically.

    Now the problem is that for the latest control in my library I added the file copy step for the post build event but I mistakenly copied the file from $(ProjectDir) instead of $(OutputPath). Looking at the differences between the XAML files in the two directories I discovered that the files in $(OutputPath) have the declarations for the event handlers removed. Figuring that this must have something to do with runtime initialization of the control, I changed the copy command to source from $(OutputPath).

    Problem solved! The Connect function is now being called and the automatic binding of the event handlers is working.

    I still don't understand why I need to configure the post build steps to copy the XAML files and the reason for the XAML file in the $(OutputPath) directory having it's event handlers removed. But I'll leave that as a question to solve another day.


    Friday, August 9, 2013 2:30 PM