[UWP]在uwp C#中发布移动应用程序

时间:2016-01-10 09:39:33

标签: c# uwp windows-10-mobile

我在Windows 10移动设备门户中发现了一些允许在用户手机上运行应用程序的新API。 您可以启动它以查看结果:http:// {PhoneIP} / api / app / packagemanager / packages 还有另一个API来启动应用程序: API /任务管理器/应用

启动现代应用

HTTP动词:POST

参数

appid:app的PRAID启动,hex64编码

package:应用程序包的全名,hex64编码

我有这个代码来运行应用程序但是没有任何想法吗?

public class PackageInfoToRun
{
    public string appid { get; set; }
    public string package { get; set; }
    public string PackageFamilyName { get; set; }
}

public class PhoneInstalledPackages
    {
        public Installedpackage[] InstalledPackages { get; set; }
    }

    public class Installedpackage
    {
        public string Name { get; set; }
        public string PackageFamilyName { get; set; }
        public string PackageFullName { get; set; }
        public int PackageOrigin { get; set; }
        public string PackageRelativeId { get; set; }
        public bool IsXAP { get; set; }
    }

private static string Encode(string strn)
    {
        var toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(strn);
        string appName64 = System.Convert.ToBase64String(toEncodeAsBytes);
        appName64 = appName64.Replace(" ", "20%");
        return appName64;
    }

public async Task<PhoneInstalledPackages> GetInstalledApps()
    {
        //  /api/app/packagemanager/packages
        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/api/app/packagemanager/packages");
        string res = "";
        webrequest.Method = "GET";
        try
        {
            using (var webresponse = await webrequest.GetResponseAsync())
            using (StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream()))
            {
                res = loResponseStream.ReadToEnd();
            }
            var des = JsonConvert.DeserializeObject<PhoneInstalledPackages>(res);
            return des;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

public async Task<bool> RunAppAsync(string appid, string packagename)
    {
        HttpResponseMessage http = new HttpResponseMessage();
        string str;
        try
        {
            var package = new PackageInfoToRun()
            {
                appid = Encode(appid),
                package = Encode(packagename)
            };
            using (HttpClient client = new HttpClient())
            {
                var serial = JsonConvert.SerializeObject(package);
                http = await client.PostAsync("http://127.0.0.1/api/taskmanager/app", new StringContent(serial, Encoding.UTF8, "application/json"));
                using (HttpResponseMessage response = http)
                {
                    str = await response.Content.ReadAsStringAsync();
                    string retur = str;
                    if (retur.Contains("true"))
                    {
                        return true;
                    }
                    return false;
                }
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

我的代码出了什么问题?谢谢:))

1 个答案:

答案 0 :(得分:0)

我使用您的代码进行了一些小改动,它对我有用。 现在RunAppAsync看起来像这样。不要忘记指定 PORT

HttpClient client = new HttpClient();           
var serial = JsonConvert.SerializeObject(package);
var result = await client.PostAsync(String.Format("http://127.0.0.1:*PORT*/api/taskmanager/app?appid={0}&package={1}", package.appid, package.package), new StringContent(serial));

包裹在哪里

var package = new PackageInfoToRun()          
{
    appid = Encode(app.PackageRelativeId),
    package = Encode(app.PackageFullName),
};

app变量是InstalledPackage类的实例。