A in XAML: Ain’t for WPF/Silverlight

Even though XAML is officially the acronym of eXtensible Application Markup Language,- a generic term-, you can not usually separate it from UI frameworks, WPF and Silverlight. This creates some confusion, most people think of WPF as the declaration of UI with XML. Not that this is wrong, this explanation does not necessarily suggest decoupling data, logic and user interaction. XAML only helps WPF to achieve it, there is definitely more in WPF. I admit, XAML originally stood for eXtensible Avalon (code-name for WPF) Markup Language. But hey, there is more in XAML, too.

Since the first day I learned about WPF, I almost never used a UI designer tool. Perhaps, that is why I started using XAML everywhere I could, one could say it was love at first sight. They say love lasts roughly 3 years and even though it has been a little bit longer than that for me, I still feel strong about it. Lucky for me, Microsoft does also. Together with the recent release of VS2010 Beta 2, XAML finally has its own assembly, System.Xaml.dll targeting .NET Framework 4 Beta 2. I have now plenty of projects which can loose their dependencies on PresentationCore.dll, PresentationFramework.dll and WindowsBase.dll. I hope to convert them all easily and start making more.

First one to go was my almost forgotten open-source project Geostructor. I was expecting minor problems but I was wrong. I had none except the fact that, I had unconsciously created dependencies on WPF assemblies. After moving those dependent codes to where they really belong, I was ready to go with one little replacement. Replaced all class references of XamlReader and XamlWriter with the new XamlServices class. Not only everything worked as expected, some of my problems were gone too. This is what made me to write about the problem.

Back when I first encountered this problem, I was planning to post my solution here. Then, I heard about a new feature in the upcoming System.Xaml.dll and thought I should wait for it. In other words, I was lazy. So here it goes:

Old Problem

Here is a sample file saved by Geostructor v0.3, which targets .NET Framework 3.5 SP1:

Here, RelatedPoint property of RelativePoint is of type IGPoint which inherits INotifyPropertyChanged interface. RelatedPoint objects listen to the PropertyChanged events of the referenced objects. In order to keep these references after saving and loading back, I had to prevent XamlWriter from producing such result:

Old Solution

Continue reading

Expander inside ScrollViewers, a.k.a. when WPF power is out of control

Yesterday, I spent almost an hour on a small but annoying problem. I was working on a visual editor for an IDE, I develop for internal-use. Have I mentioned before that I started working (for more than a couple of months now) in a company again? Clearly, I haven’t been blogging actively passed year. Let’s see whether I will be able to change this in 2009 or not.
Anyway, this visual editor is for a DSL, or should I say a XAML grammar? It was the perfect opportunity to show the power of WPF. However;

Power is nothing without control
Power is nothing without control

Problem:

Visual editor’s job is to display the workflow in a tree view fashion. I chose displaying listboxes inside listboxes instead of a treeview. Each last node has an expander inside the listboxitem container. When user expands an item, containing listbox grows to display all of the items. I have no problem with that, in fact, that is exactly what I am grateful for. There is one catch, though. Expander controls not only expand but also shrink. When it does, listbox sits back and enjoys the void. A small sample to play with:

Solution:

You probably did know this. I did also, however when things are under multiple levels of ControlTemplates, DataTemplates and Styles, I was reluctant to go deep and investigate. Actually, I was reluctant to do anything at all. I had a great new years eve party and a greater headache. Luckily, WPF also provides an easy solution. Although, I spent a lot time to guess the problem.

Am I wrong to think that this was the default behavior when a control with a scrollviewer is not bounded? If I am, I can easily blame it on countless shots or hitting the floor so many times. Who else did work Friday, just to see “s?he” could not?

Riatalks, Snooker and Joker

I usually have a problem giving breaks. My body stopped sending signals to my brain long time ago, due to not getting back any. However I realized that I have limits too.

I was at the riatalks, a two day event at two separate conference halls. I worked out a plan and was going to be able to attend all talks that interested me. However, I forgot one small thing, timing is not an additive function around here. At the first day, even though some presenters couldn’t make it, talks was not over when I had to leave.

At that time, my body had a few reservations already. Some of you might know, snooker is a demanding game, physically and mentally. So I had to shutdown the message queue completely. In contrast, I had my personal best after 5 hours and just before my last sip at 2 A.M. Snooker is sometimes called chess with balls, however chess is a rather simple game. Snooker is pretty much Go with sticks to me.

Second day things got worse, time wise. I kept running between the halls, to see if any starts on time. Still I had the chance to chat with yesterdays last presenter, Daron Yondem. Daron and Ferruh Mavituna (in Turkish) had the best presentation skills at the talks. I am certainly not an authority but I can at least suggest everyone to read scott’s tips. Needless to say, I had to leave early for another round of snooker.

