Lightswitch and Silverlight – Passing parameters to a screen

14 בנובמבר 2011

תגיות: , , ,
4 תגובות

In his great article “Hierarchical Data with LightSwitch” Michael Washington is demonstrating how Lightswitch can be extended using custom Silverlight controls. The article helps to build a tree view control for displaying hierarchical information such as departments, categories etc. 

After implementing the solution I wanted to extend the tree view so it will effect the screen itself and refresh the selected item data according to the selected node in the tree (The solution brought in the article only describes the ability to refresh the tree view when items are updated or added to the list).

(I used my tree view to display hierarchical categories in my application so the code will reference the items as “categories”)

Making calls from custom Silverlight controls to methods defined inside a Lightswitch screen is easy:

  1. add a method to the screen
  2. call the method using the following format from the control:

var objDataContext = (IContentItem)this.DataContext;
var Screen =
   (Microsoft.LightSwitch.Client.IScreenObject)objDataContext.Screen;
Screen.Details.Dispatcher.BeginInvoke(() =>
{
    Screen.Details.Methods["UpdateSelectedDepartment"].CreateInvocation().Execute();
        
});

Using method invocation you can call methods defined on the screen but the only problem is those methods don’t have properties – something which is required in this case since we want UpdateSelectedDepartment to knows what the selected department is.

This can be handled in two ways, the first one (which I will not bring here) involves altering internal files so the invoked method will refer to passed arguments – it worked for me but it was very unstable.

The second method (which I’m currently using) was to do the following:

  1. Add a parameter typed as the selected value in the tree view to the screen (In may case parameter called SelectedDepartment of type “Department”).
  2. Add a method to the screen which will be invoked by the control (In my case I named it UpdateSelectedDepartment).
  3. Add The following code to the method:

partial void UpdateSelectedDepartment_Execute()

{

   Departments.SelectedItem = this.SelectedDepartment;

}

  1. Use the following code inside the Silverlight control:

public SilverlightTreeControl()

{

  InitializeComponent();

  treeViewControl.SelectedItemChanged += new RoutedPropertyChangedEventHandler(treeViewControl_SelectedItemChanged);

}

void treeViewControl_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object e>)

{

var objDataContext = (IContentItem)this.DataContext;

var Screen =
               (Microsoft.LightSwitch.Client.IScreenObject)objDataContext.Screen;

Screen.Details.Properties["SelectedDepartment"].Value = e.NewValue;

Screen.Details.Dispatcher.BeginInvoke(() =>
            {
                Screen.Details.Methods["UpdateSelectedDepartment"].CreateInvocation().Execute();
        
            });

}

As you can see the solution is simple – update the selected department parameter, call the method, access the parameter and set the current item to be the parameter value.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

4 תגובות

  1. emad26 בדצמבר 2011 ב 12:12

    Could You Please Provide source code (sample project) thanks in advance

    הגב
  2. Larry9 ביולי 2012 ב 20:20

    Hi Doron

    Thanks for posting this…it's exactly what I need for a project I am working on.

    I new to LightSwitch and Silverlight but I have the treeview and screen working. I have been trying to follow your code but I am not sure where some of the values come from as I am getting an error.

    There are some syntax errors in the code that make it difficult for a newbie like me to understand. One instance is:

    (object sender, RoutedPropertyChangedEventArgs)

    which should be:

    (object sender, RoutedPropertyChangedEventArgse)

    Is it possible for you to post a working solution for this? I can learn from that 🙂

    Thanks

    הגב
  3. alessio25 בספטמבר 2012 ב 11:44

    Very Good
    I completed with this the functionality the example of treeView in this link http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/53/Tree-Control-Hierarchical-Data-with-LightSwitch.aspx
    thank you a lot

    הגב
  4. Filippcsolar176621 בפברואר 2013 ב 8:47

    Something at me personal messages do not send, a mistake….

    הגב