To my understanding, binding errors are displayed only during debug mode in Visual Studio's Output window. However, I want to know about broken bindings when the user runs my app, and I want to notify him that something is not working quite right.
Is there a way to handle binding exceptions from code, when the datacontext is set, and some bindings are broken?
It's possible to catch all Silverlight binding exceptions in all property setters. See here.
Related
My application is working fine, all of a sudden while loading a designer in wpf form, im getting errors.
Object reference not set to instance of an object.
As if it is in a loop. After pressing the enter button for sometime. Im getting this error.
Microsoft Visual Studio XAML UI Designer has stopped working.
And after this, im getting this.
System.Runtime.Remoting.RemotingException
[9980] Designer process terminated unexpectedly!
The number in square brackets is changing evrytime.
Im using Visual Studio 2012. Im not yet running the application, Im just switching to designer mode from code behind. If I compile and run the application, it runs fine.
Kindly help.
Edit:
Here are the three errors in single image. (I cant post morethan one link)
xamlerrors
There's an error either in the code-behind or DataContext (if you're using a ViewModel). After you fix that error, click on Click here to reload the designer. What's happening is that the designer is trying to load everything up during design time and since there's an error, it's unable to, as if the program was actually running. That null reference exception can be a bit misleading in terms of trying to figure out the cause, because it'll be thrown if you have an error in your code-behind or ViewModel.
Things to check:
Is your View referencing the correct ViewModel?
Is your code-behind portion of the View matching the View name? Some people copy their Views, but forget to change the Class name in the code-behind.
Do you have any errors in your ViewModel?
What about models? If they're loaded during design time and contain errors, this could cause the above exception.
I noticed that those kind of errors are almost everytime caused by
an error in the parameterless constructor of the view
an error in the constructor of the view model which is attached to the view
Keep in mind that when the designer loads, it calls the parameterless constructor of the view (there has to be one for the designer!). If there are "complex things" done there which can only performed correctly at runtime, there will likely be errors at design time.
The same is true for the constructor of the view model which is called from the view.
Check whether design mode is active
For example you should not load data from a repository in a constructor.
If you do it in the constructor, at least check whether the design mode is active or not like explained in this answer here about the GetIsInDesignMode attached property and only do the complex logic in the constructor if the design mode is not active.
Debugging the problem
You also have the possiblity to debug the problem. In order to do this, you have to open a second instance of Visual Studio and debug the designer process of your original Visual Studio instance from there.
The process is described in detail here: Debugging an Exception (only) occurring in design view
Why is it that every time I open a user control or anything a little more complicated than simple markup, Visual studio is unable to display it in the designer? Sometimes I have a form where there are several elements, but all the designer displays is a rectangle? How does one work on more complicated pages with XAML?
If you use Binding in XAML, VS designer invokes the binded object as a compiled manner like part of your program being executed. At this time, if the binded object does not prepare any fake data programatically which gives null in general, VS designer is failed to render UI because it is treated as NullReferenceException during rendering process.
Those fake data is called design-time data in XAML.
To prepare design-time fake data for UI, please check this link: https://stackoverflow.com/a/11561369/361100
In my experience, Visual Studio's designer is almost completely useless for anything more complicated than the simplest designs. Have you tried using Blend? It actually works (most of the time.)
What techniques are there for debugging issues with data binding in a Windows Metro style application? Are there techniques available like those for WPF and Silverlight applications, described at:
How can I debug WPF bindings?
Debugging Data Bindings in a WPF or Silverlight Application
How can I turn binding errors into runtime exceptions?
EDIT: I was originally asking about WinRT data binding debugging techniques so that I could troubleshoot the issue described at Metro: Why is binding from XAML to a property defined in code-behind not working?. I eventually found a solution to this issue, but experimenting with the working solution, I did not see any message in the Visual Studio 11 output window when I purposely misspelled the property name so that it would not be found. It also does not appear that PresentationTraceSources is available to WinRT apps.
Another possible solution:
sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
DebugSettings.BindingFailed += OnDebugSettingsOnBindingFailed;
}
private void OnDebugSettingsOnBindingFailed(object sender, BindingFailedEventArgs args)
{
new MessageDialog(args.Message).ShowAsync();
}
...
}
Original source: http://www.tozon.info/blog/post/2012/07/23/Debugging-WinRTXAML-bindings.aspx
If you look at the output window in VS you will see data binding trace messages on errors. You get this automatically for C++ applications and for managed applications you have to turn on unmanaged debugging to see them. This is an area we are looking to improve on, but for now you have the ability to turn them on and see the trace outputs.
In VS11 beta, the templated projects offer a way to help debug binding errors.
I wrote it up here http://www.kelvinhammered.com/?p=150
I always use immediate window to track binding issues.
Here's what msdn says about it :
In some settings configurations, first-chance exception notifications
are displayed in the Immediate window.
To toggle first-chance exception notifications in the Immediate window
On the View menu, click Other Windows, and click Output.
Right-click on the text area of the Output window, and select or
deselect Exception Messages.
(in fact default setting was ok for me in vs2010)
hope this can help.
I'm using the binding RelativeSource with the FindAncestor Mode but the binding is not working. How do I debug and see if It is able to find the ancestor?
use Snoop
EDIT: you can of course use the usual debugging mechanisms, but I like Snoop the best. You can navigate to your control and if your binding failed it tells you so
Or you can set PresentationTraceSources.TraceLevel on the binding.
If you are using VS2010 remember to set the Data binding value in Options->Debugging->Output Window
I use WPF Inspector, it's a nice free tools for debugging WPF application in XAML level.
I want to bind Validation.HasError to a boolean property on my viewmodel. I only want to know when there is a validation error so that I can disable my buttons on my VM using canexecute methods from my relay commands. I get this error though:
'Validation.HasError' property is read-only and cannot be set from markup.
I have tried different modes and they all cause this error. How do I do this? It shouldnt be this difficult.
You can't set a Binding on a read-only dependency property. This is a known bug at Microsoft Connect. You might vote for it.
The BookLibrary sample application of the WPF Application Framework (WAF) shows how to listen to the Validation.HasError property and disable some buttons.