如何在Silverlight XAML页面中创建框架到外部网页?

时间:2013-04-24 14:51:20

标签: c# silverlight xaml frame

我对.NET很陌生,尝试用一个200x200框架的简单XAML网页,里面有Google。我有一个XAML页面,如下所示:

    <UserControl x:Class="Oleo.CI.Content.IU.Form.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    xmlns:telerikInput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
    xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
    xmlns:uiControl="clr-namespace:Oleo.CI.Contenidos.IU.Control;assembly=Oleo.CI.Contenidos.IU.Control"
    mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="1002" Width="1002" Height="812" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0 10 18 -10">
          <navigation:Frame x:Name="Cont" Source="http://www.google.com" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Width="200" Height="200"/>
          <TextBlock Text="Oferta" Grid.Column="3" Grid.Row="2"  HorizontalAlignment="Right" />
    </StackPanel>
 </UserControl>

我尝试过两种导航:Frame和sdk:框架但框架没有显示在带有该代码的页面上(其他控件如TextBlocks的RadGrids可以正常工作)。我不确定我做错了什么,有人可以对我有所了解吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您无法在Silverlight框架内打开网页。 Frame类表示支持在 Silverlight页面之间导航的控件。 通过处理Frame的NavigationFailed事件,您可以看到导航失败。

<navigation:Frame x:Name="Cont" Source="http://www.google.com" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Width="200" Height="200"
            NavigationFailed="Cont_NavigationFailed"/>

它将抛出异常消息: 无法加载URI的内容。 URI可能无效。

<强>更新: 您可以使用Html中的iframe标记来实现目标。

这是你要做的: 在创建Silverlight应用程序时,Visual Studio会在您的解决方案中创建两个项目。一个用于Silverlight应用程序,它生成.xap文件和一个简单的Asp.Net网站,默认包含TestPage.aspx。您的.xap文件包含在此网页中,.xap的高度和宽度设置为100%。当然,您可以根据需要进行操作。目前我已将其设置为高度和宽度的50%。如果您要在浏览器中右键单击此部件上的鼠标,您将看到Silverlight上下文菜单。在您的Web应用程序的其他部分中,您将看到Html Web页面的默认上下文菜单。 因此,您必须将iframe标记添加到该页面。

    <iframe src="http://www.msdn.com" height="700px" width="700px"></iframe>
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
            width="50%" height="50%">
            <param name="source" value="ClientBin/SilverlightApplication1.xap" />
            <param name="onError" value="onSilverlightError" />
            <param name="background" value="white" />
            <param name="minRuntimeVersion" value="5.0.61118.0" />
            <param name="autoUpgrade" value="true" />
            <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/?LinkId=161376" 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>

但是,我必须说你无法在iframe中打开谷歌网站。因为谷歌阻止了iframe。