Question

I'm making a Silverlight application and I'm using a MediaElement to play a video from the user's disk that I know the path of (say, "C:/foo.MOV"). I'd like a Javascript trigger from the browser to change the source of the MediaElement to another known file (eg "C:/bar.MOV"). I can make a button to do this in Silverlight, and I can have a Javascript trigger execute code inside the Silverlight app, but when I do, the MediaElement appears empty.

I've even tried having the Javascript call the btnLoadNewMediaTest_Click event, and while that event works fine called from user clicks on the button, it doesn't affect the media at all when called from outside the app.

Looking at the MediaElement in the debug, it seems that when it's called from the Javascript the MediaElement's Source appears as null and it seems to have made an empty copy.

I can confirm the Javascript is triggering the events in Silverlight, as it trips breakpoints in the Silverlight code.

Was it helpful?

Solution

I have managed to solve this: I created an EntryPoint class that is scriptable from JavaScript. When the JavaScript sendCommand is triggered, it puts a command and args into a queue held by the entry point. Every tick of a timer in the Silverlight app, the app checks the Count() of the queue and gets any commands and processes them.

From the Javascript, I call silverlightControl.Context.EntryPoint.setCommand("commandname", "args").

In the EntryPoint I have

[ScriptableMember()]
    public string setCommand(string commandValue, string argsValue)
    {
        commands.Enqueue(commandValue);
        args.Enqueue(argsValue);
        commandWaitingFlag = true;
        return Application.Current.HasElevatedPermissions.ToString();
    }

In the Silverlight itself, I have a DispatcherTimer with an interval of 100ms. This has a tick event:

    public void Each_Tick(object o, EventArgs e)
    {
        //Other code
        if (entryPoint.commandWaitingFlag)
        {
            handleEntryPointCommands();
        }
    }

From inside handleEntryPointCommands I call a method of the entryPoint, getCommand():

    public string[] getCommand() {

        string commandOut = string.Empty;
        string argsOut = string.Empty;
        if (commands.Count > 0)
        {
            commandOut = commands.Dequeue();
            argsOut = args.Dequeue();
            if (commands.Count == 0)
            {
                commandWaitingFlag = false;
            }
            return new string[2] { commandOut, argsOut };
        }
        else
        {
            return new string[2];
        }
    }

and then can use the command I've gotten however I like. Hopefully that's more helpful with some code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top