rulururu

post Book Recommendation: XAML in a Nutshell

April 16th, 2008

Filed under: .NET, Books, WPF — Kai @ 7:34 pm

XAML in a NutshellToday I’d like to recomment a book to you. It’s called XAML in a Nutshell (O’Reilly, 2006; ISBN: 0596526733) by Lori A. MacVittie.

For those of you who don’t know already XAML is a .NET technology. It’s a markup language that can be used to help create desktop applications, web pages, and printable documents.

It stands for Extensible Application Markup Language and is pronounced as zammel ([zæ:mɛl]).

XAML generally follows XML syntax rules, just as any other XML-based markup language does. Each XAML element has a name and one or more attributes. Attributes correspond directly to object properties, and the name of the XAML element exactly matches the name of a CLR class definition.

XAML is pure markup, which means that while the names of event handlers are specified as attributes, you must implement the actual logic of the event handler in code. If you’re familiar with ASP.NET programming techniques, then you’ll be familiar with the term codebehind, which refers to the code “behind” a XAML interface element that is responsible for providing application logic such as event handlers.
It can be implemented in either C# or VB.NET. In both cases, the code can be placed inline in the XAML file, although this contradicts best practices in separating the presentation and application logic layers.

A XAML file can be compiled into a .baml (Binary XAML) file, which may be inserted as a resource into a .NET Framework assembly. At run-time, the framework engine extracts the .baml file from assembly resources, parses it, and creates a corresponding WPF visual tree or workflow.

When used in Windows Presentation Foundation, XAML is used to describe visual user interfaces. WPF allows for the definition of both 2D and 3D objects, rotations, animations, and a variety of other effects and features.

Here’s an overview of what to expect:

  • Introducing XAML
  • Getting Started
  • XAML Basics
  • Layout and Positioning
  • Resources
  • Storyboards and Animations
  • Elements
  • Controls
  • Shapes and Geometry
  • Layout
  • Animations and Transformations
  • Events

Along with this, you get eight appendices covering the major parts of the XAML technology.

This excellent book gives the reader a quick reference to XAML with examples. In other words, this book provides documentation of all core components and presents detailed discussions on features such as animation, resources, and layout that will help you on your way to becoming a XAML developer.
Even if you’ve never before heard of XAML, this book will give a good starting point to begin your investigations into this new technology…

The best comes last. You can get a preview online on google’s books: [link]

post Basic but reusable: Reading CSV

April 16th, 2008

Filed under: .NET — Kai @ 9:54 am

First, let’s recite the rules of CSV: Each line in a text file represents a record. The fields on each line are separated by commas.

Generally a CSV file should be abide by this rules:

  • Each record is one line (with exceptions)
  • Fields are separated with commas
  • Leading and trailing space-characters adjacent to comma field separators are ignored
  • Fields with embedded commas must be delimited with double-quote characters
  • Fields that contain double quote characters must be surounded by double-quotes, and the embedded double-quotes must each be represented by a pair of consecutive double quotes.
  • A field that contains embedded line-breaks must be surounded by double-quotes
  • Fields with leading or trailing spaces must be delimited with double-quote characters
  • Fields may always be delimited with double quotes
  • The first record in a CSV file may be a header record containing column (field) names

Take for example the next trivial CSV file:

