【问题标题】:Open another application from the current one从当前应用程序打开另一个应用程序
【发布时间】:2019-08-01 23:52:14
【问题描述】:

我们在 Windows 应用商店中有一个 UWP 应用。通过这个应用程序,我们想在同一系统上启动各种应用程序。对于这个过程,我们需要做两件事。

  1. 检查系统上是否存在该应用
  2. 如果是,启动它。如果没有,请提供反馈

我们尝试了几件事,但我正在寻找最好的方法来做到这一点。 我们希望同时启动其他 UWP 应用和独立应用。

我尝试弄乱 Unity PlayerPrefs,但这很奇怪。如果我制作一个自定义 PlayerPref 并检查它是否存在于 1 个应用程序中,它会起作用,但是一旦我在 UWP 中制作了一个 playerpref 并在 Standalone 中检查它,我什么也得不到。当然反之亦然。 (是的,我知道 UWP 将其 playerprefs 保存在其他地方)

最好的通用解决方案是什么?继续摆弄 Playerprefs 并根据我们要打开的应用程序搜索不同的路径?(独立,UWP)或其他方式?

编辑:到目前为止我所拥有的:

        if (Input.GetKeyDown(KeyCode.Backspace))
    {
        PlayerPrefs.SetString("42069" , "testing_this");
        PlayerPrefs.Save();
        Debug.Log("Wrote key 42069 to registry with: -value testing_this-");
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (PlayerPrefs.HasKey("42069"))
        {
            Debug.Log("I found the key 42069 in my registry");
            cube.SetActive(true);
        }
        else
        {
            Debug.Log("I cant find key 42069 in my registry");
        }
    }

    if (Input.GetKeyDown(KeyCode.S))
    {
        const string registry_key = @"SOFTWARE\DefaultCompany";
        using(RegistryKey key = Registry.CurrentUser.OpenSubKey(registry_key))
        {
            if (key != null)
                foreach (string subKeyName in key.GetSubKeyNames())
                {
                    if (subKeyName == "RegistryTesting")
                    {
                        Debug.Log("I found the key on path: " + registry_key);
                    }
                }
        }
    }

编辑:没有人?我知道有办法。我需要做的就是检查 UWP 应用程序中是否存在独立应用程序。但我无法访问 UWP 应用程序中的寄存器。我知道有一些方法可以使用桥梁等,但我不知道如何以及从哪里开始。

【问题讨论】:

  • 如果您能发布您目前拥有的代码以便我们提出建议,将会很有帮助。
  • 对,我的错。我添加了它。这没什么,因为我刚刚开始测试,但我真的找不到合适的方法来做到这一点。现在,当我打开一个应用程序时,它可以工作,按退格键创建密钥,然后按空格键检查它是否存在。但是如果我用相同的代码打开一个 UWP 应用程序并按空格检查它是否存在,它就不起作用了。
  • 好的,我明白你要做什么了。我不相信你可以在这样的应用程序之间共享 Playerprefs。我相信您将不得不创建自己的机制,例如写入和读取注册表(即设置注册表项 = 1,然后从另一个应用程序读取相同的注册表项),写入具有已知位置的文本文件,读/写两个应用程序或类似的东西都可以访问的数据库。
  • 我明白了。太糟糕了。我会继续自己做更多的研究,否则我将不得不像你说的那样找到其他东西。感谢您的帮助。
  • 我也做了更多的研究,请参阅此处的帖子stackoverflow.com/questions/48899692/… 如果是这种情况,您最好的选择是从两个应用程序都可以访问的数据库中读取/写入。

标签: c# unity3d uwp


【解决方案1】:

我遇到了类似的情况,但我正在检查应用程序是否正在运行,如果没有,请启动它。在我的情况下,我想要检查和启动的应用程序不是我编写的,也不是 UWP,因此我的解决方案可能不适合您,因为这样做的功能受到限制。

首先在 package.appxmanifest(代码)中添加受限功能。

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap"

然后为应用添加“appDiagnostics”功能。

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="appDiagnostics" />
</Capabilities>

现在您可以请求访问正在运行的进程并进行检查的权限。

using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.System;
using Windows.System.Diagnostics;
class ProcessChecker
{
public static async Task<bool> CheckForRunningProcess(string processName)
    {
        //Requests permission for app.
        await AppDiagnosticInfo.RequestAccessAsync();
        //Gets the running processes.
        var processes = ProcessDiagnosticInfo.GetForProcesses();
        //Returns result of searching for process name.
        return processes.Any(processDiagnosticInfo => processDiagnosticInfo.ExecutableFileName.Contains(processName));
    }
}

启动非 UWP 应用/进程有点脏,但可以。

首先,需要一个简单的控制台(非 uwp)应用程序。将以下代码中的 directoryPath 替换为您适用的目录路径。

using System;
using System.Diagnostics;

namespace Launcher
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 3) return;
                string executable = args[2];
                string directoryPath = "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\";
                Process.Start(directoryPath + executable);
            }
            catch (Exception e)
            {
                Console.ReadLine();
            }

        }
    }
}

构建控制台应用并将 Launcher.exe 放置在 UWP 应用资产文件夹中。

现在您需要添加运行 Launcher 的功能,为此,请向 UWP 应用添加“runFullTrust”功能。

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="runFullTrust" />
<rescap:Capability Name="appDiagnostics" />
</Capabilities>

对于桌面,您还需要在 package.appxmanifest(代码)中添加桌面功能和扩展。

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap mp rescap"

然后在package.appxManifest的下方和里面。

<Extensions>
    <desktop:Extension Category="windows.fullTrustProcess" Executable="Assets\Launcher.exe" >
      <desktop:FullTrustProcess>
        <desktop:ParameterGroup GroupId="SomeGroup1" Parameters="ProcessName1.exe"/>
        <desktop:ParameterGroup GroupId="SomeGroup2" Parameters="ProcessName2.exe"/>
      </desktop:FullTrustProcess>
    </desktop:Extension>
</Extensions>

最后,添加您的应用版本所需的“UWP 的 Windows 桌面扩展”引用。

现在您可以调用 Launcher 并启动必要的过程。

public static async void LaunchProcess(int groupId)
    {
        switch (groupId)
        {
            case 1:
                await FullTrustProcessLauncher.LaunchFullTrustProcessForAppAsync("SomeGroup1");
                break;
            case 2:
                await FullTrustProcessLauncher.LaunchFullTrustProcessForAppAsync("SomeGroup2");
                break;
        }
    }

综合以上,一种可能是……

    public enum ProcessResult
        {
            ProcessAlreadyRunning,
            FailedToLaunch,
            SuccessfulLaunch
        }
    public static async Task<ProcessResult> CheckLaunchCheckProcess1()
        {
            if (await CheckForRunningProcess("ProcessName1.exe")) return ProcessResult.ProcessAlreadyRunning;
            LaunchProcess(1);
            return await CheckForRunningProcess("ProcessName1.exe") ? ProcessResult.SuccessfulLaunch : ProcessResult.FailedToLaunch;
        }

这只是一个示例,说明如何在单个 uwp 应用程序中启动非 uwp 应用程序。对于 Windows 商店应用提交,受限功能需要批准,如果被拒绝,可能会延迟或停止部署。

如果调用应用程序和启动应用程序都是 uwp 并由您编写,则适当的解决方案可能是使用 URI 进行应用程序之间的通信,MS doc 链接 Launch an app with a URI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多