7

我编写了一个小型 .NET 控制台程序,它将启动 Windows 8 模拟器。非常简单:

using Microsoft.SmartDevice.Connectivity;
using Microsoft.SmartDevice.Connectivity.Interface;
using Microsoft.SmartDevice.MultiTargeting.Connectivity;

MultiTargetingConnectivity connectivity = new MultiTargetingConnectivity(CultureInfo.CurrentUICulture.LCID);
var devices = connectivity.GetConnectableDevices();
ConnectableDevice connectableDevice = devices[2];
Console.WriteLine("Found Connectable Device \'" + connectableDevice.Name + "\' for Device id {" + connectableDevice.Id + "}.");

很简单。但是,我现在要做的是以编程方式与设备交互。我知道我可以使用 iDevice.installApplication 启动自己的应用程序,但我真正想做的是运行模拟器附带的内置应用程序(邮件应用程序)。我可以使用这些SmartDevice.Connectivity库来发送触摸,或启动GetInstalledApplications()方法中未显示的“隐藏”应用程序吗?API 很稀疏,似乎没有很多开发人员在使用它。

4

1 回答 1

1

我实际上找到了一个比框架更好的SmartDevice框架。在该C:\Program Files (x86)\Microsoft XDE\8.0文件夹中,您可以找到Microsoft.XDE有助于为 Windows 8 模拟器包装器/皮肤提供动力的 DLL,并且是允许您与模拟器交互的托管库。示例代码:

private static void BootViaXdeLibs()
{
    var factory = new Microsoft.Xde.Wmi.XdeWmiFactory();
    Console.WriteLine("Polling for virtual machines");

    var virtualMachine = factory.GetAllXdeVirtualMachines(SettingsOptions.None)[2];
    Console.WriteLine("Found machine {0}", virtualMachine.Name);

    if (virtualMachine.EnabledState != VirtualMachineEnabledState.Disabled)
    {
        Console.WriteLine("Virtual Machine {0} is already running. Shutting down. ", virtualMachine.Name);
        virtualMachine.Stop();
        while (virtualMachine.EnabledState != VirtualMachineEnabledState.Disabled)
        {
            Thread.Sleep(1000);
        }
    }

    Console.WriteLine("Starting {0}", virtualMachine.Name);

    virtualMachine.Start();
    while (virtualMachine.EnabledState == VirtualMachineEnabledState.Starting)
    {
        Thread.Sleep(1000);
    }

    Console.WriteLine("Sleeping before image capture to give boot time");
    Thread.Sleep(30000);

    //Click on the screen
    virtualMachine.SendMouseEvent(new MouseEventArgs(MouseButtons.Left, 1, 295, 260, 0));
    Thread.Sleep(100);
    virtualMachine.SendMouseEvent(new MouseEventArgs(MouseButtons.None, 0, 295, 260, 0));
    Thread.Sleep(1000);

    Console.WriteLine("Saving screenshot");

    //Capture Screen
    var res = virtualMachine.GetCurrentResolution();
    var image = virtualMachine.GetScreenShot(0, 0, res.Width, res.Height);
    image.Save("C:\\image.png", ImageFormat.Png);

    virtualMachine.Stop();
}
于 2013-11-07T17:16:16.423 回答