venerdì 16 marzo 2012

Insert item in a Generic Type list

Need to add an empty item to a generic type list?

In my silverlight project I have almost 60 lists, populated from an Oracle database and binded to different comboboxes.

Each generic type share same structure for fields name: TableName+FieldName
for example:

//1
public class GROUPS 
{
    public long? GROUPSID{ getset; }
    public String GROUPSCODE{ getset; }
    public String GROUPSDESCRIPTION{ getset; }
    public bool? GROUPSENABLED{ getset; }
}


//2
public class CUSTOMERS
{
    public long? CUSTOMERSID{ getset; }
    public String CUSTOMERSDESCRIPTION{ getset; }
}

As can see, both classes has two fields: ID and DESCRIPTION.
So how to if I need to add empty element to both with a generic method?

Here is my solution:

private void InsertEmptyItem<T>(List<T> list)
   {
    PropertyInfo[] propertyInfos = typeof(T).GetProperties();
 
    PropertyInfo id = (from p in propertyInfos where p.Name.Contains("ID"select p).FirstOrDefault();
    PropertyInfo description= (from p in propertyInfos where p.Name.Contains("DESCRIPTION"select p).FirstOrDefault();
 
    if (id == null || description== null)
        return;
 
    T instance = (T)Activator.CreateInstance(typeof(T));
    id.SetValue(instance, nullnull);
    description.SetValue(instance, "---"null);
 
    list.Insert(0, instance);
   }


Happy programming!

venerdì 3 febbraio 2012

lunedì 16 gennaio 2012

WP7 - Navigation with MVVM

There is a very interesting blog by my colleague Marco about navigation between pages in a Windows Phone 7, using MVVM pattern.

Here is the article (mostly in Italian)
http://silverlightedintorni.wordpress.com/2012/01/15/wp7-navigazione-mvvm/

giovedì 5 gennaio 2012

Whitespace is not allowed after end of Markup Extension

While working on a new Silverlight project I faced this error during compile: Whitespace is not allowed after end of Markup Extension.

The issue it's related to the Binding: be careful white spaces before and after parenthesis.
Here is a sample of code to explain better what I mean (check hi-lighted part):

<ComboBox Grid.Row="4" Grid.Column="4" DisplayMemberPath="Description"
          ItemsSource="{Binding DescriptionsList} "
          SelectedValue="{Binding SelectedDescription, Mode=TwoWay}"
          SelectedValuePath="DescriptionId" />


Have nice coding ;)

giovedì 1 dicembre 2011

Element is already the child of another element

I found this post  by HakOmaN very useful for understand my "Element is already the child of another element" problem in a Prism application:

http://blogs.msdn.com/b/hakoman/archive/2010/06/11/silverlight-navigation-framework-and-prism.aspx

Bye!

venerdì 25 novembre 2011

Silverlight 4: ChildWindow freezes the entire application

There is a known bug in Silverlight 4 ChildWindow control:
sometimes, without apparently reason, application freezes and you need to refresh the page.

A easy solution for this issue is this to use this instead of normal ChildWindow:
public class MyChildWindow : ChildWindow
{
      protected override void OnClosed(EventArgs e)
      {
           base.OnClosed(e);
           Application.Current.RootVisual.SetValue(IsEnabledProperty, true);
      }
}

venerdì 7 ottobre 2011

Visual Studio: 32bit to 64bit causes not valid win32 application resx

There is a (small???) bug in Visual Studio 2010 RESX generator:

when try compile a 32bit application on a 64bit system, if you have an ImageList control within your form, you may get a non-very-clear error in a form's resx file and project wont compile.

The problem is related to the base64 string generated by Visual Studio on 64bit systems.

If you double-click on error, it should go somewhere near the ImageList control string. To fix it just change manually


<data name="ImageList1.ImageStream" 
      mimetype="application/x-microsoft.net.object.binary.base64">
<value>
   AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w

to


<data name="ImageList1.ImageStream" 
      mimetype="application/x-microsoft.net.object.binary.base64">
<value>
   AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w



this should fix the error and let you compile the project!
Unluckily the problem will came again and again each time you will change something in the form.

Hope Microsoft's guys will provide a fix soon.

Here the discussion.