Filtering data in Silverlight

by Brian Keating Sat, March 06 2010 18:23

Ever want to filter data in Silverlight? here's a simple example that uses a lambda expression to search on name (case sensitive)

 

ComboBox cbx = ((ComboBox)sender);

ICollectionView dataView =
            CollectionViewSource.GetDefaultView(this.DataContext);
if (!string.IsNullOrEmpty(cbx.Text))
    dataView.Filter = f => ((Job)f).Name.Contains(cbx.Text);
else
    dataView.Filter = null;

Tags: , ,

Silverlight | WPF

Cocoa Is My Girlfirend

by Brian Keating Wed, March 03 2010 20:53

Tonight I've stumbled across an interesting Blog on cimgf.

http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/#more-909

It's been many years since I wrote PHP, so I'd naturally use asp.net serverside (or if i had a pc powerfull enough I'd use the new REST capabilities of Sharepoint 2010 server).

I've been dying to have a play with Azures for sometime now but I'm always either snowed under with work or playing with some other technology, I remember reading that Azures (storage at least) has a REST API, and with the launch of the iPhone on the Vodafone network at the end of this month I've been looking into what's involved in writing objective C (and some mono stuff) so it seems I may finally get to have a play :-)

 

Tags:

iPhone | REST

Silverlight 4 Databinding and VS2010

by Brian Keating Wed, March 03 2010 15:32

If you've got a data driven application, databinding is the infrastructure of choice that makes the link between data and your UX

Silverlight 4 has a few improvments that brings it closer to WPF

  • TargetNullValue  - lets you specify the what to display in the target when the source value is null
    e.g.  {Binding Path=Name, TargetNullValue=NoName}
  • StringFormat - specify how a strnig should be formatted
    e.g.  {Binding Path=Salary, StringFormat=C}
  • FallbackValue - I love this one, comes in pretty handy when dealing with polymorphic classes where a you know in advance what properties specialized classes have and want to display them... or display a fallback value if they don't exist.
    e.g.  {Binding Path=LastLoggedOn, FallbackValue=Never}

 

 

Tags:

Silverlight

Silverlight and WPF

by Brian Keating Tue, March 02 2010 21:44

 

Tonight I tried to use the FlowDocument that you'll know well if you are familiar with WPF....

Important part above "tried to use"  .... but  ... silverlight version 3 doesn't support it :-(

Thats a second thing I've found lacking as I move some code to Silverlight, i've also found that DataTriggers don't work like the do in WPF Cry

Tags:

Silverlight | WPF

Different views in WPF/Silverlight

by Brian Keating Thu, February 25 2010 22:01

In my early WPF days I noticed the magic that having two different controls bound to the Same ObservableCollection meant that when I selected an item in one control, the same item got selected in the other.... which i I didn't want.

CollectionViewSource To the rescue

<Window.Resources>
<local:MyData x:Key="MyData"/>  
    <CollectionViewSource x:Key="AllData" Source="{StaticResource MyData}"/>  
    <CollectionViewSource x:Key="SearchData" Source="{StaticResource MyData}" Filter="MySearch"/>  
</Window.Resources> 
 
<ListBox ItemsSource="{Binding Source={StaticResource AllData}}"/>  
<ListBox ItemsSource="{Binding Source={StaticResource SearchData}}"/>

 

 

Tags:

Silverlight | WPF

C# 4.0 Dynamic Keyword

by Brian Keating Wed, February 24 2010 23:39

"object x" is a shorthand for "System.Object x".
This declares the variable x to have the type System.Object, this is strongly typed.
And since C# provides autoboxing, you can assign anything you want to this variable.

"var x = E" declares a variable x to be of the type of the expression E.
The E is required, not optional. This is a strongly typed declaration, and you can only assign values whose type is typeof(E) to it.

"dynamic x" declares the variable x to have dynamic semantics.
This means that the C# compiler will generate code that will allow dynamic invocations on x.
The actual meaning of "x.M" is deferred until runtime and will depend on the semantics of the IDynamicObject implementation

Tags:

Diagramming

C# on the iPhone

by Brian Keating Wed, February 24 2010 23:08

Recently I've noticed that the country has gone iPhone mad and I'm starting to buy into it myself... my blackberry days may be numbered as the only distinct advantage I see is that bandwidth is not used retrieving emails... but nowadays bandwidth is not so much as issue,, e.g. you get 2gb a month on a vodafone contract Cool