alice, bob, eve, trudy
alice said: "don't move","""I won't"", he replied"

The first line parses into four separate fields (”alice”, “bob”, “eve”, “trudy”). The second one is trickier, but it produces two values.

The easiest implementation would be this:

private void readFile(String filePath)
{
  TextReader tx;
            try {
                tx = new StreamReader(filePath);
                String line;
 
                while ((line = tx.ReadLine()) != null)
                {
                    parseData(line);
                }
 
                tx.Close();
            } 
            catch(IOException ex) 
            {
                Console.WriteLine(ex.Message);
            }
}

Can be simply used that way:

private void parseData(String d)
{
            String[] sepData = d.Split(',');
            foreach(String d in sepData)
            {
                Console.WriteLine(d);
            }
}

And as you can guess, the code produces output like this:

> alice
> bob
> eve
> trudy

Those are just basics but they are reusable in some projects.

post Code nested inside a namespace?

April 11th, 2008

Filed under: .NET — Kai @ 7:38 am

Recently I wondered if code should be always nested in a namespace or not.

The question actually shouldn’t be which is “better” rather which is better for a particular case.

using bar;
namespace Foo {
  // etc
}

or

namespace Foo {
  using foo;
  // etc
}

I don’t know how to answer the question “which is better?” because I do not know the intended purpose of the code. I therefore tried to think of ways that these could be different, and answer the question “how do these differ?”

The answer is that if there is only one namespace in the file, it hardly matters. If there are two or more namespaces in the file, then put the directives you want shared amongst them at the top of the file, and the ones you do not want shared inside each the namespace. I further noted that having multiple top-level namespaces in the same file is a little weird, so maybe don’t even go there and make it a moot point.

My first statement was not right. There is a way that these could be different that I forgot about. The first version is equivalent to

using global::bar;
namespace Foo {
  // etc
}

The second version is equivalent to

namespace Foo {
  using global::bar;
  // etc
}

unless Foo contains a namespace called bar. In that case, it is equivalent to

namespace Foo {
  using global::Foo.Blah;
  // etc
}

So there are at least two ways that things can get broken here. First, moving a not-fully-qualified directive from outside to inside (or vice versa) may cause a semantic change in the program. And second, if there is a not-fully-qualified directive inside the namespace which refers to a top-level namespace then adding a child namespace that collides with that name can introduce a semantic change in the program.

Therefore my advice is:

  • have only one top-level namespace per file
  • if you choose to put using directives inside your namespaces then fully qualify them with the global alias qualifier

If you take this advice then it really does not matter whether the directives go inside the namespace or outside; either way you will not accidentally break yourself by introducing a name collision.

post Monitor class: Thread Safety

April 10th, 2008

Filed under: .NET — Kai @ 7:57 am

The Monitor class controls access to objects by granting a single thread a lock for an object. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object no other thread can acquire the lock for the object. Additionally, the Monitor class can be used to ensure that no other thread can access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object.

I ever wondered what the difference between using a lock statement, versus using a Montior.Enter / Monitor.Exit clause is? Actually there’s a few, but the main one is that a lock statement is inherently thread safe, where-as a Montior is not. So -

try {
Monitor.Enter(_lock);
DoThreadSafeCode();
} finally {
Monitor.Exit(_lock);
}

is essentially the same as -

lock (_lock) {
DoThreadSafeCode();
}

post Linq Querys and Joins

March 31st, 2008

Filed under: .NET, Database — Kai @ 8:20 pm

I have been playing with LINQ just a few times but I can say I am impressed with it. It does nearly everything out of box. In This post I will illustrate how to use Joins in LINQ queries.

Many a times we want a query where by we retrieve the data from one table and some the related data from the other table. Let says we have a category table and a posts table. Now when I retrieve all the records of the posts I also want to have the related category name (which is there in the category table). So I need to make a join between three tables to get the records.

Here is the LINQ Query to do the job:

var t = from p in Blog.Posts
            join cp in Blog.CategoryPosts on p.PostId equals cp.PostId
            join c in Blog.Categories on cp.CategoryID equals c.CategoryID
            select new
            {
                PostId = p.PostId,
                CategoryName = c.CategoryName,
                PostName = p.PostName,
                PostSubName = p.PostSubName           
            };

So basically to make a join between two tables we use the join keyword. After specifying the join keyword we need to provide the column name on which the join will be made. And then we need to provide the condition on which the join will be made. Hence the on keyword with the condition.

It’s so simple. If you know a little bit of SQL then this syntax shouldn’t be a problem. LINQ makes working with data in its various guises easier.

By intergating it into the language, we have rich integrated support for working with data.

However, there are times where the syntax is slighly different from what you would typically expect with TSQL. Once case where this occurs is when trying to join two data sources that are related by more than one field (also know as a composite key). This differs from standard joins where one table has a primary key and the other table has a foreign key id.

post Long Pathes

March 25th, 2008

Filed under: .NET, Windows — Kai @ 2:10 am

Just a short snipped I’d like to not forget about.

The maximal possible pathlength on Windows is, as you probably know, 255 chars. For that reason you can use the following:

Convert short path to long:

String s = Short2Long(@"D:\MYTEMP~1\RESOUR~1\sql.txt");
 
public static String Short2Long(String sShortPath) {
    StringBuilder sLongPath = new StringBuilder(255);
    GetLongPathName(sShortPath, sLongPath, sLongPath.Capacity);
    return sLongPath.ToString();
}

Vice versa:

String s = Long2Short(@"D:\My Temp\ResourseProvider\sql.txt");
 
public static String Long2Short(String sLongPath) {
    StringBuilder sShortPath = new StringBuilder(255);
    GetShortPathName(sLongPath, sShortPath, sShortPath.Capacity);
    return sShortPath.ToString();
}

Hope it might help you somewhere along the way ;)

ruldrurd
« Previous PageNext Page »
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)