2010-07-12 65 views

回答

3

Application.Exit事件添加事件处理程序。在该处理程序中调用WebService。在XAML /代码看起来是这样的:

<Application 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="SilverlightApplication.App" 
    Exit="App_Exit"> 

</Application> 

而且

public partial class App : Application 
{ 
    private void App_Exit(object sender, EventArgs e) 
    { 
     // Code to call WebService goes here. 
    } 
} 
+0

在web服务异步方法。例如:方法save()。在服务saveAsync();和saveCompleted()。 saveAsync是执行,但如果使用您的答案saveCompleted不执行。 – isxaker 2010-07-12 13:34:15

+0

@Mikhail你的意思是说你没有收到回调事件?由于它是异步的,这可能是因为你的应用在服务返回之前已经关闭。你需要回报价值吗? – 2010-07-12 14:09:39

+0

确切地说是...... – isxaker 2010-07-12 14:29:27

0

查看贾斯汀Niessner的答案的评论:你不能取回返回值。如果您所调用的服务不是关键性的(因为我们假设它只是捕获一些使用情况统计信息),那对您来说可能是正确的。如果您需要任何情况下都有返回值,并且您希望多次使用SL应用程序,则可以将记事本写入IsolatedStorage(即同步操作),并在应用程序启动时将其发布到服务器下次。

2

您无法在Silverlight中关闭应用程序的Web请求。

2

我有一个应用程序需要在关闭前保存信息。我在托管Silverlight控件的页面中使用了javascript。

Javascript和使用

<script type="text/javascript"> 
    var blocking = true; 

    function pageUnloading() { 
     var control = document.getElementById("Xaml1"); 
     control.content.Page.FinalSave(); 
     while (blocking) 
      alert('Saving User Information'); 
    } 

    function allowClose() { 
     blocking = false; 
    } 
</script> 


<body onbeforeunload="pageUnloading();"> 

</body> 

和 app.xaml.cs

public partial class App : Application 
{ 

    [ScriptableMember()] 
    public void FinalSave() 
    { 

     srTL.TrueLinkClient proxy = new CSRM3.srTL.TrueLinkClient(); 
     proxy.DeleteAllUserActionsCompleted += (sender, e) => 
      { 
       HtmlPage.Window.CreateInstance("allowClose"); 


      }; 
     proxy.DeleteAllUserActionsAsync(ApplicationUser.UserName); 

    } 

} 
+0

javascript while while block在不同的浏览器上引发警报,运行的脚本失去控制......除此之外,这似乎工作 – Rumplin 2011-03-23 07:43:11

0

是的,只是做的Web服务调用,不等待返回值..因为它永远不会到达

所以做到这一点:

private async void Application_Exit(object sender, EventArgs e) 
    { 
     // Tell DBSERVER_V14 pipe we have gone away 
     await connect_disconnect_async(MainPage.username, MainPage.website, false); 
    } 

但不这样做:

private async void Application_Exit(object sender, EventArgs e) 
    { 
     // Tell DBSERVER_V14 pipe we have gone away 
     var status = await SmartNibby_V13.connect_disconnect_async(MainPage.username, MainPage.website, false); 
     if (status) 
     { 
      Console.WriteLine(status); 
     } 
    } 

,因为你永远不会有一个“状态”值与测试。