Easy Windows-Phone 7 Navigation

Page navigation in WP7 is crappy:

  1. You must pass an instance of System.Uri which must be of UriKind.Relative
  2. You cannot pass any object but only strings. If you need to pass some object, you usually pass an id and find the object in the target page by the id, assuming you have access to the model, which is usually a public property of some static class.

Here is a sample navigation:

In the source page:

private void NavigateToSomePage()
{
    NavigationService.Navigate(
        new Uri("/SomePage.xaml?data_id=" + data.ID, UriKind.Relative));
}

In the target page:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var data =
        GetTheDataByTheID(NavigationContext.QueryString["data_id"]);

    // continue
}

A kick-ass solution for kick-ass coders

First, a sample navigation using the solution:

In the source page:

private void NavigateToSomePage()
{
    NavigationService.Navigate<SomePage>(data);
}

Continue reading

Posted in .NET, WP7 | Tagged , , | Leave a comment

CLAP@github

CLAP’s source code is now available on github:
https://github.com/adrianaisemberg/CLAP

Posted in .NET, Command-Line | Tagged , | Leave a comment

CLAP: Help and Empty Handlers

Previous post introduced the Global and Help features of CLAP.

In this post, a new syntax is introduced to handle Help and Empty handlers.

Get It

Help

In the following example, the Help method is defined as a help handler using the [Help] attribute:

class Program
{
    static void Main(string[] args)
    {
        Parser<MyApp>.Run(args);
    }
}

class MyApp
{
    [Help]
    public static void Help(string help)
    {
        Console.WriteLine(help);
    }

    [Verb]
    public static void Foo(string bar)
    {
    }
}

If the input verb is “help” the help handler is triggered and prints the help string to the console.

The help handler must accept a single string argument which will automatically contain the generated help string.

Name Override

The name of the handler can be different from the method’s name. In the following example, “h” is used instead of “help”.

class MyApp
{
    [Help("h")]
    public static void Help(string help)
    {
        Console.WriteLine(help);
    }
}

Aliases

Like verbs, additional names can be registered for a help handler. In the following example, “help”, “h” and “?” are all registered for printing the help:

class MyApp
{
    [Help(Aliases = "h,?")]
    public static void Help(string help)
    {
        Console.WriteLine(help);
    }
}

Empty Handlers

Empty handlers are invoked when the exe is executed without any arguments.

In the following example, the “Foo” method is defined as the empty handler:

class MyApp
{
    [Empty]
    public static void Foo()
    {
    }
}

Notice that no more than one empty handler can be registered for a type. In-addition, the empty handler must not accept any parameters.

The empty handler can be a static or an instance method. If it is an instance method, like verbs, an instance of the type must be passed to the Run method of the parser.

Empty Help Handlers

An empty handler can be defined as a help handler, meaning that when no arguments are passed to the exe, the help is handled:

class MyApp
{
    [Empty, Help]
    public static void Help(string help)
    {
        Console.WriteLine(help);
    }
}

The method will be invoked also if “help” is passed as an argument.

Notice that an empty help handler has the exception of having a single string parameter, for the help string.

Summary

Up until now,  a whole set of verbs, parameters and global parameters could be defined using only attributes, so the Main method only included a single call the the parser.

If Help and Empty handlers were needed, they had to be registered using delegates (or lambda expressions).

From now on (version 1.2), Help and Empty handlers can be also defined using attributes, leaving you with the minimum code required for running your app from the Main method.

Posted in .NET, Command-Line | Tagged , | Leave a comment