I closed the weekend with another physical challenge. We went to “The Dark Knight” at an IMAX theater, together with my snooker body. I heard the most stupid excuse to not giving a break, “… since this is an IMAX …”. Rest should be ” and as technology advance, we go backwards …”. I know intermissions are not common in most of the countries, but this movie’s length is not common either. I was cool with it but I saw people suffer.

Everyone suggests watching the movie in an IMAX theater however even 4K resolution is not enough at a big enough screen. Simple division that is, however if you had seen the movie, you might wanna check out how other math guys watched it, beware of the spoilers.

Today, I can tell by the mirror that breaks are necessary, so here is one.

Thinking in or out of programming languages

I remember the first time I took a look at C#. As a C++ programmer, I was impressed not by the language but by the IDE. I could do without multiple inheritance, but there were no templates either; case was closed, for good, I thought. Fortunately, many did not close the case. It did not took long for Microsoft to release C# 2.0; however, to me, damage was done. I am not talking about the huge .NET libraries, for which generics could help a lot. Real victims were the developers; there are still programs being written as if in C# 1.0. I don’t want to go into details of this matter right now. If you want to discuss, please don’t hesitate to comment.

Anyway, I started to use C# and I was happy until 3.0. Wait, wait, I know what is going to happen if I don’t make myself clear. C# is my language of choice when I need UI; but, you can never stop programming in C++. If I happen to use both of them for a project, I constantly switch between C++, C++CLI and C#. This is where the trouble begins. Before C# 3.0, I could say that my productivity was pretty much the same in each one of them. After 3.0, I caught myself trying to accomplish everything in C# as much as I could. Easiest way is not always the right way. Again, I have got to skip this if I want to finish this post. The point is, I use C# more and more everyday.

I have always had complaints about C# but a few days ago, I realized that some differences in programming languages are harder to overcome than I thought. I usually make a mistake, caused by experiences in another language, once or twice. What I have found out is that, if I can’t rationalize the difference, I make it all the time.

Suppose you wrote an interface which has a property with no setter. You also wrote lots of classes, implementing this interface. As long as these classes implement a public getter for the property, there is no problem, right? One day, you realize that number of these classes will increase and all will benefit from an abstract base class. (As a side note; there are no distinctions as interfaces or abstract classes in C++. There are classes without any implementation and partially implemented classes which could stand as counter-parts respectively. So, the following problems have no counter-part in C++) First of all, your new base class must declare (copy) all members of the interface as abstract members if there is no implementation. Is this really necessary? Secondly, you have to mark all of your previous implementations with override. I hear you saying, I can do all of these with a couple keyboard shortcuts, thanks to my refactoring tool. Last but not least, you can not do this all the time without changing your design of previously written classes. That’s right! Suppose one of your implementations also provide a public setter for our non-problematic-looking property, and another one with a private setter. Can you tell the problem?

Do you think in or out of your language?

P.S. This post is a side product of a project, I promised earlier, GeoStructor. Its code does not contain a solution to the problem above, so has lots of repetition. Actually, code requires lots of changes. When that happens, I will probably write another WPF related article which will include some techniques (regarding XamlReader/XamlWriter, Dragging, etc.) I used.


P.P.S. Initially, I tried to explain a problem without codes in order to provoke thinking. However, discussions will be healthier with a sample. Please expand to see the code. Sorry about the naming.

How many blograms of inventure have I got?

It has been a while since my last post. I am not sure why. My intention was to keep it rolling at least once a week. It was not a bad start either. I guess, I thought that a series of articles would help me focus. Since articles were technical, I gained a bit of recognition. So, why not continue? I was really busy at work, but knowing that is never an excuse for me, I kept looking.

Now, I realize that I felt obliged to keep the style. I didn’t want to steal readers’ time with my personal ramblings. Each time I started writing a new post, I found a different reason not to. Sometimes, I wasn’t happy with the quality. Some were a lot more time consuming than it should be, especially C++ ones. I wanted to write articles which were original if not extra-ordinary. Well, I couldn’t be more wrong. I have got to make mistakes and make them soon. Not only that, I know why I want to make mistakes. Here is the story.

Last month, I made a few important decisions about my life. Maybe the most important of all was to quit my job. It was a sudden decision. After a couple of though weeks, I managed to clean up my ongoing tasks and proved my sketches to my ex-co-workers. I had no plans, whatsoever. Initial reactions were like “- Where is your new workplace ? — There isn’t any “. Only a few close friends did not act as if I had lost my mind. Without a regret, I gave myself a few weeks of rest. A sabbatical year is not common among programmers, I suppose. I don’t want to be an exception either. Still, I couldn’t decide what to do. I’ve had times, considering opportunities abroad. Although, most suitable jobs for me are located in the States, family stuff keeps me in İstanbul. To keep a long story short, I want to target software developers and the reason of this alone requires a new post.

