2012-05-02 2 views
0

html로 실버 라이트 컨트롤을 숨기려고합니다. 문제는 숨기기 코드가 Silverlight 컨트롤에 의해 호출 된 경우입니다. 다시 표시 한 후에 Silverlight가 다시로드되고 Silverlight 컨트롤의 모든 데이터가 손실됩니다.Silverlight 컨트롤의 부모 노드의 가시성을 변경하고 실버 라이트 컨트롤이 다시로드되었습니다.

다음은 문제를 보여줄 수있는 간단한 샘플입니다. 실버 제어 : 에서 MainPage.xaml

<Grid x:Name="LayoutRoot" Background="White"> 
    <Button Content="Hide" Height="23" HorizontalAlignment="Left" Margin="118,210,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
    <sdk:Label Height="28" HorizontalAlignment="Left" Margin="44,92,0,0" Name="label1" VerticalAlignment="Top" Width="323" /> 
</Grid> 

MainPge.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Browser; 

namespace SilverlightApplication1 
{ 
public partial class MainPage : UserControl 
{ 
    [ScriptableMember()] 
    public event EventHandler TestEvent; 

    public string timeStr; 

    public MainPage() 
    { 
     InitializeComponent(); 
     this.label1.Content = DateTime.Now.ToShortTimeString(); 
     HtmlPage.RegisterScriptableObject("Control", this); 

     timeStr = DateTime.Now.ToString(); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     TestEvent(sender, new TestEventArgs()); 
    } 

    [ScriptableMember] 
    public string GetTime() 
    { 
     return timeStr; 
    } 
} 

public class TestEventArgs : EventArgs 
{ 
    [ScriptableMember()] 
    public string Name { get; set; } 

    public TestEventArgs() { 
     this.Name = "Test"; 
    } 
} 
} 

test.html를

function show() { 
document.getElementById("silverlightControlHost").style.visibility = "visible"; 
//document.getElementById("silverlightControlHost").style.display = ""; 
} 

    function hide() { 
     document.getElementById("silverlightControlHost").style.visibility = "hidden"; 
     //document.getElementById("silverlightControlHost").style.display = "none"; 
    } 

    function showTime() { 
     var timeStr = app.Content.Control.GetTime(); 
     document.getElementById("timeDiv").innerHTML = timeStr; 
    } 

    var app; 
    function silverlightOnLoad() { 
     alert("load"); 
     app = document.getElementById("silverlightApp"); 
     app.Content.Control.TestEvent = function (sender, arg) { 
      //if you keep these two alert statement. while the first one works fine,  //you can see the second one would 
      //throw an error after the silverlight control is hidden. 
      // alert(arg.Name); 
      hide(); 
      //alert(arg.Name); 
     }; 
    } 

<input type="button" id="btnShow" value="Show" onclick="show();" /> 
<input type="button" id="btnHide" value="Hide" onclick="hide();" /> 
<input type="button" id="Button1" value="Show Time" onclick="showTime();" /> 
<div id="timeDiv"> 

</div> 
<div id="silverlightControlHost"> 
    <object id="silverlightApp" data="data:application/x-silverlight-2," type="application/x-silverlight-2" 
     width="100%" height="100%"> 
     <param name="source" value="ClientBin/SilverlightApplication1.xap" /> 
     <param name="onLoad" value="silverlightOnLoad" /> 
     <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> 

HTML에서 "닫기"버튼으로 실버 제어를 숨기면 , 모두 잘 작동합니다. Silverlight 컨트롤에서 버튼으로 컨트롤을 숨기면 "표시"버튼을 클릭 할 때 silverlightOnLoad가 다시 호출됩니다. 어떤 도움이 필요합니까?

답변

0

http://forums.silverlight.net/p/17061/57793.aspx과 유사합니다.

간단히 말해서 플러그인에 포커스가 있고 숨기려고하면 Silverlight 플러그 인이 종료됩니다. 플러그를 숨기기 전에 window.focus();에 전화를 걸거나 (HTML 페이지의 다른 포커스를 설정할 수있는 요소에 포커스를 설정하십시오)

app.Content.Control.TestEvent = function (sender, arg) { 
    window.focus(); 
    hide(); 
}; 
관련 문제