javascript获得silverlight对象的方法(转)

1 如果要获得的对象就是产生事件的对象,则最简单。正如QuickStarts中列举的那样
<TextBlock Text="click me"  FontSize="50"
MouseLeftButtonDown="changelocation" />
</Canvas>
function changelocation(sender, args) {
sender["Canvas.Top"] = 70;
}
2 如果要获得对象包含的子元素对象,则可使用findName方法
<Canvas
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Transparent"
MouseLeftButtonDown="changeEllipseColor">
<TextBlock Text="click me"  FontSize="50"/>
<Ellipse x:Name="myEllipse"
Height="200" Width="200"
Canvas.Left="30" Canvas.Top="80"
Stroke="Black" StrokeThickness="10" Fill="LightBlue"/>
</Canvas>
function changeEllipseColor(sender, args) {
sender.findName("myEllipse").Fill = "red";
}
但这个方法似乎只能通过父对象获取子对象,可能是我还没有完全搞对,欢迎指正。
3 如果要通过一个对象或的其他非子对象,可使用getHost()
function OnPlay(sender,arg)
{   
     var host = sender.getHost();
     var otherObj = host.content.findName('otherName');
}
4 如果连一个对象也没有,则先用document.getElementById()方法获得host对象。
 var host = document.getElementById(hostID');
 var  obj = host.content.findName('objID');
注意hostID是在CreateSilverlight函数中指定的
function createSilverlight()
{
  Silverlight.createObjectEx({
   source: "TextContent.aspx",
   parentElement: document.getElementById("SilverlightControlHost"),//不是这个
   id: "SilverlightControl",//就是这个
   properties: {
    width: "80%",
    height: "24",
    version: "1.1",
    enableHtmlAccess: "true"
   },
   events: {}
  });
posted @ 2009-12-21 16:48  3.mu  阅读(242)  评论(0编辑  收藏  举报