Yesterday, when I started my computer, I had over 500 unread posts on my reader. I read reluctantly until an inspiring post by Jeff Atwood. In my case, I feel like closing without a bookmark and going to open a random page. I want to keep reading this programming book and wish, I could read it all. It’s just that, I suspect a lifetime is not enough. There were still over 500 posts, waiting for me to read, but I decided to add one for you. For now, it looks like my adventures on inventing new ways to program will continue on this blog. I can wait a couple of more years. In the mean time, I hope this blog will gain weight and help me along. By the way, for a retirement plan, I imagine myself as a benevolent dictator of a new programming language ( a country would also do ) and bashing the young guys, who never stops coming with all kinds of strange ideas!

P.S. If you were expecting a WPF related article, soon I will write another post about an open source project of mine.

WPFix Part 3 (Extension Methods)

One of the new features, a.k.a. syntactic sugars, in C# 3.0 is extension methods. At first, it looks like compiler magic and it is no big deal. But isn’t it the same about class method? When a function argument is hidden, suddenly whole world changes. Clearly, OOP is not just a hidden this pointer, but we can think of extension methods as class methods that will only access the public fields. This may look like a step backwards but we are not living in an ideal world, in fact not even close to ideal. This is more like a side-step that will provide new ways forward. One of these new ways is LINQ, language integrated query. However, it is hard to come up with another as useful as LINQ. Is there a way to utilize extension methods in WPF as we did lambda expressions?

While introducing the lambda converters, I said that implementing IValueConverter or IMultiValueConverter is not a complete waste of time. There were two reasons behind this statement and hence two problems ahead of lambda converter with dreams of being the ultimate binding converter.

Problem 1

One of them was code maintainability in large projects, where a converter can be used more than once. Typing the same expression again and again is not an option.

Solution 1

Idea occurred to me, while reading Kent Bogaart‘s article on WPF converters and Josh Smith‘s article on piping value converters. They have both suggested a way to reuse and combine converters. Then, I had one of those, wait a minute, moments. This is where extension methods kick in. We could write our Convert method in an extension method and use them inside the expressions. Not only, maintaining these converters will be easier, but also they will be combinable by nature.

Continue reading

WPFix Part 2 (Binding Extension)

This time, I will skip the introduction and jump right into technical stuff.

In the previous post, I tried to reduce XAML’s dependency to code files for even the simplest operations. There were a lot of ways other than my lambda converter. We could write a custom binding which could save a lot more of typing. However, my intention is not to type less, but to keep relevant parts together. Still typing less does not hurt, as long as it is clear. We will come back to this, but before that …

Problem 1

Suppose we have an adjustable background color. Don’t worry, I am not going to write another custom color picker for WPF. I will try to use the lambda converter.

Parsing this lambda expression looks a bit harder. Multiple parameters are not a problem but SolidColorBrush and Color classes are.

Solution 1

Continue reading

WPFix Part 1 (Lambda Converter Extension)

As a mathematician, I always had a fondness for languages. As a programmer, this fondness became a devotion. Some might think that language is just a part of implementation which follows analysis and modeling. I agree that implementation is the least important one. I agree that it is like a tool for implementation, you can choose one or the other. I agree that a separate article, -which does not contain WPF in its header-, is more appropriate for this kind of topic. However, I also believe that programming languages have a much broader effect which includes modeling and even analysis. Our process of thinking is effected by our ways of communication.

Eventually, I will connect this to WPF, somehow in the future. In the last few years, I suppose the number of new programming languages has an exponential growth if we also include the new versions. Some believe that domain specific languages will reproduce asexually, some believe that languages converge to one as they copy their best parts. Even if there is a convergence, there is long way on which we speed up. My guess is that the hypothetical unified programming language, which will be the base of many domain specific languages and will act as a connection between them, will be the way to go. While waiting, I suggest we learn a few of these doomed languages. I hope, we will together witness how less these paths are traveled. I promise that following this post, my technical and theoretical musings will also travel separately. But now, lets start by a few technical ones. C# 3.0 and XAML are two new languages in two different paths. Lets get them together!

I always try to defend WPF in discussions with my colleagues. However, I will try to criticize WPF in this series of articles, namely WPFix. Visual Studio 2008 is to be released by the end of this month but I think, WPF and XAML still have a long way to go. Don’t get me wrong, I am just as glad as you are. I only wish, there was a better way than lengthening the evaluation period to spread the ideas. I will try to keep my critiques precise and search for practical solutions. Hopefully, I will also provide introductory links.

Problem

Data binding is pretty much the heart of WPF. We can think of WPF as a pretty complicated converter which consists of little converters for simple data types. However, these converters in the XAML may appear smaller than they are.

This may not be a complete waste of time from code maintenance perspective. But we, mortals, sometimes need a more practical method.

Solution

A similar problem in C# has been resolved lately by lambdas. You are no longer have to declare your method which won’t be used anywhere else. Here comes another future article name to my mind, maintenance is overrated. Personal Note: Use more controversial article names than WPFix Part 1. Back to our problem, we need a way to express ourselves in XAML. By express, I mean code. Hence lambda expressions.
Continue reading