6/21/2011

Pass parameters to a WPF Click-Once Application

Introduction


Environment : Visual Studio 2010

Application   : WPF

Language     : VB.NET

Deployment  : Click-Once

Suppose you have a WPF application which has 5 XAML windows.

1. Main Window

2. Application 1

3. Application 2

4. Application 3

5. Application 4

When application get launched, the Main Window will open which will have a combo-box containing the Application's Name to select.


So any user can go from Main Window to App1, App2, App3 or App4.

Once deployed, the URL will be something like, http://www.servername.com/MainWindow.application

Problem :


But the problem is if a user wants to directly open App1, App2, App3 or App4, he/she won't be able to.

Solution :


The solution is simple.  Pass the parameters to the URL of Click-Once and force it to recognize the parameters and open up the application.

The ClickOnce application named MainWindow that you host on servername, and you want to pass in a value for the variable username when the application launches. Your URL might look like the following:

http://www.servername.com/MainWindow.application?AppID=App1

Steps To Be Followed :

A.  If you are using the application's config file then follow the below procedure:
Steps :

1. Open your project (In Visual Studio 2010)

2. Right click on your project and select Properties:



3.  Select Publish Tab and Select Options.





4. Select the Allow URL parameters to be passed to application check box.







B.  If you are using any ClickOnce.target file:




Add Below Entry:




    <GenerateDeploymentManifest

EntryPoint="$(ClickOnceInstallDirectory)$(AssemblyName).exe.manifest"

DeploymentUrl ="$(ClickOnceApplicationUrl)"

OutputManifest="$(ClickOnceInstallDirectory)$(AssemblyName).application"

AssemblyName="$(ClickOnceAppTitle)"

MapFileExtensions="true"

Publisher="$(Company)"

AssemblyVersion="$(AssemblyVersion)"

MinimumRequiredVersion="$(AssemblyVersion)"

Product="$(ClickOnceAppTitle)"

Install="true"

UpdateEnabled="true"

UpdateMode="Foreground"

TargetFrameworkVersion="v4.0"

TargetFrameworkMoniker=".NETFramework,Version=v4.0"

                TrustURLParameters="true"

>


Code :

Write below code in your MainWindow.xaml.cs/vb file:



    Public Sub New()

        InitializeComponent()

            Dim instance As ApplicationDeployment

            If (ApplicationDeployment.IsNetworkDeployed) Then

instance = ApplicationDeployment.CurrentDeployment

                If Not IsNothing(instance) Then

                    Dim activateUri As Uri = instance.ActivationUri

                    If activateUri IsNot Nothing AndAlso activateUri.Query <> String.Empty Then

                        Dim nameValueTable = System.Web.HttpUtility.ParseQueryString(activateUri.Query)

                        If nameValueTable Is Nothing Then

                            'nameValueTable Is Nothing

                        Else

                            If nameValueTable.Keys.Count = 0 Then

                            'No Parameters has been passed.

                            Else

                            Select Case nameValueTable("ID")

                                Case "1"

'Open App1

                                Case "2"

'Open App2

                                Case "3"

'Open App3

                                Case "4"

'Open App4

                                Case Else

'Open Main Window

                            End Select

                            End If

                        End If

                    End If

                End If

            End If

    End Sub

Conclusion


Please let me know if you need any help.

0 comments: