Debugging Data Binding in Winforms C#? - c#

I'm getting alot of issues lately in a project im working on with databinding. When im about to display a certain form that has bindings inside its controls, the .Show method throws expcetions regarding the binding - "Cannot bind to property or column on datasource. Parameter name: dataMember.".
Now, I'm quite aware this is a rather generic error message, but i can't find a decent way to get any info on which binding failed, which control, which data source/member and so on.
Is there a good way to find this?

I guess some general debugging tips will help:
When debugging, is there an inner exception with more information?
Another tip is to turn on 'Show external code' in the stack trace window. This will allow you to go further down into the stack to the .Net Framework code itself. Turn on your locals window for information on the content of the current context, this might lead you to the problem.
Also be sure to turn on 'Break on exception' for all CLR exceptions.
Good luck.

Well, I'm stumped. One of the controls in the form is a UserControl i made, Which contains some bindings in it.
This used to work fine. Now, I've added inhertience from IEnumerable to my control so it can return certain objects inside it for a different feature i tried adding. Now, If i 'Step-Into' the .Show call, I reach the IEnumrable implementation i made in a certain case.
Although it returned an empty collection at this time, since i had no elements to return, That apparently caused the ArgumentException that i got with the DataBinding for some reason. The moment i remove the IEnumrable inheritence from my control, Everything is back to normal.
I'm completely clueless as to why this happened.

Related

Prism for Xamarin.Forms unable to AutoWire

I am trying out Prism.Forms for my next client project. Unfortunately, I am seeing a strange behavior with my samples.
Pretext
It seems like XF or Prism does not like me including an existing XAML page (also mentioned by Brian Lagunas - The man - himself at the Evolve session). But renaming a page also throws it off and the ViewModelLocator can't seem to locate the ViewModel anymore. Even if I re-create the page with same name, It can't find it ViewModel. Manually setting the BindingContext works, but I am trying not to create my dependencies for constructor injection.
Question
While working on existing code, a minor change caused NullReference exception when using NavigationPage as a root. Here is an example of a working sample app
https://github.com/hnabbasi/xamarin/tree/master/XFPrism/XFPrism
I am using a NavigationPage and pushing a ContentPage. Then I push another ContentPage, then I show a Modal page via button click and communicate. I send parameters back to Second content page via NavigationParameters.
To break it, simply try to swap the ISayHello service with IPageDialogService. I am not sure what's going on that's breaking it.
Thanks in advance :)
The INavigationService is a named type and as I recall a known limitation of Unity is that named types must precede unnamed types.
As mentioned by Dan S. in the comments. The MainPage is not set when I was calling the PageDialogService to display the alert, hence the NullReference.
In case you really need to do something like this where you want to somehow wait for something to finish before doing an operation, try Task.Yield().
I fixed my scenario as below,
// will yield for current operation of page appearing
await Task.Yield();
// by now, the page has appeared and set, so this should work fine
SayHiCommand.Execute(this);
I have used this workaround in the past. If you know a better way of handing this, please comment away.

Can I always be sure of the hierarchical order of controls found via FindWindowEx?

What am I doing?
I am automating a 3rd party application. The login window looks like this
Note: there is a background image embed within the form and both the USERNAME and PASSWORD are actually part of an image. Just in case anyone wonders why the below classes treeview from Window Detective doesn't have, possibly ยป Label controls.
Where am I at?
I don't have any problems getting handles of components. I mean, I can generally access each window/control using FindWindowEx function.
I can implement a GetWindowTextLength and/or GetWindowText functions for buttons to distinguish which one I have got a handle of. While this approach works fine with Buttons (.Caption) it would not benefit me when working with Edit controls. It seems that the Edit controls do not have any unique properties to distinguish which one is which. (correct me if I am wrong)...
The problem
Assume that I need to know at the compile stage which Edit control I am going to be handling, so I don't send a password to the username and other way around. There are only two but like I've said I'm not sure how to get the right one.
I wouldn't mind figuring it out at run-time but I am unsure of how to distinguish the difference without sending a message and actually visually identify which one is which...
Current solution
I wouldn't really call this a solution but for now I am relying only on the fact that each time I run my code I always get a handle of the second (password) Edit control as the first handle returned.
Question
Can I be 100% sure the second Edit control will always be returned first in the hierarchy returned by the FindWindowEx function?
If someone can confirm my idea than I'd already have had a solution BUT if I can't always expect the second Edit control to be returned then I would like to hear some insight on how to handle this situation.
Note: I didn't think any code is actually required for my question but if anyone would like to see the code then please leave a comment and I will add it to the question.
Thanks a lot for your time.
Each child HWND has a property called ID. Use GetWindowLongPtr( hWnd, GWLP_ID ) to get it. Depending on how the target application is coded, that ID may be a way to distinguish between child control. Knowing the ID you can get the child HWND with the GetDlgItem API (works fine when the parent is not a DialogBox, the API should have been called GetChildByID).
Beware: some target applications really use random/unreliable values for IDs.
It seems that there is a general consensus: Enumeration APIs rely on the Z-Order. See for example that SO answer (maybe making your question some sort of 'duplicate')
While some applications may "play" with the Z-Order of their child windows, in the standard case they won't, which means that the first created child window is at the top of the Z-order, and the last created one is at the bottom.

