locked
Howto use the initParams of a SilverlightApplication In LightSwitch RRS feed

  • Question

  • Hi,

    If spent quite some time in finding out how to use the initParams of a SilverlightApplication In LightSwitch.

    I cannot use, unfortunately, Application_Startup(object sender, System.Windows.StartupEventArgs e).
    This works only in a SilverLight application.

    What I want to do is when a LightSwitch application loads,
    in Application_Initialize() of Application.cs, I want to set a global variable "SessionId" with a value that is passed throug the silverlight app
    with  <param name="initParams" value="sid=TheSessionId" />

    This "SessionId" can be used throughout the LichtSwitch app.


    So far I got info from:

    A: http://social.msdn.microsoft.com/Forums/en-US/d8728f78-f20f-473b-9b32-1ebdff5976c0/passing-parameters-to-lightswitch

    B: http://social.msdn.microsoft.com/Forums/en-US/74ea9a07-6e2f-46bc-9fc1-87175b965272/appmanifest-and-deployment-targeting-a-class-library-in-silverlight

    A:
    "If you really need to access start parameters of the SilverLight application,
    (initParams supported in the SilverLight framework).
    There is a workaround.
    Turn on showing all files in the solution explorer, you can find AppManifest.xml in the ClientGenerated project.
    This file declares that the Microsoft.LightSwitch.Runtime.Shell.Implementation.App class is loaded,
    when the LightSwitch runtime starts.
    You can create your own application class inheriting from this class,
    and change the AppManifest.xml to point to your class.
    In this way, you can hook up the StartUp event,
    and get the initialization parameters in that event.
    Those values can be preserved and used later."

    I created a class called "appStartUp.cs" in the folder Client/UserCode


    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace LightSwitchApplication
    {
        public class AppStartUp : Microsoft.LightSwitch.Runtime.Shell.Implementation.App
        {
            private void AppStartUp_Startup(object sender, System.Windows.StartupEventArgs e)
            {
               public string mySid{get;set;}
                
                if (e.InitParams.Count == 0)
                {
                    mySid = "";
                    
                    return;
                }
    
                foreach (var param in e.InitParams)
                {
                    if (param.Key == "sid")
                    {
                        mySid = param.Value;
                    }
                }
            }
        }
    }



    In appManifest.xml I changed the  
    EntryPointType="Microsoft.LightSwitch.Runtime.Shell.Implementation.App"
    to
    EntryPointType="AppStartUp"

    When I run the app I get an error:

    Line: 104
    Error: Unhandled Error in Silverlight Application
    Code: 2103
    Category: InitializeError
    Message: Invalid or malformed application: Check manifest     


    B: This is described on http://social.msdn.microsoft.com/Forums/en-US/74ea9a07-6e2f-46bc-9fc1-87175b965272/appmanifest-and-deployment-targeting-a-class-library-in-silverlight

    "After altering the AppManifest.xml file, you have 2 definitions for the application entry point:
    One in AppManifest.xml.
    One in the SilverlightApplication1.csproj:
    right click on the app project, properties, and remove the "startup object" that was originally set by the wizzard."

    In LightSwitch I cannot find SilverlightApplication1.csproj, So I cannot remove the "startup object"

    I hope someone has an answer for me, and i asume for a lot of other LightSwitch Application developers.

    Best Regards
    Norbert Kramer

    Monday, September 16, 2013 9:26 PM

Answers

  • You can get the initParams in a more straight-forward fashion in the Application_Initialize() method:

    public partial class Application
    {
        partial void Application_Initialize()
        {
            IDictionary<string, string> initParams = null;
            Dispatchers.Main.Invoke(() =>
            {
                initParams = System.Windows.Application.Current.Host.InitParams;
            });
    
            string sessionId = null;
            if (initParams.TryGetValue(sessionId, out sessionId))
                this.SessionId = sessionId;
        }
    
        public string SessionId { get; private set; }
    }

    No need to change any of the manifest or configuration files.

    Justin Anderson, LightSwitch Development Team

    • Marked as answer by NorbertK Tuesday, September 17, 2013 8:07 AM
    Tuesday, September 17, 2013 6:05 AM
    Moderator
  • Dear Justin,

    Thanxs for your reply.

    This got me on the road again!

    Best regards,

    Norbert Kramer

    • Marked as answer by NorbertK Tuesday, September 17, 2013 8:06 AM
    Tuesday, September 17, 2013 8:06 AM

All replies

  • You can get the initParams in a more straight-forward fashion in the Application_Initialize() method:

    public partial class Application
    {
        partial void Application_Initialize()
        {
            IDictionary<string, string> initParams = null;
            Dispatchers.Main.Invoke(() =>
            {
                initParams = System.Windows.Application.Current.Host.InitParams;
            });
    
            string sessionId = null;
            if (initParams.TryGetValue(sessionId, out sessionId))
                this.SessionId = sessionId;
        }
    
        public string SessionId { get; private set; }
    }

    No need to change any of the manifest or configuration files.

    Justin Anderson, LightSwitch Development Team

    • Marked as answer by NorbertK Tuesday, September 17, 2013 8:07 AM
    Tuesday, September 17, 2013 6:05 AM
    Moderator
  • Dear Justin,

    Thanxs for your reply.

    This got me on the road again!

    Best regards,

    Norbert Kramer

    • Marked as answer by NorbertK Tuesday, September 17, 2013 8:06 AM
    Tuesday, September 17, 2013 8:06 AM