Archive

Archive for the ‘WPF’ Category

My first real WPF and Blend 2 application

October 21, 2008 Comments off

I’ve been familiar with WPF since December 2006 and the release of C# 3.0, and I’ve had Expression Suite installed for almost a year. While I’ve toyed with it here and there, I have never devoted the time and effort necessary to really begin to become proficient.

However, since last week’s Silverlight 2.0 release, I have been burying myself again in learning new technologies. I finally decided to take the plunge, so I installed Silverlight 2.0, the Visual Studio updates, and upgraded to Expression Suite 2 SP1. I spent a couple of days going through ScottGu’s tutorials and some others on Silverlight.net. I followed along and built the samples, some in Visual Studio and some in Blend.

Most of these have been around for a while, so I’m not claiming any kind of leading edge stuff here. What I wanted to do was to share some of the insights I’ve had in attempting this project.

What’s the big deal

XAML marks a sea change in how user interfaces are developed. The end result of XAML is still .NET objects, and as such they can be created and managed programmaticaly, but the ability to simply describe what you want is very attractive. For a long time, I did not like XAML itself, and one of my reasons for putting off learning WPF was that I wanted to wait until something like Blend made all XAML interaction obsolete. After diving in for a few days, I no longer feel that way, but I’ll share more about that later.

What really makes all this so attractive is the ability to do basically whatever I can imagine for an interface. More importantly, I can do it without a ton of hand coded control drawing, something I never liked doing in the first place. The behavior of a control truly is separate from its presentation, and the presentation can be altered or replaced in any number of ways. In other words, you can achieve some pretty cool effects with a reasonably small effort.

Visual Studio or Blend?

I’ve been having an ongoing discussion with a friend of mine at RVNUG about the usefulness of writing WPF applications within Visual Studio. Having seen some demos and had some training on Blend, I was staunchly in the Blend corner on this one. While you can drag and drop controls in Visual Studio, it takes a lot of hands on XAML coding to get anything more than a rudimentary window up and running. As I mentioned previously, after seeing some XAML presentations I really wanted to avoid that as much as possible, which is what makes Blend so intriguing.

But, having gone through ScottGu’s Silverlight tutorial and building an application in Visual Studio, I have a better appreciation for it now. One thing I do like about using Visual Studio is that it is keyboard centric. As a classic Midrange developer I have always shied away from using the mouse as much as possible, so it appeals to my keyboard-philia. And of course, Intellisense is still the killer feature and makes it much more palatable. Also, as a seasoned and grizzled web developer who still likes the occasional dip into VI and Notepad, it only took a little time with XAML to feel comfortable with what was going on. Anyone familiar with XHTML and CSS should find XAML completely doable. It is, of course, a lot more complex, with numerous options and quirks, but it is still familiar territory.

Now, all that being said, I still prefer Blend 5 to 1 over Visual Studio for Visual XAML development. I have only run into a few things that I couldn’t accomplish easily through Blend, and I’m new enough to it that I still chalk it up to just not knowing the tool well enough. Applying and developing styles is still one of these areas: I so far have not figured out how to do them in Blend, so I revert to XAML editing.

Which brings me to my question of the day: “Should I use Visual Studio or Blend?”  The answer is a resounding “both!” OK, I’m sure you saw that one coming, but let me explain my position. If you are a developer, there is no question that you are going to use Visual Studio. After all, it is our bread and butter, and all the real code will still be developed in our beloved IDE. But designing serious WPF solutions in Visual Studio would be far too painful, even with great Intellisense support. There are simply too many options to have to code them by hand.

It reminds me of my first Windows application: a Java Swing application that I wrote in Wordpad. Believe me, the pain of that experience made me instantly recognize the value of Visual Studio and is largely responsible for my shift to Microsoft technologies. On the same order, as soon as I saw Blend I knew that this was the tool I needed to design good WPF applications. So, for layout and Visual Tree management, use Blend. When you find a problem that you think you must solve using XAML editing, switch over to Visual Studio and take advantage of Intellisense, which Blend does not have.

Quirks

A couple of things so far have jumped out at me. While Blend and Visual Studio do a pretty good job of keeping each other in synch, there are a couple of irregularities.

