从iframe中的silverlight应用获取父页面网址

时间:2010-08-25 03:32:28

标签: javascript silverlight iframe

我需要能够找出托管我的silverlight应用程序的页面。我可以使用System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsolutePath获取iframe的网址,但我想获取托管iframe的网页的网址。

System.Windows.Browser.HtmlPage.Document.DocumentElement.Parent为空。

谢谢!

1 个答案:

答案 0 :(得分:2)

有人给了我一个link,但他们的答案已经消失了?干杯无论是谁! :)

这就是我这样做的方式:

MainPage.xaml.cs中:

    public MainPage()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Page_Loaded);
        HtmlPage.RegisterScriptableObject("Page", this);
        HtmlPage.Window.Invoke("GetParentURL");
    }

   [ScriptableMember]
    public void GetParentURL(string result)
    {
        if (result.IndexOf("WhatIamLookingFor") > 0)
            imgLink.Visibility = Visibility.Visible;
        else
            imgLink.Visibility = Visibility.Collapsed;   
    }

Default.aspx的:

<script type="text/javascript">    
   function GetParentURL() {
       var control = document.getElementById("silverlightControlHost");
       control.children[0].Content.Page.GetParentURL(parent.location.href);
   }   
</script>

<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param name="source" value="/ClientBin/MyApp.Silverlight.xap"/>
      <param name="background" value="transparent" />
      <param name="minRuntimeVersion" value="3.0.40624.0" />
      <param name="autoUpgrade" value="true" />
      <param name="windowless" value="true" />
      <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
         <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
      </a>
    </object>
    <iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>