Pass parameters to a WPF Click-Once Application

Naimishforu
Posted by in WPF category on for Intermediate level | Points: 250 | Views : 20721 red flag
Rating: 5 out of 5  
 1 vote(s)

We will see how we can pass parameters to a WPF Click-Once Application and depends on the parameter we can use the 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("AppID")

        		                        Case "App1"

							'Open App1

                		                Case "App2"

							'Open App2	

        		                        Case "App3"

							'Open App3

                        		        Case "App4"

							'Open App4

		                                Case Else

							'Open Main Window

                	            End Select

                            End If

                        End If
            
                    End If

                End If

            End If

    End Sub

1.   If (ApplicationDeployment.IsNetworkDeployed) Then

This line checks if the application is being called from the Click-Once.

2. Dim activateUri As Uri = instance.ActivationUri

Here, activateUri gets the URL with Parameters.

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

The namValueTable will get the Parameters in NameValueCollection.

Check This Link for more information on NameValueCollection.

For Test, Pass the parameters as,

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


Hope this was useful, do let me know if you need any help.


Thanks

Page copy protected against web site content infringement by Copyscape

About the Author

Naimishforu
Full Name: John Doe
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India



Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)