The first real problem I had was in adding existing projects to my solution in Visual Studio. I started my solution in Visual Studio and then opened it up in Blend to work on the design. Later in the same session, I went back to Visual Studio and added several projects. Now that I had some CLR objects to work with, I wanted to try Data Binding, so I followed one of the online tutorials but no joy. No matter what I did, I could not get Blend to find the objects. Every time I tried, I received a slew of “file could not be located” errors. Finally, I restarted Blend and when I opened my solution, there the missing objects were.

Second, there have been several times when I’m not sure that I am being properly prompted to reload. I could be imagining it, but I feel pretty strongly that I have made changes in one without being prompted by the other to reload them. Perhaps this is just a matter of timing Saves.

Conclusion

Well, I don’t really have any as of yet. I do think that a lot of developers are going to struggle against the designer learning curve, yours truly included. But I think in the long run we will be much better off. So far, I am pleased with my efforts. I like the combination of resources the two applications provide me, and I amd getting more comfortable in deciding which to use for certain scenarios.

I’m not quite ready for a tutorial series, but I will try to share some of my learning with you all as I go. In the meantime, give it a try yourself and let us know what you think. Happy Coding!

Categories: .NET 3.5, Blend, C# 3.0, Expression, WPF

Post Conference Detox

November 16, 2007 Comments off

I am travelling home today, but in between flights I am checking into a few items. First of all, and yes I know most of you are saying “Duh!”, I am checking out MSDN Subscriptions for the first time. See, when we started learning .NET we did not know if it would stick for us not, so the idea of committing to a subscription just wasn’t in the cards. Now, two versions of VS later I think there is no question that we are in .NET to stay.

I’m still waiting for VS2008 to RTM before I download it, but this way I can do it without waiting for CDs or anything like that (not to mention all the other code I get access to). The Visual Studio Professional version of MSDN is $1199 with an annual renewal of $799. Again, a no brainer.

I’m also looking at Microsoft Expression Blend. Preview 2 is out, and supposedly when it goes RTM it will be available on MSDN as well. I think I will go ahead and download the free tria now that XAML makes a lot more sense to me.

So, I have begun detoxing and will be employing these new goodies as soon as possible.? There are a million things floating about in my head.? I actually fell asleep last night thinking of a new WPF layout for a small internal database I started last week. I haven’t been this excited about coding in a long time: thank you Microsoft!

Categories: .NET 3.5, C# 3.0, WPF

Live Blogging VSLive! Austin – Day 2

November 13, 2007 Comments off

Keynote Address “Mapping your way through Microsoft’s Managed Data Access” – Michael Pizzo, Microsoft

10:30am:

The purpose of LINQ is to overcome some of the inherent flaws in Ad Hoc SQL data access from applications.

  • LINQ provides a set of language extensions
  • Integrates Query access as part of the application rather than implemented as Strings
  • Return values are in fact classes complete with strong typing rather than generic DataSets
  • Implements IQueryable to use LINQ over Collections
  • LINQ is available against DataSets, XML, and Entities as well as SQL

LINQ to SQL:

This is going to be the most important part of LINQ. This will essentially allow integration of the SQL Schema into usable objects in the Application.

  • Essentially a Strongly Typed SqlClient
  • Classes map to tables, views, SQL, and other Schema
  • Classes are generated by the LINQ Designer in VS2008 (saw a great demo of this)
  • Allows simple renaming of properties
  • Foreign Keys are implemented as Relationships
  • This means that additional table rows can be read in using Object syntax as opposed to additional SQL
  • Allows Type Inheritance for sub-Schema relationships
  • Can perform better than IDataReader
  • Can use StoredProcedures instead of SQL
  • Uses DataContext objects instead of IDbConnection – be sure to wrap in using statements for proper disposal
  • SQL Results become objects
  • Foreign Keys are available as Collections
  • DataContext has a .Log property for retrieving connection information
  • DataLoadOptions class allows finer control over when and how Related tables are read into the object

LINQ to DataSet:

  • DataSets still have value
  • Disconnected, serializable, etc.
  • LINQ makes filters, joins, projections, cross DataTable and cross Collection queries
  • Supports Typed and Untyped DataSets

ADO.NET Entity Framework:

The Entity Framework allows the Database Schema to be translated into complex Business objects.

  • Object Models are more complex than Schema’s rectangular storage model
  • Enables apps to work with higher level concepts such as relational navigation
  • Supports Type inheritance
  • Built over DataProviders
  • EntityClient is a declarative mapping of model to schema
  • LINQ can run over EntityClient
  • Produces Business objects
  • Provides Change tracking
  • Designers for this built into VS2008

The Future – Astoria:

  • Data Services over the web
  • LINQ/Query/CRUD over the web
  • Delivered as XML
  • Consumed over HTTP
  • LINQ can be used in Silverlight

11:40am update:

Attended “LING: What’s it all about?” by Ken Getz. I was really afraid that after the excellent keynote it would be repetitive, but NOT SO! I got so much out of it, and lots of it is not LINQ specific.

  • LINQ format is From… Where… OrderBy… Select
  • Select is always last, which really throws us SQL users
  • Select is last in order to properly support Intellisense (which unfortunately really only works in VB for now)
  • The From clause includes an item handle and indicates a Data Source
  • Uses implicit variable declarations – in both VB and C#, you can get an object variable to define it’s own type based on return value

Extensions Methods:

  • Add new methods to existing classes without access to the source code
  • Must be Public
  • Must accept a parameter of the type you wish to extend
  • In C#, it must be defined in a Static Class
  • C# uses the this keyword to indicate that this is an extension method
  • Can be used as a Static Class Method or an Instance method

Automatic Properties:

  • For properties that only expose an internal variable, the need to define and use that variable is no longer required
  • Does not require Constructors to update them, but it opens the problem of initializing properties that are not included in any constructor
  • SO – a Constructor can be used that does not actually exist: properties can be indicated on the new statement like so: MyClass c = new MyClass() {prop1 = value, prop2 = value};
  • Which means you can use a Constructor that essentially does not exist

LINQ for Objects:

  • Great for analyzing Collections
  • Takes advantage of Extensions Methods
  • Query class has many built in extensions:Any, All, Average, Where, Concat, Contains, Convert, Count, Distinct, Except, etc. etc.
  • Can use functions in Select clause
  • Functions must supply an expression (called “Lambda Expressions”)
  • Can include Anonymous Methods
  • Expressions can only have one item in and one item out
  • There can only be one expression on a function
  • Example: foreach(Actor a in Actors.Where(x => x.BirthDate.Month == 9))
  • “x” above is the individual object notation, “=>” indicates execute this comparison
  • Can use Anonymous Types – essentially, these are objects created on the fly (like creating columns in SQL Select) and added as a new Type to the resulting objects in the resulting Collection
  • Example: “Select new {NameOfProperty = actor.LastName + “, ” + actor.FirstName}”
  • Use Skip and Take to paginate different rows from within a collection

1:40pm update:

Attended “Styles and Data Templates” given by Bill Hollis. A good follow on from where we left off yesterday, the class got into some of the finer aspects of Templates and Styles. The more I see this stuff, the more I want to smack my head: I’ve had 3.0 installed since last year’s conference, but I’ve never forced myself to spend some time learning any of it. I look at it now and wish I had. But in truth this is the right time to be learning about it: with VS2008 and Blend, the technology is finally beginning to catch up with the theory. This is also the right time for my company given that we are just entering the development stage of a new suite of Applications. But enough rambling, on to the highlights:

  • Styles can be changed on the fly for any Element using the FindResource() method
  • TargetType is used to specify a style for all Elements of the same type en masse (within subsequent scope). This can be applied at any level, where ever a Resource can be defined (which is anywhere)
  • TargetType can only be applied to a single Type (this is not true for named Styles) – it cannot be found and applied to children of a different type
  • You can develop and import ResourceDictionaries to store Resources externally – think reuse, like importing CSS files
  • Styles support inheritance, so you can pull from a common parent and apply additional styles or override styles for specific elements
  • Style precedence is deermined from the element up, so if the element describes and style, then it overrides any named styles or TargetTypes
  • Inheritance accomplished by using “BasedOn={StaticResource parentStyleName}” on the Style definition
  • Styles can be tied to properties (and Events in WPF have reflecting properties, so by extension they can be tied to Events also)
  • DataBinding can be done at the Control or Container level, so you can specify a single Binding for an entire DataTemplate or Panel and then use it in child Elements
  • DataTemplate is like a mini-Window defined in XAML
  • DataTemplate places content in a ContentControl
  • The root Element is usuallya Panel, but can be anything
  • Anything goes regarding layout with a DataTemplate
  • Blend has a DataTemplate editor (why doesn’t VS have some of this stuff?)
  • A DataTemplate can have Triggers that change aspects of its display based on events and properties
  • They can be applied base don individual properties to any level Element

