Visual Studio

Blending with Sample Data feature (Blend 4)

Posted on

Had a chance to play more with Blend 4 this week, have been eyeing on the Sample Data feature for a while but only had a chance to use it in practice now.

So imagine if you have defined the backing view model below

public class MainViewModel
{
	public MainViewModel()
	{
		// Insert code required on object creation below this point.
	}
	public IList<Person> Persons { get; set; }
}

public abstract class Person
{
	public String Name { get; set; }
	public Int32 Age { get; set; }
	public String Initials { get{ return "some initials"; } }
}

public class Male : Person
{
}

public class Female : Person
{
}

You can then open the view, on the top right panel, click on the Create Sample Data from Class option below

Choose the view model to generate the sample data, click Ok

Bam! the sample data file has been generated nicely below, and notice one thing, the read only Initials property can be “set” as well there :)


Before moving on to the next step, be sure to replace Person Type on the rows into either Male or Female.

Now, time to get into the view with the d:DataContext binding and data templates by Data Type for each Male and Female.

hhhmm… a little bit disappointing, isn’t it? all are seem to be in place, but why it’s displaying these weirds _.di12.NameSpace.Class rather than our expected data template result?
Now back to the xaml, add d:IsDesignTimeCreatable=”False” attribute on both data template elements, save, close and reopen the xaml again.

And there’s the shiny UI with some sample data in it :)

Notice that Blend sometimes doesn’t refresh the design view as soon as we make some changes on the data template (it works fine on non data template changes).
I only found that the effective way is to just close and reopen to see the changes effect, so if you’re editing something on the data template and it’s not reflecting, try to close and reopen it first :)

I found this sample data feature is very useful, how bout you?

Have also shared the sample project for grabs here.

Hope this helps :)

Extension over an extension for Dispatcher

Posted on

I’m working on a migration of an existing winform control in our WPF app into a new WPF shiny control ;)

Interestingly it was then breaking with the exception: The calling thread must be STA, because many UI components require this when trying to perform some action on the WPF control where it’s working fine for the existing winform version.

Turned out that this was due to the code was being invoked from a background thread through threadpool. Now it’s pretty easy to resolve this, we can use the Dispatcher property which is inherited by DispatcherObject abstract class which is the base class of WPF User Control.

 

 

 

 

 

 

 

Now the Dispatcher class itself turned out to have a DispatcherExtensions class (below) as well which accept Action type

 

 

 

 

 

 

So in order to fix the calling thread exception, we just need to have the following code.

void OnSomeRequest()
{
    if (!Dispatcher.CheckAccess())
    {
        Dispatcher.Invoke(() => OnSomeRequest());
    }
    else
    {
        someUserControl.DoSomething();
    }
}

Now if we only have 1 certain place which will perform some UI update in the code, this is okay, but imagine if you have many places in the code that you need to repeat the pattern above :p

So we can simplify the above again into below, we have reduced the code from 8 lines into just 1 line :)

void OnSomeRequest()
{
    Dispatcher.InvokeIfRequired(() => someUserControl.DoSomething());
}

The new extension over existing DispatcherExtension is simple.

using System;
using System.Windows.Threading;

namespace ExtensionTests
{
    public static class AnotherDispatcherExtensions
    {
        public static void InvokeIfRequired(this Dispatcher dispatcher, Action action)
        {
            if (!dispatcher.CheckAccess())
            {
                dispatcher.Invoke(action);
            }
            else
            {
                action();
            }
        }
    }
}

Note that we can always add more extensions into this class, for me, I only have one above as I only require it, simple and only introduce when you need it :)

Eric De Carufel has written the more complete extensions as well in his blog post
And if you’re wondering why VS intellisense is giving you a false alarm that CheckAccess is not available for Dispatcher, Claus Konrad explained it in his blog post

Linq Samples available under your nose

Posted on

I often visit MSDN 101 Linq Samples whenever I need to dig something out for linq stuffs, but it’s just yesterday that I actually found that the whole samples are actually available locally on the drive.

Below is the direct path of the sample folder (may vary depends on your CPU and VS version) and the linq samples are available under CSharpSamples.zip -> CSharpSamples\LinqSamples\SampleQueries

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033

Just press F5 (build) and you’ll get this faboulus application loaded with all examples with the real codes to play with as well. And gotta hand it to the guys who wrote this app, it’s really neatly structured from both UI and code point of view. Well done and thanks guys ;)

Debug/Trace output is not being captured by DebugView when VS2010 in debug mode

Posted on

I love Sysinternal tools and one of them that I normally keep running aside Visual Studio while coding  is DebugView. I used it quite frequently when I was working with BizTalk application.

There are several benefits that I can always think of with DebugView:

  • The time information is always useful, it’s always there for each line, very useful especially when looking into performance
  • The outputs are not being cluttered by the other output messages such as in Visual Studio output messages
  • It’s running externally, which also means that we can run this separately with the real app and see the debug/trace messages in there without any configuration required.
  • You can even connect to other machine to capture the output, this is really useful if you have an application server, you can just fire up the tool and say that you want to connect to that particular server and walahhh you can see the debug/trace outputs (of course with administrator access to that machine in mind)

Now, I actually just realized today at home when trying to create and debug a simple app with VS 2010, the output is not being captured anymore in the DebugView and sadly this was said by Design.

I can see that a lot of people have the same frustrations (here and here) as well because of this, you can find couple of suggested workarounds for this, however it seems that you still can’t get the debug/trace being output into the DebugView when debugging the code (you can see the debug/trace in DebugView by just running the app). I haven’t tried whether this is working or not when running unit tests in VS2010, but I would suspect the same thing will happen.

I was looking an alternative where we can actually find a way to “inject” the messages into somewhere that DebugView can capture by probably having a derived class of TraceListener, but i didn’t manage to find one unfortunately, yet.

This is just my rant because of something that we can get easily is now not anymore :|

Love to hear if you have any findings on this, but I’ll probably live by using the Ctrl+F5 for now as I don’t feel like doing -> Debug.WriteLine(DateTime.Now.ToString() + ” – ” + someDebugMessage);

 

Blue Screen on VS2008 SP1 Profiler using Intel core i7 (Nehalem)

Posted on

Just got blue screen when trying to start up VS2008 SP1 Profiler.
And as usual, google saved the day.

There is a KB 958842 – FIX: A Stop error or a crash occurs on the operating system when you profile an application by using Visual Studio 2008 Service Pack 1

This applies to any PC which is using Intel Nehalem Processor (i7).

Hotfix can be downloaded at http://code.msdn.microsoft.com/KB958842