I'm trying to create a custom control using UserControl. When I drop the custom control on a window, it displays for a second then the designer crashes and I get the messag:
An Exception was thrown
ArgumentNullException: Value cannot be null.
Parameter name: sp
The stack trace shows an error in a call the ServiceProvider constructor.
Any idea what's going on on here? I tried this with a blank UserControl on a blank Window and got the same error.
Thanks for your help.
XAML designer will call the UserControl's constructor when loading in designer.
If in the constructor or UserControl.Loaded is another Method that is not running in the design mode should be skip.
In order to avoid this you can place a if condition as follows in your UserControl constructor
if(DesignerProperties.GetIsInDesignMode(this))
return;
// another Method that Running in RunTime
WPF user control throws design-time exception
After further googling the issue, it seems that this is related to the presence of installshield projects in the solution. If I remove all installshield projects, I don't get the exception anymore.
This is more of a workaround than a solution though...
Related
Is there a way to see which UserControl/Window/etc is being instantiated when these errors are happening?
A typical error looks like this:
System.Windows.Data Error: 40 : BindingExpression path error: 'BackgroundColor' property not found on 'object' ''MapContainerViewModel' (HashCode=25350572)'. BindingExpression:Path=BackgroundColor; DataItem='MapContainerViewModel' (HashCode=25350572); target element is 'SolidColorBrush' (HashCode=35109313); target property is 'Color' (type 'Color')
Problem? Where is it happening? In which XAML file? In which class?
Doesn't it seem bizarre that you get a list of symptoms, but not the patient's name?
In a small project I would probably know where to look. I'm currently refactoring an MVVM project with hundreds of Windows/UserControls, many of which have similar looking Binding Paths.
So I have to do an "Entire Solution" search for the property names and come up with a list of candidates (UserControls/Windows) that may have caused the Binding error when they were instantiated.
Another option would be to have Visual Studio break and show me the XAML as soon as a System.Windows.Data error occurs. Following tutorials like this one, I can get the code to break--but it doesn't indicate any XAML or class name. The stack trace shows nothing--as the controls are being automatically created via MEF.
Thanks for any help.
Chad.
UPDATED:
Live Visual Tree doesn't work in this case, because the UserControls/Windows in question are sitting in an MEF container and aren't attached.
Any visual approach won't work because the views/datacontexts (view models, in this case) were instantiated via MEF (DI pattern) and are waiting to be added to the visual tree.
The debugger is not going to help you very much with XAML. This is because XAML is declarative while the debugger is designed for imperative code. For WPF you need to rely on a different tool set, specifically the Live Visual Tree and the Live Property Explorer in VS2017.
Here is a simple WPF app with a few textbox controls.
One of the text box controls is not working. How do I find it? First open the Live visual tree. Click the enable selection button in the WPF tool bar (the second button). Then select the control which is not working. The live visual tree will select the control.
Now select the TextBox parent control in the Live Visual Tree (FirstValueField). Now open the Live Property Explorer.
Note the yellow box around the Text property. This is where the error is. Click to the right of the property field and select "Go To Source".
So here is where the error is. You can see where I changed the code to introduce the error.
This is the general approach to debugging WPF, XAML or any declarative code: (1) give up on the debugger...it will simply get in the way; (2) learn how to use the fit-for-purpose tools to find errors; (3) if that doesn't work, use print statements.
In my experience the WPF tools are excellent for finding common errors. The more complex your WPF becomes however, the more you will need to embed code to diagnose and identify the problem.
I've read a lot of questions on SOF and links (for example http://www.beacosta.com/blog/?p=52)
But is there easy way to know what exact file I should look into when Binding fails?
If we have one Application and a lot of forms, it can be difficult too.
Have you tried checking "Thrown" for the exception in question in the Debug->Exceptions menu.
E.g. if you get
System.Windows.Data Error: 35 : BindingExpression path error ...
Then you can tell the debugger to break on it by checking "Thrown" under Common Language Runtime Exceptions -> System.Data -> System.DataException. This is however, only useful if the exception originally occurs in your code. Other exceptions, such as binding to non-existing properties and so on will silently fail and only print in the Output window. There is some discussion on http://visualstudio.uservoice.com for improving XAML debugging
I just found the most WONDERFUL post when looking for this. It is a listener that listens for binding errors and throws up a message box with details. It only works when running from within Visual Studio, so you won't show it to your users. Two steps - copy the class into your project, and set the listener in your main window.
http://tech.pro/tutorial/940/wpf-snippet-detecting-binding-errors
You can use Snoop for this: http://snoopwpf.codeplex.com/
Just use Snoop to point to your application and then you can sort on Binding Errors. All Bindings Errors will be highlighted in RED, and show the property of the control.
When i try to reload designer it gives me this error, what it means?
The designer try to execute the current xaml to render it on your editor. So if you have an issue in your xaml, or in your xaml.cs constructor code (and all code referenced in this constructor), this issue will occur until you resolve the problem.
I'm using WAF (Wpf Application Framework) to create a dialog as shown in the ModelView sample application. I am trying to put up a simple AboutBox by mirroring the code for putting up the CreateEmailAccountWizard dialog box. My About box shows up fine the first time, but when I call it again from the menu, it gives me the following exception:
Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.
First, I don't know what this message means. Second, what am I doing wrong? The sample application doesn't throw this exception when you close and reopen the CreateEmailAccountWizard dialog box. My code is nearly identical to it. Any help would be greatly appreciated.
Never mind. Found the source of the problem. I was creating a singleton from MEF and it was causing the same dialog instance to run twice. I solved the issue by doing the following:
var shellView = _container.GetExportedValue<IShellView>();
_aboutDialogViewModel = _container.GetExportedValue<AboutDialogViewModel>();
_aboutDialogViewModel.ShowDialog(shellView);
I also had to set the MEF attribute on the class to tell it to not use a singleton:
[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class AboutDialogViewModel : ViewModel<IDialogView>
{
[ImportingConstructor]
public AboutDialogViewModel(IDialogView view) : base(view)
I've created a custom control (a class that inherits from Control). When I put it on a Form I can work with it on Visual Studio IDE. It shows me an error and I don't see the form.
The error message is this: La variable 'ctrlImagen' no está declarada o no se asignó nunca.
It's a winform for a Compact Framework app.
How can I solve this? (it the class inherits from UserControl it works perfectly)
Sounds crazy, but Visual Studio is selectively executing code for your control in the designer. First thing to check is your constructors. Make sure you have an empty, default constructor, that is public, even if you never plan on using it. After that make sure any code you have tied to layout events (such as resize) are good to go, these are likely the culprits, as thats where I always find problems when my custom controls don't work in the designer.
It sounds like you have a bug in the code for your Control. This sounds like a runtime error that is preventing the control from rendering.
Re-read your code and look for potential null-pointer exceptions, unassigned variables, stack overflows, etc. The bug is lying in their somewhere.
Any chance your user control doesn't have a public default constructor? I can get a similar error "The variable 'userControlX' is either undeclared or was never assigned' if the constructor isn't public.
Would need more info, though.
It seems a design time exception has happened in initialization logic that gets executed before your form initialization logic executed. A good example would be a NullPointerException fired by the default constructor (or one or more methods it invokes) of a user control that contained by your form.
I put this on constructor to solve the problem:
this.ClientSize = new Size(21, 21);
The beging of my class is this:
public class ControlMapa : Control
{
public ControlMapa()
{
this.ClientSize = new Size(21, 21);
...
Thank you!