- 1 year ago
How to pass args to a UWP Application with EntryPoint="Windows.FullTrustApplication" in AppxManifest.xml
In a UWP application with EntryPoint="Windows.FullTrustApplication"
specified in the AppxManifest.xml
, you can pass command-line arguments to the application using the ActivationArguments
property of the App
object. The ActivationArguments
property provides access to the command-line arguments passed to the application during activation.
Here's how you can access the command-line arguments in a UWP application:
Open the
App.xaml.cs
file in your UWP project.In the
OnLaunched
method, you can access the command-line arguments using theActivationArguments
property of theApp
object.The
ActivationArguments
property returns an instance of theIActivatedEventArgs
interface. You can check if the activation type is of typeLaunchActivatedEventArgs
and then access the command-line arguments from theArguments
property of theLaunchActivatedEventArgs
object.
Here's an example of how to access the command-line arguments:
csharpusing Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace YourUWPAppNamespace
{
sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
if (e.Arguments != null)
{
// Access the command-line arguments passed to the application
string commandLineArgs = e.Arguments;
// Process the command-line arguments as needed
}
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
// Save app state and stop any background activity
}
}
}
In this example, we access the command-line arguments using e.Arguments
and store them in the commandLineArgs
variable. You can then process these command-line arguments as needed within your application.
Please note that the EntryPoint="Windows.FullTrustApplication"
attribute in the AppxManifest.xml
enables your UWP application to run as a full-trust desktop application, allowing it to access the command-line and other capabilities not available to typical UWP applications. However, keep in mind that this type of entry point requires specific permissions during the application's installation and may not be appropriate for all scenarios.