Delete control from form

I've currently got custom control that has somehow lost its parent and is now not parented to anything but it's still in the list of controls in the form designer. The delete button also doesn't work and is thus disabled. This happens every now and again and its a pain to go through the designer code and remove manually, plus there are other developers that this will annoy and may confuse them.
I'm therefore trying to add a Verb within the controls ComponentDesigner to delete itself from the form. But I realised that because its not 'childed' to anything, it therefore cant be removed as a child. How would I therefore go about deleting a control from the form designer via code?
This can happen when one of your controls throws an exception at design time. That's rarely a silent event, the designer shows a popup message box. Not getting a message box may happen when you swallow exceptions in your code with a try/catch.
Trying to fix this by hacking a designer just adds to the problem. Fix it by editing the designer code, it is okay when you know what you're doing. If you can't find the reason then get it to a point where you can make it somewhat reproducible. Then start another instance of Visual Studio, Tools + Attach to Process and select the first instance. Debug + Exceptions, tick the Thrown box for CLR exceptions so the debugger will stop when the exception is thrown.
Back up the file.
Open up the designer file; e.g., Form1.Designer.cs
Expand this region: Windows Form Designer generated code
You should be able to find your control in the code and delete it. Be careful.
I solved the issue by finding the loose controls within the Document Outline tab. This way is super easy and is graphical.

WPF AutoCompleteBox Data Virtualization

I am trying to implement Data Virtualization on a WPF AutoCompleteBox.
I found Bea Stollnitz's code here which works great on a ListView and I made it to work on an ComboBox easily, but there's no way I can seem to get it working right on an AutoCompleteBox.
To be precise, it works - the list is virtualized, items are not all loaded on startup - but what seems to happen is that the AutoCompleteBox iterates through all the items in the list as soon as the ItemsSource changes and this ends up breaking the whole thing (i.e. on load, all the pages in the list are requested, so even though they are virtualized, they will all be requested in the beginning and get loaded). My guess is that this is because of the filtering that the ACBox does, but I'm surprised that there's not way to prevent it, since the control does normally allow filtering to be done on the server-side using the Populating event.
I feel that I'm missing something, I can't believe that no one has done something like this before or that it can't be done, so I'm guessing that I'm just doing something obviously wrong that I can't figure out since I'm new to WPF.
Here's some of the things that I've done to try and get this together (based on Internet searches around similar problems):
I made sure all the conditions for the built-in UI virtualization are ok including explicitly turning it on, setting the max height of the ListBox etc.
I replaced the ListBox in the AutoCompleteBox with a ListView like the one that Bea uses in her example. Side by side, the ListView by itself works as expected, but the one embedded in the ACBox does not.
I tried using no filtering in the ACBox, using a custom filter, handling the populating evenet manually etc. This doesn't help. Making a custom filter is obviously not enough since the custom filter only allows you to specify the result of evaluating one item, the code that loops through the list is not visible so you can't prevent looping. Turning the filter to "None" doesn't do anything either.
Any and all suggestions are welcome!
The target for this is .NET 3.5 and I am using the WPF Toolkit (Feb 2010 release)
Thank you!
I tracked this down to OnItemsSourceChanged() (see source). In there, the AutocompleteBox stores a "local cached copy of the data", which is why I see the behavior noted above.
This is a private method, so no overriding here.
It seems to me that because of this you can't apply DataVirtualization to the AutoCompleteBox, at least not using the ideas in Bea's solution. If anyone has any different thoughts regarding this, I would love to try it out, but until then, this is what I believe the answer to be.

Setting the Site Property of TFrame

I am attempting to create a CustomControl in C# which contains a Delphi TFrame, which can be dragged from the VisualStudio toolbox onto a Form. This much I have working correctly, however when I attempt to resize the control errors occur due to the TFrame not running in design mode (it attempts to access the event handlers for a data grid on the frame).
I have created a TFrame object in Delphi which contains various controls (data grid, combobox etc), and a CustomControl in C# which contains the TFrame object.
To get around this problem I think I need to set the Site property of the TFrame object, so that DesignMode is true. How can I go about doing this?
I know I can get around this problem by checking at the start of each method if the LicenseManager.UsageMode is set to DesignTime, but this seems a very bad way of solving the problem. So if possible I would like to get the site property set correctly.
I am using CodeGear RAD Studio 2007 and Visual Studio 2008.
Thanks in advance
Turns out this problem is caused by a bug within Visual Studio where the DesignMode and Site property never gets set on components created within a constructor or a UserControl. Therefore the DesignMode property is always false.
There are several proposed solutions all of them involve adding a test to any event handlers or methods where code should only be executed at runtime. The first option is to check the LicenseManager.UsageMode property, and the second option is to check the name of the executing process (e.g. if its "devenv" then its design time).

Categories

Resources