c# - Windows store app crashes when no internet -
i creating first windows store app , have several web service calls during startup , periodically through app. have noticed app ot start/crashes or closes down when don't have internet access because of web serivice calls. want app start in normal way initial data , seem normal when there no internet access. data webservice weather data show in various textboxes , graphs.
the code below shows webservice calls in extended splash screen.
public sealed partial class extendedsplashscreen : page { //parameteritem max1dayago = new parameteritem(); //parameteritem min1dayago = new parameteritem(); public extendedsplashscreen() { this.initializecomponent(); } /// <summary> /// invoked when page displayed in frame. /// </summary> /// <param name="e">event data describes how page reached. parameter /// property typically used configure 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 webservice object servicereference.webservicesoapclient webserviceobj = new servicereference.webservicesoapclient(); //first create object holds max data yesterday var getmax1dayagoobj = await webserviceobj.getselectedmaxmindataasync(parameterselector, periodselector[0], modeselector[0]); //create object holds min data yesterday var getmin1dayagoobj = await webserviceobj.getselectedmaxmindataasync(parameterselector, periodselector[0], modeselector[1]); //save arrayofvalue , arrayofunit parameteritem object. these objects created during startup // , can accessed , updated methods in page later see maxminbutton_click method //for maxminbutton use these data //create object holds max data last week var getmax1wekagoobj = await webserviceobj.getselectedmaxmindataasync(parameterselector, periodselector[1], modeselector[0]); //create object holds min data last week var getmin1wekagoobj = await webserviceobj.getselectedmaxmindataasync(parameterselector, periodselector[1], modeselector[1]); //create object holds max data last month var getmax1monthagoobj = await webserviceobj.getselectedmaxmindataasync(parameterselector, periodselector[2], modeselector[0]); //create object holds min data last month var getmin1monthagoobj = await webserviceobj.getselectedmaxmindataasync(parameterselector, periodselector[2], modeselector[1]); (app.current app).max1dayago.arrayofvalue = getmax1dayagoobj.arrayofvalue; (app.current app).max1dayago.arrayofunit = getmax1dayagoobj.arrayofunit; (app.current app).min1dayago.arrayofvalue = getmin1dayagoobj.arrayofvalue; (app.current app).min1dayago.arrayofunit = getmin1dayagoobj.arrayofunit; (app.current app).max1weekago.arrayofvalue = getmax1wekagoobj.arrayofvalue; (app.current app).max1weekago.arrayofunit = getmax1wekagoobj.arrayofunit; (app.current app).min1weekago.arrayofvalue = getmin1wekagoobj.arrayofvalue; (app.current app).min1weekago.arrayofunit = getmin1wekagoobj.arrayofunit; (app.current app).max1monthago.arrayofvalue = getmax1monthagoobj.arrayofvalue; (app.current app).max1monthago.arrayofunit = getmax1monthagoobj.arrayofunit; (app.current app).min1monthago.arrayofvalue = getmin1monthagoobj.arrayofvalue; (app.current app).min1monthago.arrayofunit = getmin1monthagoobj.arrayofunit; string[] startupdata = new string[13]; startupdata[0] = " " + (app.current app).max1dayago.arrayofvalue[0] + " " + (app.current app).max1dayago.arrayofunit[0]; // maxtemp startupdata[1] = " " + (app.current app).max1dayago.arrayofvalue[1] + " " + (app.current app).max1dayago.arrayofunit[1]; // maxwindsped startupdata[2] = " " + (app.current app).max1dayago.arrayofvalue[2] + " " + (app.current app).max1dayago.arrayofunit[2]; // maxairpressure startupdata[3] = " " + (app.current app).max1dayago.arrayofvalue[3] + " " + (app.current app).max1dayago.arrayofunit[3];// maxrainfall startupdata[4] = " " + (app.current app).min1dayago.arrayofvalue[0] + " " + (app.current app).min1dayago.arrayofunit[0]; // mintemp startupdata[5] = " " + (app.current app).min1dayago.arrayofvalue[1] + " " + (app.current app).min1dayago.arrayofunit[1];// minwindsped startupdata[6] = " " + (app.current app).min1dayago.arrayofvalue[2] + " " + (app.current app).min1dayago.arrayofunit[2];// minairpressure startupdata[7] = " " + (app.current app).min1dayago.arrayofvalue[3] + " " + (app.current 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 startup data global variables (app.current app).navigatedata = startupdata; this.frame.setnavigationstate(e.parameter string); this.frame.navigate(typeof(mainpage)); } }
an approach have in of our team based apps prior call return data of net, network status checked. example:
async task refreshfromweb(...) { if (!app.hasinternetaccess) { await new windows.ui.popups.messagedialog(strings.nointernetwarning).showasync(); return; } //attempt access here } public static bool hasinternetaccess { { var profile = windows.networking.connectivity.networkinformation.getinternetconnectionprofile(); if (profile == null) return false; return profile.getnetworkconnectivitylevel() == windows.networking.connectivity.networkconnectivitylevel.internetaccess; } }
we took approach @ times similar uses await , returns true or false (the same done above, approach above gives dialog)
public static async system.threading.tasks.task hasinternet() { var profile = windows.networking.connectivity.networkinformation.getinternetconnectionprofile(); var hasnetaccess = profile != null; if (!hasnetaccess) await new windows.ui.popups.messagedialog( content: infohub.apphubviewmodel.strings.nointernetwarning, title: infohub.apphubviewmodel.strings.nointernetwarning).showasync(); return hasnetaccess; } async void yourcontrolevent_click(object sender, itemclickeventargs e) { //if net access, stuff, otherwise ignore if (await isinternet()) { //do net calls here } }
Comments
Post a Comment