I've had a bit of a play around with Objective-C, interesting but .NET is my forte so I was delighted to see that it was possible (kinda0 to write code in c# for the iPhone platform.

The success of .NET platform from MS on multiple systems came when the Mono project became real. This open-source framework (cross-platform) brought the speed of C# development to Linux and OSX platforms. And now it will take us further.

As we all know, Apple has a highly strict inclusion policy for third-party runtime environments, which makes impossible to distribute apps that use this technology, like .NET and Java. So how can I be talking about .NET on the iPhone?

Because of a process called Static Compilation. Mono had already created a way to generate native code from common intermediate language (CIL) produced by .NET in native code in the past (AOT Ahead-OfTime compilation), with the intention of helping to reduce the speed of the initialization process and increase the process sharing among multiple processes. However, to keep the portability among different machines, a small piece of code was still in JIT (just-in-time) compilation – this method is the key to virtual machine systems that generate intermediate code which is converted to native code during execution.

So this process generates the final result in native code BEFORE the executing time, and this decreases also the size of the final app (since framework must go with the app), which does not need to load the codes to execute JIT and interpret CIL – even though it still adds approximately 6Mb from mono framework itself in the app’s final size.

There are also a few other tricks and Mono features that developers can use to reduce the size of Mono executables and assemblies for deployment in mobile environments. You can use the Mono linker to shrink the library size, you can omit the JIT and code generation engines from the executables, and you can strip out CIL instructions from the assemblies.

Static compilation makes it possible to build Apple-approved iPhone applications with Mono, but it comes with some limitations. Generics and dynamically-generated code are currently not supported when AOT compilation is used.

There are a lot of hoops to jump through right now to set up iPhone cross-compilation for Mono, but de Icaza (Novell's lead mono developer) says that developers who want to start now can use Unity, a third-party commercial programming framework for 3D game development that is built on Mono. Unity supports several platforms, including the iPhone and the Wii, and comes with its own built-in Mono cross-compilation environment.

Nowadays, thank to Unity 3D, there are over 40 apps (mostly games) on App Store that were written in C# and that carry mono framework inside. Wouldn’t it be great if Apple included that in the firmware? Imagine that if we have these 40 apps installed, it’s 40 times the same framework consuming space and no need for that. But that’s asking too much, right? :)

Tags:

C# | iPhone

WPF Textbox changing validation

by Brian Keating Tue, February 23 2010 13:42

Here's how to ensure that databinding happens when the value of a textbox changes. (as apposed to loosing forcus for example)


<TextBox Text="{Binding Interval, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />

Tags:

WPF

Anonymous delegates and Lambdas

by Brian Keating Mon, February 22 2010 23:47

Just a sample that may catch you eye as unusual..

 

 class WorkItem
{
    public WaitCallback Callback;
    public object State;
    public ExecutionContext Context;

    private static ContextCallback _contextCallback = s =>
    {
        var item = s as WorkItem;
        item.Callback(item.State);
    };

    public void Execute()
    {
        if (Context != null)
            ExecutionContext.Run(Context, _contextCallback, this);
        else
            Callback(State);

    }
}

 but here's the same code using anon delegates

class WorkItem
{
    public WaitCallback Callback;
    public object State;
    public ExecutionContext Context;

    private static ContextCallback _contextCallback = delegate(object s)
    {
        var item = s as WorkItem;
        item.Callback(item.State);
    };

    public void Execute()
    {
        if (Context != null)
            ExecutionContext.Run(Context, _contextCallback, this);
        else
            Callback(State);

    }
}

Tags:

C#

The threee A's of Silverlight Security.. (Part I)

by Brian Keating Mon, February 22 2010 21:36

·      Authentication

·      Access control

·      Auditing

Authentication.

One of the critical requirements of any LOB application ia authentication; before the user can use any function he will authenticate by giving a user id and password.

 

In ASP.NET web application, thi can easily be done by taking advantage of the membership provider and the server-side ASP.NET login controls. In Silverlight, there are two ways to enforce authentication: authentication outside and authentication inside.

Authentication outside is very straightforward and is similar to ASP.NET applications, with this approach, authentication happens in anASP.NET based web page before the Silverlight application is displayed. The authentication context can be transferred into the Silverlight application through the InitParams parameter before a Silverlight application is loaded. The other approach is to use a webservice.

 

 

Tags:

Silverlight