我正在创建我的第一个 Windows 商店应用程序,并且在启动期间我有几个 Web 服务调用,并且还定期通过该应用程序调用。但是我注意到当我因为网络服务调用而无法访问互联网时,我的应用程序将无法启动/崩溃或只是关闭。我希望我的应用程序以一些初始数据以正常方式启动,即使没有互联网访问也看起来正常。我从 web 服务获得的数据主要是我在各种文本框和图表中显示的天气数据。
下面的代码在我的扩展初始屏幕中显示了 Web 服务调用。
public sealed partial class ExtendedSplashScreen : Page
{
//parameterItem max1DayAgo = new parameterItem();
//parameterItem min1DayAgo = new parameterItem();
public ExtendedSplashScreen()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
string[] periodSelector = { "1DayAgo", "1WeekAgo", "1MonthAgo" };
string[] modeSelector = { "max", "min" };
string[] parameterSelector = { "umtTemp1", "umtWindSpeed", "umtAdjBaromPress", "umtRainRate" };
//Create a webservice object
ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();
//First we create an object that holds max data for yesterday
var getMax1DayAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[0], modeSelector[0]);
//create an object that holds min data for yesterday
var getMin1DayAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[0], modeSelector[1]);
//Save arrayOfValue and arrayOfUnit to a parameterItem object. these objects are created during startup
// and the can be accessed and updated by all methods in this page later we will see that maxMinButton_Click method
//for the maxMinButton will use these data
//create an object that holds max data for last week
var getMax1WekAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[1], modeSelector[0]);
//create an object that holds min data for last week
var getMin1WekAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[1], modeSelector[1]);
//create an object that holds max data for last month
var getMax1MonthAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[2], modeSelector[0]);
//create an object that holds min data for last month
var getMin1MonthAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[2], modeSelector[1]);
(App.Current as App).max1DayAgo.arrayOfValue = getMax1DayAgoObj.arrayOfValue;
(App.Current as App).max1DayAgo.arrayOfUnit = getMax1DayAgoObj.arrayOfUnit;
(App.Current as App).min1DayAgo.arrayOfValue = getMin1DayAgoObj.arrayOfValue;
(App.Current as App).min1DayAgo.arrayOfUnit = getMin1DayAgoObj.arrayOfUnit;
(App.Current as App).max1WeekAgo.arrayOfValue = getMax1WekAgoObj.arrayOfValue;
(App.Current as App).max1WeekAgo.arrayOfUnit = getMax1WekAgoObj.arrayOfUnit;
(App.Current as App).min1WeekAgo.arrayOfValue = getMin1WekAgoObj.arrayOfValue;
(App.Current as App).min1WeekAgo.arrayOfUnit = getMin1WekAgoObj.arrayOfUnit;
(App.Current as App).max1MonthAgo.arrayOfValue = getMax1MonthAgoObj.arrayOfValue;
(App.Current as App).max1MonthAgo.arrayOfUnit = getMax1MonthAgoObj.arrayOfUnit;
(App.Current as App).min1MonthAgo.arrayOfValue = getMin1MonthAgoObj.arrayOfValue;
(App.Current as App).min1MonthAgo.arrayOfUnit = getMin1MonthAgoObj.arrayOfUnit;
string[] startupData = new string[13];
startupData[0] = " " + (App.Current as App).max1DayAgo.arrayOfValue[0] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[0]; // maxTemp
startupData[1] = " " + (App.Current as App).max1DayAgo.arrayOfValue[1] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[1]; // maxWindSped
startupData[2] = " " + (App.Current as App).max1DayAgo.arrayOfValue[2] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[2]; // maxAirPressure
startupData[3] = " " + (App.Current as App).max1DayAgo.arrayOfValue[3] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[3];// maxRainfall
startupData[4] = " " + (App.Current as App).min1DayAgo.arrayOfValue[0] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[0]; // minTemp
startupData[5] = " " + (App.Current as App).min1DayAgo.arrayOfValue[1] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[1];// minWindSped
startupData[6] = " " + (App.Current as App).min1DayAgo.arrayOfValue[2] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[2];// minAirPressure
startupData[7] = " " + (App.Current as App).min1DayAgo.arrayOfValue[3] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[3];// minRainfall
// Main fields
// ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();
var getLatestTempObj = await webServiceObj.GetLatestDataAsync("umtTemp1");
var getLatestWindObj = await webServiceObj.GetLatestDataAsync("umtWindSpeed");
var getLatestwindDirObj = await webServiceObj.GetLatestDataAsync("umtAdjWinDir");
var getLatestairPressureObj = await webServiceObj.GetLatestDataAsync("umtAdjBaromPress");
startupData[8] = " " + getLatestTempObj.Value + " " + getLatestTempObj.Unit;//temperatureMainTxtBlock.Text
startupData[9] = " " + getLatestWindObj.Value + " " + getLatestWindObj.Unit;//temperatureMainTxtBlock.Text
startupData[10] = "" + getLatestwindDirObj.Value; //temperatureMainTxtBlock.Text
startupData[11] = " " + getLatestairPressureObj.Value + " " + getLatestairPressureObj.Unit;//temperatureMainTxtBlock.Text
startupData[12] = "Last update: " + getLatestwindDirObj.Timestamp;//temperatureMainTxtBlock.Text
//save the startup data to the global variables
(App.Current as App).NavigateData = startupData;
this.Frame.SetNavigationState(e.Parameter as string);
this.Frame.Navigate(typeof(MainPage));
}
}