【问题标题】:Windows Phone 8 unable to call a WCF Service from App.xaml.csWindows Phone 8 无法从 App.xaml.cs 调用 WCF 服务
【发布时间】:2014-07-06 10:58:20
【问题描述】:

我有一个简单的 Windows Phone 8 应用程序。当应用程序启动时,我需要对用户进行身份验证,进行服务器调用。在 InitializePhoneApplication() 中,我的 WP8 应用程序检查此数据是否已在之前的调用中获得并存储在 IsolatedStorage 中。如果隔离存储中没有数据,则执行服务器调用。代码:

if ((IsolatedStorageSettings.ApplicationSettings.Contains("loggedin")) && 
    (Convert.ToString(IsolatedStorageSettings.ApplicationSettings["loggedin"]).ToLower() == "y"))
{
    RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
else
{
    SC = new ServiceClient();
    SC.CheckDeviceCompleted += SC_CheckDeviceCompleted;
    SC.CheckDeviceAsync(Utility.GetStatus());
}

这里是 SC_CheckDeviceCompleted() 的代码。当 CheckDeviceAsync() 返回时引发此事件(在服务上):

private void SC_CheckDeviceCompleted(object sender, CheckDeviceCompletedEventArgs e)
{
    string res = e.Result;
    if (!res.Equals(""))
    {
        IsolatedStorageSettings.ApplicationSettings["loggedin"] = "y";
        IsolatedStorageSettings.ApplicationSettings["id"] = res;
        RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
    }
    else
    {
        RootFrame.Navigate(new Uri("/Authentication.xaml", UriKind.Relative));
    }
}

从服务器接收到的数据用于选择应用程序的起始页。如果接收到空字符串,则服务器上没有注册任何数据:因此,用户必须登录。否则,用户可以立即访问应用程序的主页。

问题是这段代码不起作用。具体来说,服务端没有收到来自服务的调用(服务没有收到任何东西),所以永远不会执行 SC_CheckDeviceCompleted() 方法(因为没有收到来自服务的任何东西)并且没有页面显示。

SC 实例在 App.xaml.cs 中定义为公共静态属性,App 类:

public static ServiceClient SC { get; set; }

当从我的 WP8 应用程序的任何其他页面(我使用本地 ServiceClient 变量而不是 App.xaml.cs 中定义的 ServiceClient 属性)调用我的服务时,我的服务运行良好。我无法使用其中定义的 ServiceClient 属性从 App.xaml.cs 文件中进行服务调用。为什么?有什么问题?

【问题讨论】:

    标签: c# wcf windows-phone-8 app.xaml wcfserviceclient


    【解决方案1】:

    App.xaml 的主要目的是保存资源和页面的一些生命周期事件,例如应用程序的启动和退出。您无法在其中进行服务调用。只需在主页中使用相同的逻辑执行此操作即可。

    【讨论】:

    • 感谢 Sajeetharan 的回答。我需要服务的响应来选择正确的页面(主页或身份验证页面)。有什么办法吗?
    • 在 Landing.xaml.cs 中创建一个页面说 Landing.xaml -> 调用服务,获取响应并根据响应将用户导航到所需的页面,希望它帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    相关资源
    最近更新 更多