Archive for the ‘WPF’ Category.

How to resolve the "Could not create an instance of Type" error when "Reloading the designer" in Visual Studio 2008?

So, I recently started trying to implement the application I am developing using an MVVM model.

However, I ran into this annoying problem where when I have my main window’s XAML code including this line:

            <uc_treeview:PluginTreeViewControl Margin="0,0,0,29" MinWidth="240" />

My PluginTreeViewControl object looks as follows:

using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Input;
using LANDesk.HealthCheck.PluginOutput;
using LANDesk.HealthCheckViewer.LoadOnDemand.Sections;

namespace LANDesk.HealthCheckViewer.LoadOnDemand.PluginTreeView
{
    public partial class PluginTreeViewControl : UserControl
    {
        //readonly GroupViewModel mGroup;

        public PluginTreeViewControl()
        {
            InitializeComponent();

            Output o = new Output();
            OutputViewModel viewModel = new OutputViewModel(o.PluginGroups);
            base.DataContext = viewModel;
        }
    }
}

So I found that the lines after the InitializeComponent() function are causing my attempts to “Reload the designer” to fail. If I comment them out, the designer reloads. Of course, then if I have to uncomment them before compiling or debugging, and comment them again, when working in the Designer.

So after a while a thought came to me that maybe their is some type of “if” statement that would be true for the designer but not for runtime. So I researched and found this: DesignerProperties.GetIsInDesignMode Method

After reading about this, I changed my code in my Constructor to this:

    public PluginTreeViewControl()
    {
        InitializeComponent();

        // This "if" block is only for Visual Studio Designer
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            return;
        }
        Output o = new Output();
        OutputViewModel viewModel = new OutputViewModel(o.PluginGroups);
        base.DataContext = viewModel;
    }

And wouldn’t you know it, I have solved the issue entirely. The Designer now reloads just fine (as it doesn’t seem to error) and at run time the “if” statement is always false so the lines I need always run.

Also, the overhead of an “if (DesignerProperties.GetIsInDesignMode(this))” in inconsequential. However, I attempted to remove this overhead in Release builds as follows:

    public PluginTreeViewControl()
    {
        InitializeComponent();

        // This "if" block is only for Visual Studio Designer
        #if DEBUG
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            return;
        }
        #endif
        Output o = new Output();
        OutputViewModel viewModel = new OutputViewModel(o.PluginGroups);
        base.DataContext = viewModel;
    }

Now, I don’t have a problem with my Designer. This workaround makes me super happy!

Learning the Model-View-ViewModel (MVVM) pattern for WPF and C#

So, I recently came across some information on a development style called Model-View-ViewModel or MVVM.

There is a Microsoft article written by Josh Smith about MVVM here:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

And an MSDN blog here:
http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx

Basically the idea is that you separate the code for three elements:

There is a book by Josh Smith on Advanced MVVM here:
http://joshsmithonwpf.wordpress.com/advanced-mvvm/

So I have recently become aware of this model and will learning more about this over time.

I am developing an Application at work and I will try to use the MVVM pattern.

How to process command line parameters or arguments in a WPF application?

UPDATE 10/25/2010:
Avoid using Environment.CommandLine. It appears much easier, but isn’t as robust. I learned a while ago that the command line arguments can be accessed using Environment.CommandLine. This is only different than the process below in that it is way easier and the first argument is the full path of the executable. So all this work is not exactly necessary, right? Wrong! I tried Environment.CommandLine for a while and it didn’t last. There are times when this executable is launched by another executable and the Environment.CommandLine is not set even though the other executable launched this executable with parameters. So I had to return to using the steps below anyway.


Ok, so I wanted to handle command line parameters, which is easy in every other language, however, the need for easy was overlooked in WPF. It is not obvious and you are not going to figure it out without being told how to do it.

Microsoft provides a sample here, you can look at.
http://msdn.microsoft.com/en-us/library/aa972153.aspx

I am going to walk you through creating a new WPF Project in Visual Studio 2008. Then I will walk you through handling command line parameters (arguments).

  1. Open Visual Studio 2008.
  2. Go to File | New | Project.
  3. Under Visual C#, choose WPF Application and give the project a name and then hit OK.
  4. Go to Project | ProjectName Properties (where ProjectName is the name of your project).
  5. In the Properties of you project, click on Debug.
  6. Enter three parameters intothe Command line arguments text field: Param1 Param2 Param3
  7. Close the properties window.
  8. Double-click on App.xaml to open it. It looks like this:
    <application x:Class="ParametersForWPF.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        StartupUri="Window1.xaml">
        <application.Resources>
    
        </application.Resources>
    </application>
    
  9. Add a carriage return after StartupUri=”Window1.xaml” and start type inside the bracket the word Startup=. As soon as you see an equals sign you will get a pop up with the words . Double-click on that. The name Application_Startup will automatically be added.
    <application x:Class="ParametersForWPF.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        StartupUri="Window1.xaml"
        Startup="Application_Startup">
        <application.Resources>
    
        </application.Resources>
    </application>
    

    Note: This will also automatcially update the App.xaml.cs file which originally looks as follows:

    using System.Windows;
    
    namespace ParametersForWPF
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
        }
    }
    

    But after adding the Startup=”Application_Startup” line, a function called Application_Startup is automatically added.

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
    
    namespace ParametersForWPF
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
    
            private void Application_Startup(object sender, StartupEventArgs e)
            {
    
            }
        }
    }
    
  10. Now we only want to work with arguments if there are some, so lets add an if statement inside the Application_Startup function as shown:
            private void Application_Startup(object sender, StartupEventArgs e)
            {
                if (e.Args.Length > 0)
                {
    
                }
            }
    
  11. Ok, so the next step is to create a public static string[] member variable to hold the arguments and assign the arguments array to it. I called my member variable mArgs. I use prefix it with m so I know it is a member variable.
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;
    
    namespace ParametersForWPF
    {
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            public static String[] mArgs;
    
            private void Application_Startup(object sender, StartupEventArgs e)
            {
    
                if (e.Args.Length > 0)
                {
                    mArgs= e.Args;
                }
            }
        }
    }
    
  12. Now, in order to access the data in Windows1.xaml.cs, just call the App.mArgs array in the constructor as shown.
            public Window1()
            {
                InitializeComponent();
                String[] args = App.mArgs;
    
            }
    
  13. Put a break point on the line and start with debugging and sure enough you will see your arguments properly assigned to the String[] args variable. So you now have your parameters accessible in your WPF application.Hope this helps you.

    Copyright ® Rhyous.com – Linking to this article is allowed without permission and as many as ten lines of this article can be used along with this link. Any other use of this article is allowed only by permission of Rhyous.com.

Where to get free training and tutorial videos for WPF?

So WPF doesn’t seem very well documented. I have been struggling to find good documentation and learn all its intricacies. I like tutorials and training videos and examples and samples, all of which have been difficult to come by.

So I thought I would share with everyone the following web site where Microsoft provides quite a lot of training videos on WPF.

WPF Videos
http://windowsclient.net/learn/videos_wpf.aspx

If you take time to watch all these videos, you will probably be a master at WPF very quickly.

A good site for learning WPF ListView as GridView development in C# and WPF

So i needed to make a table layout in WPF and I noticed that it is not that simple to learn.

However, I found a really good site that teaches you how to populate a ListView using a GridView from XML, a DataTable, data from a database, etc…

The new WPF GridView customized 1 of 3
http://www.codeproject.com/KB/miscctrl/GridView_WPF.aspx

I spent all day trying to figure out what this site taught me in less than an hour.