- Download source — 62.7 KB
Introduction
While developing WPF applications, design view plays an important role not only placing the controls but also we can see the run time view at design time. How does it look like? It becomes frustrating when we see some design time errors and we cannot put a break point in XAML file to diagnose the error, moreover due to this single error sometimes, whole designer fails rendering other controls. So this article enables us to debug the design view of XAML documents in WPF.
Background
It is a pre-requisite that one should be familiar with basic WPF and most importantly one should know how to set design time data context. You can refer to other articles on CodeProject to know how to set a design time data context like this one.
Problem
While designing WPF applications, we frequently see the following types of error in our design view (See pic). Since XAML code does not allow us to insert a break point and debug the stuff, I will share a small trick to trap this error. (References are already there on the internet, but still developers are not so habituated to using it. The reason is that most of them don’t know it.)
- First of all, close all opened XAML documents in Visual Studio.
- Open the new instance of the same application in Visual Studio (say
app2
). - Again, close all opened XAML documents. (To be on the safe side, you may close all documents in
app2
). - Now open Task Manager just to verify whether XDesProc.exe must not be running. (Basically XDesProc.exe is responsible for debugging XAML files, so if any XAML documents are opened, then it launches automatically). So, if you find that XDesProc.exe is running (in Processes Tab), just kill it (right click on process and click on End Process Tree option).
- Now switch to
app1
(original instance of Visual Studio in which you want break point to be hit). Now, open the file containing the design data view model (MainWindowViewModelDesignData.cs) and place a break point in the first line of its constructor. - Switch to
app2
again and open the View (i.e. MainWindow.xaml in which design time error is raising). It will launch the XDesProc.exe in Task Manager. - Switch to
app1
again, and go to Debug -> Attach to Processes… context menu item in Menu bar of Visual Studio. - Search the XDesProc.exe and click on attach button.
- Switch to
app2
and close and reopen the same XAML document (MainWindow.xaml). Once you do it, break point will get hit! - Here, you will find that you have not instantiated the
PersonList
property which is causingNullReference
Exception in WPF Designer. After fixing it, you will see that now the designer is showing the data as well.
Points of Interest
For Visual Studio 2010 users, you will find devenv.exe in place of XDesProc.exe. Also, you can tweak with the exception settings if your break point does not becomes active (i.e., Go to Debug -> Exceptions settings -> Common Language RunTime Exceptions).
This member has not yet provided a Biography. Assume it’s interesting and varied, and probably something to do with programming.
I have this classes on the same namespace:
public partial class BaseForm : Form
{
bool isNew = false;
public BaseForm() {}
public BaseForm(bool isNew)
{
InitializeComponent();
this.isNew = isNew;
}
.
.
.
}
public partial class BitSetForm : BaseForm
{
public BitSetForm(bool isNew) : base(isNew)
{
InitializeComponent();
}
new private void InitializeComponent()
{
.
.
.
}
}
1) And I got this warning: Could not find type «..BaseForm,» Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built using the setting for your current platform or Any CPU.
2) Design-Time Errors in the Windows Forms Designer appears and hiding the design pane of the «BitSetForm» win-form.
What does this mean? What can I do to make the design pane of the «BitSetForm» win-form display again?
asked Dec 14, 2010 at 4:46
LEMUEL ADANELEMUEL ADANE
8,02216 gold badges57 silver badges72 bronze badges
You need to add a parameterless constructor to your BaseForm
.
It can even be private
; it just needs to exist.
Without one, the designer is unable to create an instance of the BaseForm to show in the design surface.
Remember to call InitializeComponent
in the constructor.
answered Dec 14, 2010 at 4:50
SLaksSLaks
857k175 gold badges1886 silver badges1956 bronze badges
0
Well a couple things…
- You need to build your application before the designer can instantiate your base class. This can be very difficult if your subclass form has a bunch of errors.
- The designer can only instantiate a class that has a default parameterless constructor. So that means your base class’s BaseForm(bool isNew) will never be called by the designer. Which means InitializeComponent will not either. You should move InitializeComponent to the parameterless constructor and have the second constructor call the first.
- InitializeComponent is private by default. You should not change its visibility to protected and since it’s private, no
new
modifier is needed. - InitializeComponent should never be chained that way to the base class. It should only be called by the constructor.
Given all those issues, I would highly recommend either giving up on Windows Forms inheritance or at least moving your base class into a separate assembly. I’ve tried it many times and it’s more trouble than it’s worth.
The key thing to remember is that when you’re viewing a form in the designer, the designer is not creating an instance of the form you see — it’s creating an instance of the base class. At runtime that is obviously not the case. So it’s very common to see different runtime/design time behavior.
answered Dec 14, 2010 at 4:54
JoshJosh
67.4k14 gold badges140 silver badges154 bronze badges
1
As mentioned by @SLaks, you need the InitializeComponent in the constructor of your class. I would actually have it in the no-parameter instance. Then, in the constructor of you boolean, i would change to
public partial class BaseForm : Form
{
bool isNew = false;
public BaseForm()
{
InitializeComponent();
}
public BaseForm(bool isNew) : this()
{
this.isNew = isNew;
}
}
So if you had other stuff you wanted performed within your BaseForm definition regardless of a parameerized startup, that too would be called. This way, the InitializeComponent is triggeed in EITHER case.
answered Dec 14, 2010 at 12:21
DRappDRapp
47.2k12 gold badges71 silver badges142 bronze badges
Chapter 17
Error Handling and Debugging
Writing a piece of software, even a relatively small one, can be an extremely complicated task. Developers usually put careful forethought and planning into the nature of the task and the means they will use to solve the task through the program that they intend to write.
The complex nature of software development invariably leads to errors in programming. This chapter sets out to explain the different types of errors that you might encounter when writing Visual Basic .NET code, some of the tools that you can use to locate these errors, and the coding structures used to prevent these errors when users run your program.
In addition to programming errors, your application should be able to gracefully handle all the abnormal conditions it may encounter, from user errors (when they enter a string where the program expects a date or numeric value) to malfunctioning devices, or simpler situations such as not being able to save data to a file because another application is using it. All these conditions may be beyond your program’s control, but your application should be to handle them. At the very least, your program shouldn’t crash; it’s OK to abort an operation and display a warning, but an application shouldn’t crash.
The errors caused by a computer program (regardless of the language in which the program is written) can be categorized into three major groups: design-time, runtime, and logic.
The design-time error is the easiest to find and fix. A design-time error occurs when you write a piece of code that does not conform to the rules of the language in which you’re writing. They are easy to find because Visual Studio .NET tells you not only where they are, but also what part of the line it doesn’t understand.
Runtime errors are harder to locate, because VS doesn’t give you any help in finding the error until it occurs in your program. These errors occur when your program attempts something illegal, like accessing data that doesn’t exist or a resource to which it doesn’t have the proper permissions. These types of errors can cause your program to crash, or hang, unless they are handled properly.
The third type of error, the logic error, is often the most insidious type to locate, because it may not manifest itself as a problem in the program at all. A program with a logic error simply
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
792 Chapter 17 ERROR HANDLING AND DEBUGGING
means that the output or operation of your program is not exactly as you intended it. It could be as simple as an incorrect calculation or having a menu option enabled when you wanted it disabled, or something complex like a database that’s duplicating order information.
This section will cover and demonstrate all three types of errors, and show you tools and techniques that you can use to hunt them down and squash them.
Design-Time Errors
Also called syntax errors, design-time errors occur when the Visual Basic .NET interpreter cannot recognize one or more lines of code that you have written. Some design-time errors are simply typographical errors, where you have mistyped a keyword. Others are the result of missing items: undeclared or untyped variables, classes not yet imported, incorrect parameter lists in a function or method call, or referencing members of a class that do not exist.
A program with as few as one design-time error cannot be compiled and run—you must locate and correct the error before continuing. Fortunately, design-time errors are the easiest to detect and correct, because VB.NET shows you the exact location of these errors and gives you good information about what part of the code it can’t understand. What follows is a brief example showing several design-time errors in just a few lines of code.
The event code shown in Figure 17.1 was typed into the Click event of a button named Button1.
Figure 17.1
VB.NET identifies the locations of design-time errors.
Note the three blue squiggly lines under various parts of this brief code (under two instances of the letter i and under the term lbNumbers). Each one of those squiggly lines represents a design-time error. To determine what the errors are, locate the Task List window in the IDE and bring it forward. The Task List displays the errors seen in Figure 17.2 for the code from Figure 17.1.
Figure 17.2
Corresponding errors in the Task List
Note You can determine which squiggly blue line corresponds to which design-time error in the Task List by doubleclicking the error in the Task List. The corresponding error will become selected in the code window.
Note that two of the errors are the same: they state “The name ‘i’ is not declared.” In this case, these errors are telling you that you’ve referenced a variable named i but you have not declared it. To fix these two errors, you need to modify the code as shown in Figure 17.3.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
TYPES OF ERRORS 793
Figure 17.3
Once declared, the variable doesn’t produce an error.
The only error remaining now is “The name ‘lbNumbers’ is not declared.” As the programmer of the application, you would probably have some type of idea what lbNumbers is. In this case, I was attempting to add 100 items to a ListBox, and lbNumbers is supposed to be the name of the ListBox on the form. This error tells me that I do not have a ListBox on the form named lbNumbers. I’ve either forgotten to put a ListBox on the form entirely, or I did add one but did not name it lbNumbers. To correct the problem, I can either make sure a ListBox is on my form with the correct name, or I can change this code so that the name matches whatever I’ve named the ListBox.
I added a ListBox named lbNumbers to my form. After doing so, however, I’m still left with a syntax error on the line, as seen in Figure 17.4.
Figure 17.4
The ListBox statement still produces a design-time error.
Note that the text of the error is different. It reads “The name ‘add’ is not a member of ‘System
.Windows.Forms.ListBox’.” This is telling you that it now recognizes that lbNumbers is a ListBox object, but there is no member (property, event, or method) named add on a ListBox. So what’s the correct way to write a line of code that adds an item to a ListBox? Some brief research in the help should yield the correct line of code—the one shown in Figure 17.5.
Figure 17.5
This syntax is correct.
Notice that all blue squiggly lines are now gone, and the Task List should be empty of errors as well. This means our program is free of syntax errors and is ready to run.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
794 Chapter 17 ERROR HANDLING AND DEBUGGING
Runtime Errors
Runtime errors are much more insidious to find and fix than design-time errors. Runtime errors are problems encountered by your program while it’s running. Runtime errors can take on dozens of different shapes and forms. Here are some examples:
Attempting to open a file that doesn’t exist
Trying to log in to a server with an incorrect username or password
Trying to access a folder for which you have insufficient rights
Requesting data from a database table that has been renamed
Opening a file on a server that is down for maintenance
Accessing an Internet URL that no longer exists
Allocating a resource without the necessary available RAM
Dividing a number by zero
Users entering character data where a number is expected (and vice versa)
As you can see, runtime errors can occur due to an unexpected state of the computer or network upon which your program is running, or simply because the user has supplied the wrong information (an invalid password, a bad filename, and so on). Because of this, you can write a program that runs fine on your own machine, and all the machines in your test environment, but fails on a customer site due to the state of that customer’s computing resources.
As you might imagine, runtime errors can be many degrees harder to diagnose and fix in comparison to design-time errors. After all, any error you make in design time is right there in front of you, on your own development PC. Not only that, but the Visual Studio compiler goes ahead and tells you right where a design-time error is and why it’s an error. The runtime error, by comparison, may only manifest itself in strange computing conditions on a PC halfway across the world. We’ll see in later sections how runtime errors can be detected and managed.
Logic Errors
Logic errors also occur at runtime, and because of this, they are often difficult to track down. A logic error occurs when a program does not do what the developer intended it to do. For example, you might provide the code to add a customer to a customer list, but when the end user runs the program and adds a new customer, the customer is not there. The error might lie in the code that adds the customer to the database; or perhaps the customer is indeed being added, but the grid that lists all the customers is not being refreshed after the add customer code, so it merely appears that the customer wasn’t added.
A second example of a logic error: suppose you allow the end user to manually type the twoletter state code of every customer address that they enter into your program. One of the functions of your program might be to display a map of the U.S. that shades the states based on the number of customers within each state. How do you suppose your shaded map will display customers with invalid state codes? Most likely, these customers would not be displayed on the map at all. Later,
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
TYPES OF ERRORS 795
the manager of the department calls you and says “The Total Customers Entered report for last month tells me that 7,245 customers were entered into our system. However, the Density Map report only shown 6,270 customers on it. Why don’t these two reports match?”
In this example, we’ve made a design decision—the decision to allow the end user to type the two-digit state code—and that decision has lead to a major logic error, the fact that two reports from the same system give different results for the number of customers entered into the system for the same time period.
Here are some actual VB.NET code snippets that produce logic errors. Consider the following code snippet.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer
i = 1
Do While i > 0 i += 1
Loop End Sub
Here we have an integer variable set to 1 and incremented by one in a loop. Each time the loop iterates, the number gets bigger. The loop will continue to iterate as long as the variable is greater than 0. See any problem with this? The problem is that the value of the variable will always be greater than 0, so the loop will never terminate. This is called an infinite loop, and it’s one of my personal favorite types of errors (favorite in the sense that I seem to always find a way to write new and exciting flavors of infinite loop). Of course, this loop isn’t exactly infinite—after 2 billion iterations, an overflow will occur, but that’s a good indication as to what happened.
Here’s another simple example of a logic error:
Private Sub ColorTheLabel(ByVal lbl As Label) If CInt(lbl.Text) < 0 Then
lbl.ForeColor = Color.Green Else
lbl.ForeColor = Color.Red End If
End Sub
This routine was intended to color the text of a label red if the label text contained a negative number, and green if it contained a positive number (or 0). However, I got the logic backward— the label text is green for numbers less than 0, and red otherwise. This code won’t produce any design-time errors or runtime crashes. It simply does the opposite of what I intended it to do.
Note finally that logic errors may or may not manifest themselves as program crashes. In the logic error examples above, the programs wouldn’t have crashed or produce any type of error message— they simply did not perform as intended. Some logic errors might indeed produce a program crash, at which point the line between a logic error and a runtime error becomes blurry. The fact that a new customer doesn’t appear in a grid might cause a crash if your program tries to highlight that new customer in the grid but the customer row isn’t there. In this case, we’ve made a logic error (not adding the customer to the grid) that’s caused a runtime error (program crashes when it tries to
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
Соседние файлы в предмете Программирование
- #
- #
- #
- #
- #
- #
- #
- #
Only Visible to You and DevExpress Support
Visible to All Users
Modify
support ticket and change its visibility
Urgent
Duplicate
We have closed this ticket because another page addresses its subject:
Disclaimer: The information provided on DevExpress.com and its affiliated web properties is provided «as is» without warranty of any kind.
Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose.
Please refer to the DevExpress.com Website Terms of Use for more information.
Recently viewed tickets
You have yet to view any tickets.
Your search criteria do not match any tickets.
A server error occurred while processing your request. Please try again at a later time.
YDB-1001
No deployment plan exists for Project with ID {0}.
YDB-1003
Define a topology to invoke RunOnce.
YDB-1005
Delete failed for {0} with id {1}.
YDB-1006
The object or action specified by the URL does not exist on the RulePoint server.
YDB-1008
The {0} with the id {1} could not be found.
YDB-1009
Object testing not supported.
YDB-1011
Conversion from {0} to {1} failed.
YDB-1012
{0} cannot be null.
YDB-1013
Cannot find any operations for web services with name {0} in the WSDL: {1}.
YDB-1014
Unable to parse the url: {0}.
YDB-1015
An error occurred while compiling DRQL: {0}.
YDB-1016
No value is provided for the required property [{0}], at type: {1}.
YDB-1018
Creation of {0} failed.
YDB-1019
The template configuration is not valid.
YDB-1020
Failed to test the object.
YDB-1021
The rule configuration is not valid.
YDB-1023
An internal server error has occurred.
YDB-1024
The value of the property {0} of type {1} in {2} is not valid.
YDB-1025
The watchlist type [{0}] is not valid.
YDB-1026
The RunOnce task failed for the source [{0}] with an exception — {1}.
YDB-1027
Data Integrity Failure.
YDB-1028
No bean information found for {0}.
YDB-1029
Cannot find service with the name [{0}] in the WSDL. {1}
YDB-1030
The required primary objects are missing in the redeploy request.
YDB-1031
The wizard configuration is not valid.
YDB-1032
Operation for the object [object name — {0} type — {1} state — {2} ] failed because its deployment is still underway.
YDB-1034
The operation [{0}] is not allowed on the object {2} currently in state {2}.
YDB-1035
Empty Analytic Map
YDB-1036
The operation [{0}] is not allowed on the predefined object [{1}].
YDB-1037
Object {0} is not valid.
YDB-1039
The request parameter contains a value that is not valid.
YDB-1040
Validation Error! Property Path: {0}, Root Bean: {1}.
YDB-1041
The value of the parameter missing in the template {0}: {1}.
YDB-1044
Value {0} for parameter {1} is not valid: {2}.
YDB-1045
The Topic {0} is not allowed for Source {1}.
YDB-1046
No services found for the WSDL.
YDB-1047
Failed to store the schedule.
YDB-1048
The given value of property : {0}, at type: {1} does not match the expected pattern {2}.
YDB-1049
The request parameter {0} is missing.
YDB-1050
Update failed for object {0} with ID {1}.
YDB-1051
Cannot create a schedule on a non-schedulable source [{0}].
YDB-1052
Conversion from {0} to {1} is complete.
YDB-1053
Provide a topic for the source.
YDB-1054
Cannot find the project with ID {0}.
YDB-1055
{0} {1} has {2} dependencies.
YDB-1055
{0} {1} has {2} dependencies: {3}.
YDB-1056
The profiler output could not be stored.
YDB-1057
Cannot find service type [{0}].
YDB-1058
Attempting to create duplicate key for {0} — {1}. Another {0} with same identifier present in the system.
YDB-1059
Deployment of Object(s) failed.
YDB-1060
{0} type beanInfo not found for {1}.
YDB-1061
Cannot find schedules for [{0}].
YDB-1062
You can deploy only when the referrer to a supporting object is a primary object.
YDB-1063
The group names [{0}] are not valid/could not be validated for deployment of {1} {2}.
YDB-1064
Action [{0}] is not allowed on object [{1}] of type [{2}] in [{3}] state.
YDB-1065
Deployment attempted on template rules for template {0} that has no deployment policy.
YDB-1066
No objects are found in the deployment package.
YDB-1067
{3} failed for objects that are not valid. Object[ name — {0} type — {1} state — {2} ] is not in valid state.
YDB-1068
Required dependent class is missing. Verify that the required jar is in the classpath.
YDB-1069
Testing of object {0} is not supported.
YDB-1070
No object was found in request to be copied.
YDB-1071
Action is not allowed — {0} — when {1}.
YDB-1072
Enter a start time with a valid value.
YDB-1073
You defined an invalid date for the schedule. Define an end time after the start time.
YDB-1074
The template has following dependencies: {0}.
YDB-1075
Project contains another [{0}] with the same name. Use a different name.
YDB-1076
Cannot find the watchlist [{0}].
YDB-1077
You can create only one dynamic schedule for a source.
YDB-1078
The reassign operation failed for objects in the project [{0}]. Define a deployment plan.
YDB-1079
The reassign operation is not applicable for template rules.
YDB-1080
The {0} operation failed as Controller Group Names are not provided for primary object {1} in the request.
YDB-1081
SQL query execution has timed out.
YDB-1082
The redeploy of supporting object operation failed.Cannot find related primary objects in the NEEDS_DEPLOYMENT state.
YDB-1083
Value of incorrect size provided for property [{0}], at type [{1}]. Size must be between [{2}] and [{3}].
YDB-1084
Only special characters such as space, dot, and hyphen are allowed. Names must start with a letter.
YDB-1085
Special characters are not allowed in the name. Names must start with a letter.
YDB-1086
No special characters except space are allowed in name. Names must start with a letter.
YDB-1087
No special characters except dot and hyphen are allowed. Names must start with a letter
YDB-1088
Value [{0}] provided for the object name [{1}] is a RulePoint keyword. Use a different value for the object name.
YDB-1089
The value of parameter [{1}] in the template rule [{0}] is missing.
YDB-1090
Template DRQL has changed. Perform an upgrade.
YDB-1091
RunOnce cannot be invoked on a non schedulable source.
YDB-1092
A warning occurred while compiling DRQL: {0}.