Thursday, February 25, 2010

context.xml not installing when using ClickOnce

We had an WPF application and were using ClickOnce to deploy the application.  When we installed the application, we got an error that looked like it could either be on install or startup.  After further investigation we found it was when we started the application.  The specific error I got was a - An unhandled exception (system.TypeInitializationException) occurred in ...  It turned out that the context.xml file that we were using with our application wasn't installed by ClickOnce.  The reason was that we had specified it as an embedded resource.  To fix it, click on the file, and then in the "Build Action" property change it to "Content".  Then right-click on your project and go to properties, and click on the "Publish" tab (I'm using Visual Studio 2008), and click on the "Application Files" button.  In the window that pops up, you should see "context.xml".  In the "Publish Status" column, make sure that you have "Include" selected, and in the "download group" specify "Required" and in the Hash column have "Include".

More information can be found here - http://blogs.msdn.com/karstenj/archive/2006/02/21/536322.aspx

For me this fixed the problem.

Monday, February 22, 2010

How to get a label to wrap in WPF

        <Label Margin="12,8,12,0">
            <TextBlock Text="This is your first time logging in and we need a little more information about you.  Please enter your first, middle, and last name along with your initials in all caps with no spaces or commas (Example PBJ)" TextWrapping="Wrap"  />
        </Label>

Thursday, February 18, 2010

Do the URL's at the top of a XAML file try to access the internet

At the top of a xaml file (after the Window declaration are the two lines below:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

The question came up if these two files cause every WPF application to try to access the Internet.  The answer is a resounding "NO".  What those two lines do is they declare a namespace.  In XML language one uses the term "xmlns" to declare namespaces.  The first line declares a namespace called "http://schemas.microsoft.com/winfx/2006/xaml/presentation" and the second line declares a namespace called "http://schemas.microsoft.com/winfx/2006/xaml".  Its not a pointer to a website, its just a name for a namespace, just like "Ted" is my name.  If I changed my name to "www.ted.com" then that would be my name, not necessarily a pointer to a website.

The difference between the first and second lines is the ":x" that is on the second line.  This basically maps the namespace associated with the second line has a prefix associated with it called "x".  "That means you can use controls in that namespace just by placing the namespace prefix before the element name (as in <x:ElementName>).  Its basically a shortcut to get to a namespace.

The first line doesn't have a prefix, so its the default namespace for the entire file.  This means that if you use a grid control and use the following <Grid>, then the XAML parser will look for that Grid control in the .NET namespaces until it finds System.Windows.Controls.Grid.

The reason its added to the top of every file, is to make sure other companies won't create different XML-based languages with the same namespace. 

Another example is that recently we purchased DevExpress and got their DXGrid control.  The following namespace was declared on some sample code:
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"

Now my understanding is that they have a namespace called "http://schemas.devexpress.com/winfx/2008/xaml/grid" and assigning a prefix called "dxg" to it.  So if I wanted to use their grid, I would do the following <dxg:GridControl ...>.  When I installed their software they installed their GridControl in the namespace "http://schemas.devexpress.com/winfx/2008/xaml/grid", so when the XAML parser runs, it would look on my computer for the namespace "http://schemas.devexpress.com/winfx/2008/xaml/grid" and try to find GridControl.

I hope that makes things clearer.



How to target a specific .NET Version when building an application in Visual Studio

When you create a new project in Visual Studio (by choosing File ➤ New ➤ Project), you can choose the version of the .NET Framework that you're targeting from a drop-down list in the top-right corner of the New Project dialog box (see Figure 1-2). You can also change the version you're targeting at any point afterward by double-clicking the Properties node in the Solution Explorer and changing the selection in the Target Framework list.

WPF Errors - InitializeComponent doesn't exist, etc

I've been learning how to write WPF applications, and I was going through activities in a book.  One of the activities is to create an ImageViewer.

I modified the Window1.xaml file and changed the first line to so that the class is called "ImageViewer.Window1".  I then called a FolderoOpenMenuItem_Click event from the xaml file, and defined it in the Window1.xaml.cs file.  I also had an InitializeComponent in the constructor.  When I compiled the application I got the following errors:

- The name 'InitializeComponent' does not exist in the current context
- 'Windows1.Window1' does not contain a definition for 'FolderOpenMenuItem_Click' and no extension method for 'FolderOpenMenuItem_Click' accepting a first argument of type 'Window1.Window1' could be found (are you missing a using directive or an assembly reference?)

This didn't make sense to me because I had defined the function, so why was it saying that nothing existed.  Also, I didn't know where the InitializeComponent function was defined.

After looking on the web, I found the following link - http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2f755d30-bd8c-4f9b-b36a-9cb56bea15cd

Basically the issue is that in the obj\Debug folder is a file Window1.g.cs and here is where InitializeComponent is defined.  The namespace I had was wrong, and once I changed the namespace to ImageViewer everything was able to build and work.

However, I was still concerned that if I changed the build configuration to release there would be problems.  But when I did so there were no problems.  But I still didn't like this solution, so what I did was right-click on the project and changed the Assembly Name and Default Namespace to ImageViewer and did a global replace for the solution on anything that said WPFapplication1 to ImageViewer.  I was able to build it and everything seemds to now work.