2

I have a SPA built on AngularJS in witch I need to display a Silverlight media player. I need to pass the id number of the currently selected media file to the Silverlight app so it can fetch the information it needs to play it. First I tried using Silverlight's initParams as such:

<param name="initParams" runat="server" value="{{value}}"/>

After that failed I attempted to get the id from the URL and set it from the Silverlight apps onLoad function like this:

<script type="text/javascript">
    var pathArray = window.location.pathname.split('/');
    var value = pathArray[1];
    var slCtl = null;
    function pluginLoaded(sender, args) {
        slCtl = sender.getHost();
        slCtl.Content.SilverlightObject.SetMediaId(value);
    }
</script>

But I quickly realized that both the value from the AngularJS controller and the URL where set after the page had loaded. So when the Silverlight app loaded it didn't have the correct information.

Right now I think that the best way to solve this would be to create a AngularJS directive that creates the Silverlight application, since that will have access to the id.

Am I on the right path here, what would be the easiest way to solve this problem?

Edit: In one of my first attempts I attached som inline javacript function to Silverlights OnLoad

<param name="onLoad" value="pluginLoaded" />

That method then called a method on the Silverlight application passing in the value.

slCtl.Content.SilverlightObject.SetMediaId(value);

Would it be possible to call this method from AngularJS? Would I have to create a directive for this too, or could I do it from the controller? How should I access to the Silverlight object in my controller/directive?

4

2 回答 2

1

这是我们提出的最终解决方案。

<div class="ui-helper-clearfix no-left-padding">
    <form id="form1" runat="server" style="height: 500px">
        <div id="silverlightControlHost" style="height: 500px">

            <object id="MediaPlayer_8" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="800" height="500">
                <param name="source" value="../../../ClientBin/MediaPlayer.xap" />
                <param name="onError" value="onSilverlightError" />
                <param name="background" value="#f2f2f2" />
                <param name="minRuntimeVersion" value="5.0.61118.0" />
                <param name="autoUpgrade" value="true" />
                <param name="onLoad" value="pluginLoaded" />
                <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration: none">
                    <img src="http://go.microsoft.com/fwlink/?LinkUserId=161376" alt="Get Microsoft Silverlight" style="border-style: none" />
                </a>
            </object>
            <iframe id="_sl_historyFrame" style="border: 0px; height: 0px; visibility: hidden; width: 0px;"></iframe>
        </div>
    </form>
    <script type="text/javascript">
            var slCtl = null;
            function pluginLoaded(sender, args) {
                slCtl = sender.getHost();
                slCtl.Content.SilverlightObject.SetMediaId(@ViewBag.CurrentMedia);
            }
        </script>

关键是:

<param name="onLoad" value="pluginLoaded" />

<script type="text/javascript">
            var slCtl = null;
            function pluginLoaded(sender, args) {
                slCtl = sender.getHost();
                slCtl.Content.SilverlightObject.SetMediaId(@ViewBag.CurrentMedia);
            }
        </script>

在我们的silverlight应用程序中

[ScriptableMember]
    public void SetMediaId(int mediaId)
    {
        _viewedMediaId = mediaId;
        _proxy.GetMediaAsync(_viewedMediaId);
    }

希望这可以帮助。

于 2015-08-25T12:43:28.677 回答
0

每当您需要进行 DOM 操作时,都应该从指令中进行

于 2013-10-07T21:15:22.947 回答