Utilities.cs CampusAppWP8::Utility::Utilities CampusAppWP8::Utility System::Windows::Media //----------------------------------------------------------------------------- //<copyrightfile="Utilities.cs"company="BTU/IIT"> //Companycopyrighttag. //</copyright> //<author>fiedlchr</author> //<sience>16.07.2013</sience> //----------------------------------------------------------------------------- namespaceCampusAppWP8.Utility { usingSystem; usingSystem.Collections.Generic; usingSystem.Device.Location; usingSystem.Globalization; usingSystem.Linq; usingSystem.Threading; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Media; usingCampusAppWP8.Resources; publicstaticclassUtilities { privatestaticManualResetEventwaitForCampus=newManualResetEvent(false); publicenumDifferenceType { Equal, Less, LessEqual, Greater, GreaterEqual } publicstaticboolDayDifference(DifferenceTypetype,DateTimedate,doubletotalDiff) { boolretValue=false; TimeSpandiff=DateTime.Now.Subtract(date); if((DifferenceType.Less==type)||(DifferenceType.LessEqual==type)) { if(diff.TotalDays<totalDiff) { retValue=true; } } if((DifferenceType.Greater==type)||(DifferenceType.GreaterEqual==type)) { if(diff.TotalDays>totalDiff) { retValue=true; } } if((DifferenceType.Equal==type)||(DifferenceType.LessEqual==type)||(DifferenceType.GreaterEqual==type)) { if(diff.TotalDays==totalDiff) { retValue=true; } } returnretValue; } publicstaticList<DependencyObject>GetChild(DependencyObjectrootObj,stringelemName) { List<DependencyObject>retValue=newList<DependencyObject>(); for(intk=0;k<VisualTreeHelper.GetChildrenCount(rootObj);k++) { varchild=VisualTreeHelper.GetChild(rootObj,k); if((childasFrameworkElement).Name.Equals(elemName)) { retValue.Add(child); } varret=retValue.Concat<DependencyObject>(GetChild(childasDependencyObject,elemName)); retValue=ret.ToList<DependencyObject>(); } returnretValue; } publicstaticvoidSetElementVisibility(DependencyObjectrootObj,stringparentGridName,stringelemName,Visibilityvis,intindex=0) { List<DependencyObject>l=Utilities.GetChild(rootObj,parentGridName); GridparentGrid=l[index]asGrid; FrameworkElementelem=null; foreach(FrameworkElementtempEleminparentGrid.Children) { if(tempElem.Name==elemName) { elem=tempElem; } } if(elem!=null) { elem.Visibility=vis; } } publicstaticVisibilityGetElementVisibility(DependencyObjectrootObj,stringparentGridName,stringelemName,intindex=0) { VisibilityretValue; List<DependencyObject>l=Utilities.GetChild(rootObj,parentGridName); if(index>=l.Count) { index=0; } GridparentGrid=l[index]asGrid; FrameworkElementelem=null; foreach(FrameworkElementtempEleminparentGrid.Children) { if(tempElem.Name==elemName) { elem=tempElem; } } if(elem!=null) { retValue=elem.Visibility; } else { thrownewNotImplementedException("CouldnotfindaUIElementwithname("+elemName+")"); } returnretValue; } publicstaticGeoPosition<GeoCoordinate>DetermineCurrentPosition() { if(!Settings.AppSetting.GeoWatchEnable) { returnnull; } GeoCoordinateWatcherwatcher=newGeoCoordinateWatcher(GeoPositionAccuracy.High); boolsuccess=watcher.TryStart(false,TimeSpan.FromMilliseconds(10000)); GeoPosition<GeoCoordinate>geoposition=null; if(success) { geoposition=watcher.Position; } elseif(watcher.Permission.Equals(GeoPositionPermission.Denied)) { //setto0point,ifaccesstodeviceisnotallow geoposition=newGeoPosition<GeoCoordinate>(); geoposition.Location=newGeoCoordinate(0,0); } watcher.Stop(); returngeoposition; } publicstaticvoidDetermineAndStoreCurrentPositionForce() { GeoPosition<GeoCoordinate>geoposition=Utilities.DetermineCurrentPosition(); if(geoposition!=null) { stringlat=geoposition.Location.Latitude.ToString(CultureInfo.InvariantCulture); stringlog=geoposition.Location.Longitude.ToString(CultureInfo.InvariantCulture); stringtime=geoposition.Timestamp.Ticks.ToString(); App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Lat,lat); App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Long,log); App.SaveToAppState<string>(Constants.GeoWatch_CurrentPosition_Time,time); } } publicstaticvoidDetermineAndStoreCurrentPosition() { stringlat=App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Lat); stringlog=App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Long); stringtime=App.LoadFromAppState<string>(Constants.GeoWatch_CurrentPosition_Time); if(lat==null||log==null||time==null||lat.Equals(string.Empty)||log.Equals(string.Empty)||time.Equals(string.Empty)) { Utilities.DetermineAndStoreCurrentPositionForce(); } else { longlongTime=0; if(!long.TryParse(time,outlongTime)) { return; } DateTimeexpired=newDateTime(longTime).AddMinutes(15); if(DateTime.Now.Ticks>expired.Ticks) { Utilities.DetermineAndStoreCurrentPositionForce(); } } } } }