How Awesomium JSObject works I cannot find an example

I am using Awesomium WebControl on a C # form and I am trying to communicate information to the browser that the USB device has been connected. I have USB detection, but for some reason I cannot build JSObject()

with values ​​for every device connected to it.

Here is the code that runs when the device is connected to the computer.

JSObject js_obj;

DriveInfo[] allDrives = DriveInfo.GetDrives();

JSValue[] js_arr = new JSValue[allDrives.Count()];

int count = 0;

foreach (DriveInfo drive in allDrives)
{
    if (drive.IsReady == true)
    {
        if (drive.DriveType == DriveType.Removable)
        {
            js_obj = new JSObject();
            js_obj["Id"] = new JSValue("");
            js_obj["TimeAdded"] = new JSValue("");
            js_obj["DriveLetters"] = new JSValue(drive.VolumeLabel);
            js_obj["Port"] = new JSValue("");
            js_arr[count] = new JSValue(js_obj);
            count++;
        }
    }
}

this.Browser.CallJavascriptFunction("DiskDetector", "DeviceAdded", js_arr);

      

But when I add a breakpoint to this.Browser.CallJavascriptFunction("DiskDetector", "DeviceAdded", js_arr);

, js_arr is an array of 4 where all values ​​are null

I think the problem is that I am using JSObject and JSObject Extends IDisposable parameter values, and I, although I have to add all ("key", "value"); but the method is not used for me

+3


source to share


1 answer


USB drives can sometimes have DriveType

DriveType.Fixed

, but not DriveType.Removable

.


Just to make sure I wasn't missing anything, I also took your code (minus your condition Removable

and referencing drive.Name

instead drive.VolumeLabel

to get the actual drive letter) to see if I could get it to work:

JSObject js_obj;

DriveInfo[] allDrives = DriveInfo.GetDrives();

JSValue[] js_arr = new JSValue[allDrives.Count()];

int count = 0;

foreach (DriveInfo drive in allDrives)
{
    if (drive.IsReady == true)
    {
        js_obj = new JSObject();
        js_obj["Id"] = new JSValue("");
        js_obj["TimeAdded"] = new JSValue("");
        js_obj["DriveLetters"] = new JSValue(drive.Name);
        js_obj["Port"] = new JSValue("");
        js_arr[count] = new JSValue(js_obj);
        count++;
    }
}

foreach (var js_val in js_arr)
{
    if (js_val == null)
        continue;

    Trace.WriteLine(js_val.GetObject()["DriveLetters"].ToString());
}

      



And it outputs:

C:\
S:\
T:\
Y:\
Z:\

      

As I expected. I'm still not sure why it doesn't work for you, but it proves the correct use JSObject

.

+5


source







All Articles