I have not seen it demonstrated, but after the presentation Billy confirmed that he has seen Vista Gadgets built on WPF. Man, the possibilities here are unbelievable. WPF really is the first GUI paradigm shift we’ve since the GUI was created.

3:10pm update – VS2008 Overview:

  • VS is a shell that hosts other Components
  • Because 3.0 and 3.5 build on 2.0, there is distinct support for each of these three frameworks in VS2008
  • 2008 Solution files are a little different than 2005
  • ASP.NET Web Applicaiton projects have AJAX support built in
  • Intellisense can be hidden by holding the CTRL key down (well, not really hidden, but it becomes translucent)
  • Intellisense is expanded and improved in VS2008: supports LINQ
  • JavaScript Debugging – use F9 to set breakpoints
  • Complete access to the Document object tree in debug
  • There is an ImmediateWindow for making and testing on the fly Script changes
  • WCF support is built in to VS2008
  • WPF Designer is built in to VS2008 (but investigate Blend)
  • WPF anchoring is controlled inside the designer window, not the properties grid
  • ClickOnce support enhaned
  • VS2008 also has XSLT debugging
  • XSLT breakpoints can be set in both theXSLT and the Data Source file
  • Web Apps have a Split Screen designer option
  • Support has been added for nested Master Pages

4:20pm update:

Attended a class on “Expression Blend for Developers” by Billy Hollis. Not really a lot of new information, but it did go into more detail and show some more hands on type use of Blend.

  • Blend is part of the Expression Suite, but Blend is included in MSDN. The rest of the suite is really meant for Graphics Designers.
  • Blend is the preferred tool for developing WPF and SilverLight UI.
  • Blend and Visual Studio share the same projects, so when you create a project in Blend you must select the Framework and Language targets even though you cannot develop code in Blend.
  • Blend is designed for Wide Format screens, so that will grant a better user experience than traditional aspect ratios.
  • Blend will execute code written in VS.
  • Grid is the default root element of any new WPF app in Blend, but it can be changed to other Panel types.
  • Grid Columns are better designed in VS than in Blend: the properties grid in VS makes setting column and row information easier while Blend only allows you to approximate sizes by eye on the design surface.
  • Otherwise, Blend is the superior tool for the UI development.
  • XAML editing in Blend does not have Intellisense (but VS does)
  • Blend has visual tools for creating DataTemplates and Styles and can save them as Resources, either for the Application, Windows, or a ResourceDictionary
  • Blend can set up DataBinding using a drop and drag technique

After all I’ve seen, between WPF and Blend, C# 3.5 and LINQ, all I can say is that I am going to have a lot of fun over the next few months.

Post session update:

The last session of the day was on the new Entity Data Model.? The keynote this morning had already hit most of the highlights, but in this session we actually got to see some of the tools and results.? And again, I am very impressed.? The technology is not ready for prime time (which is fitting since it won’t be released until spring), but it has great promise.? The point of it all is to be able to further abstract the database schema from the objects that use it.? EDM is all about abstraction: data mapping rules stored in XML files are how the complexity is managed.

Basically, the objects are defined as you would actually use them, with no regard to how they relate to database schema.? The framework then generates “go between” code to handle CRUD operations.? LINQ is used heavily to interface with the resulting Entities.? This will definitely be a technology to watch.? Here are some final bullets:

  • Can be done in VS2008, but you need to load the ADO.NET Entity Framework and tools separately
  • Provides graphical modeling of databases and Entities
  • The resulting Entities are then used in lieu of the actual DB connection (using the ADO.NET disconnected model).? In other words, you treat data like objects
  • One Entity can update multiple tables seamlessly, and the schema relationships are maintained
  • If the schema changes, must rerun the wizard to regenerate the new Entities. (unless you are a masochist who likes to do things manually)

More to come tomorrow.? Whew, these days can really wear you out!

Categories: .NET 3.5, C# 3.0, LINQ, WPF