簡體   English   中英

在 UWP 應用程序中,讀取命令行 arguments 並將其從 App.xaml.cs 文件傳遞到 MainPage.Z44CC44B81911F4BA4581C27

[英]In UWP App, read command line arguments and pass it from App.xaml.cs file to the MainPage.xaml.cs

我可以從 windows 命令提示符或 PowerShell 啟動我的UWP應用程序。 我對如何閱讀傳遞給 UWP 應用程序的 arguments 也有了一些想法。 但是,我找不到任何關於如何將那些 arguments 從App.xaml.cs文件傳遞到MainPage.xaml.cs的文檔。

例如,您可以為您的 UWP 應用程序定義一個應用程序執行別名(如下所示),這樣您就可以從cmdpowershell輕松啟動它:

<Extensions>
    <uap5:Extension
      Category="windows.appExecutionAlias"
      StartPage="index.html">
      <uap5:AppExecutionAlias>
        <uap5:ExecutionAlias Alias="MyApp.exe" />
      </uap5:AppExecutionAlias>
    </uap5:Extension>
</Extensions>

然后,您可以從OnActivated事件中讀取 arguments,如下所示:

async protected override void OnActivated(IActivatedEventArgs args)
{
    switch (args.Kind)
    {
        case ActivationKind.CommandLineLaunch:
            CommandLineActivatedEventArgs cmdLineArgs = 
                args as CommandLineActivatedEventArgs;
            CommandLineActivationOperation operation = cmdLineArgs.Operation;
            string cmdLineString = operation.Arguments;
            string activationPath = operation.CurrentDirectoryPath;
……..
}

問題:從App.xaml.cs文件中的上述事件中,如何將字符串cmdLineString的值傳遞給MainPage.xaml.cs文件? 示例:我將Hello World傳遞給命令行。 App.xaml.cs文件讀取該參數。 現在通過我的代碼,我想將Hello World值傳遞給MainPage.xaml.cs文件,這樣我就可以將它分配給主 window 中的TextBlock.Text屬性。

環境VS2019 - ver 16.5.5Windows 10 Pro - 1903

在 UWP 中,參數通常通過導航在頁面之間傳遞。

這段代碼大致展示了OnActivated方法的結構:

protected override void OnActivated(IActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;

    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();
        rootFrame.NavigationFailed += OnNavigationFailed;
        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }
    string commandParam = string.Empty;
    switch (args.Kind)
    {
        case ActivationKind.CommandLineLaunch:
            //get command parameter
            break;
        default:
            //do other things...
            break;
    }
    if (rootFrame.Content == null)
    {
        rootFrame.Navigate(typeof(MainPage), commandParam);
    }
    // Ensure the current window is active
    Window.Current.Activate();
}

拿到命令行參數后,最重要的是在這行代碼中:

rootFrame.Navigate(typeof(MainPage), commandParam);

通過導航,我們可以將參數傳遞給MainPage

之后,我們需要在MainPage中接收參數並進行處理:

主頁.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if(e.Parameter!=null && e.Parameter is string commandParam)
    {
        TestTextBlock.Text = commandParam;
    }
    base.OnNavigatedTo(e);
}

有關導航的更多信息,您可以參考此文檔

App.xaml.cs中定義一個變量

sealed partial class App : Application
    {
        public string cmdLineArgs;

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }
..........
}

然后在OnActivated事件中設置cmdLineArgs的值。

然后你可以在項目的任何地方使用cmdLineArgs

string args = (Application.Current as App).cmdLineArgs;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM