private static void Floorplan_update(App.TrackItem item) { foreach (App.TrackItem floormap in App.Config.Items.Where(n => n.Type == Itemtypes.Floormap)) { floormap.State = UpdateOnOff(item, floormap.State); floormap.SvgImage.Source = SvgImageSource.FromSvgString(floormap.State); floormap.SvgImage.ReloadImage(); } }
/// <summary> /// Returns an image source for SvgImage that loads an image from given SVG image path and /// name. /// </summary> /// <param name="svgImageName">relative path to the SVG image file</param> /// <param name="fill">when not null, an alternative fill color for SVG path elements</param> /// <returns>image source</returns> public static ImageSource GetImageSource(string svgImageName, string fill = null) { var cache = DependencyService.Get <SvgImageCache>(); Debug.Assert(cache != null, "cache object must exist"); string svgText = cache.GetSvgImage(svgImageName, fill); return(SvgImageSource.FromSvgString(svgText)); }
public static void Launcher(Grid grid, int px, int py, int sx, int sy, string header, JObject data) { Microsoft.AppCenter.Analytics.Analytics.TrackEvent("Create Launcher Widget"); try { Models.Sitemap.Widget3 item = data.ToObject <Models.Sitemap.Widget3>(); Dictionary <string, string> widgetKeyValuePairs = Helpers.SplitCommand(item.Label); CrossLogger.Current.Debug("Launcher", "Label: " + widgetKeyValuePairs["label"]); //Master Grid for Widget Grid Widget_Grid = new Grid { RowDefinitions = new RowDefinitionCollection { new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }, new RowDefinition { Height = new GridLength(2, GridUnitType.Star) }, }, ColumnDefinitions = new ColumnDefinitionCollection { new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }, }, RowSpacing = 0, ColumnSpacing = 0, BackgroundColor = App.Config.CellColor, HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, Padding = new Thickness(0, 0, 0, 10), }; //Header Widget_Grid.Children.Add(new Forms9Patch.Label { Text = header.Replace("\"", " "), FontSize = 100, TextColor = App.Config.TextColor, BackgroundColor = App.Config.CellColor, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start, LineBreakMode = LineBreakMode.NoWrap, Lines = 1, AutoFit = Forms9Patch.AutoFit.Width, }, 0, 0); //Circle SvgCachedImage svg = new SvgCachedImage { DownsampleToViewSize = false, Aspect = Aspect.AspectFit, BitmapOptimizations = false, Source = SvgImageSource.FromSvgString(@"<svg viewBox=""0 0 100 100""><circle cx=""50"" cy=""50"" r=""50"" fill=""" + App.Config.ValueColor.ToHex().ToString() + @""" /></svg>"), }; Widget_Grid.Children.Add(svg, 0, 1); //Image Widget_Grid.Children.Add(new Image { Source = widgetKeyValuePairs["icon"], Aspect = Aspect.AspectFit, BackgroundColor = Color.Transparent, VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand, }, 0, 1); //Button must be last to be added to work Button launcherButton = new Button { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Transparent, StyleId = widgetKeyValuePairs["url"] //StyleID is not used on buttons }; Widget_Grid.Children.Add(launcherButton, 0, 1, 0, 2); launcherButton.Clicked += OnLauncherButtonClicked; CrossLogger.Current.Debug("Launcher", "Button ID: " + launcherButton.Id + " created."); grid.Children.Add(Widget_Grid, px, px + sx, py, py + sy); } catch (Exception ex) { CrossLogger.Current.Error("Launcher", "Widgets.Launcher crashed: " + ex.ToString()); Error(grid, px, py, sx, sy, ex.ToString()); } }
public static async void FloormapAsync(Grid grid, int px, int py, int sx, int sy, string header, JObject data) { Microsoft.AppCenter.Analytics.Analytics.TrackEvent("Create Floormap Widget"); try { Floorplan flooritem = data.ToObject <Floorplan>(); CrossLogger.Current.Debug("Floormap", "URL: " + flooritem.Url); var httpClient = new HttpClient(); var svgString = await httpClient.GetStringAsync(flooritem.Url); //Loop through SVG and find "ID" and add as Sensor MatchCollection matches = Regex.Matches(svgString, "(id=\")(.*)(.\\b(on|off))", RegexOptions.IgnoreCase); for (int j = 0; j < matches.Count; j++) { string Itemid = matches[j].Groups[2].Value; if (App.Config.Items.Find(s => s.Name == Itemid) == null) { CrossLogger.Current.Debug("Floormap", "Adding: " + Itemid); RestService GetItemUpdate = new RestService(); string state = GetItemUpdate.GetItem(Itemid); if (state != null) { App.TrackItem sensor = new App.TrackItem { Name = Itemid, State = state, Type = Itemtypes.Sensor }; App.Config.Items.Add(sensor); } } } #region w_grid Grid w_grid = new Grid { RowSpacing = 0, ColumnSpacing = 0, Padding = new Thickness(0, 0, 0, 0), BackgroundColor = App.Config.CellColor, VerticalOptions = LayoutOptions.FillAndExpand, HorizontalOptions = LayoutOptions.FillAndExpand, }; grid.Children.Add(w_grid, px, px + sx, py, py + sy); #endregion w_grid #region Header w_grid.Children.Add(new Label { Text = header, FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = App.Config.TextColor, BackgroundColor = App.Config.CellColor, HorizontalTextAlignment = TextAlignment.Center, VerticalTextAlignment = TextAlignment.Start }, 0, 0); #endregion Header #region SVG SvgCachedImage svg = new SvgCachedImage { DownsampleToViewSize = false, CacheDuration = TimeSpan.FromMilliseconds(1000), Aspect = Aspect.AspectFit, BitmapOptimizations = false, Source = SvgImageSource.FromSvgString(svgString) }; w_grid.Children.Add(svg, 0, 0); App.TrackItem svgImage = new App.TrackItem { State = svgString, Header = header, Type = Itemtypes.Floormap, SvgImage = svg }; App.Config.Items.Add(svgImage); #endregion SVG CrossLogger.Current.Debug("Floormap", "SVGBefore"); foreach (App.TrackItem item in App.Config.Items.Where(n => n.Type == Itemtypes.Sensor)) { svgImage.State = UpdateOnOff(item, svgImage.State); } svgImage.SvgImage.Source = SvgImageSource.FromSvgString(svgImage.State); svgImage.SvgImage.ReloadImage(); CrossLogger.Current.Debug("Floormap", "SVGAfter"); //Button must be last to be added to work Button dummyButton = new Button { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand, BackgroundColor = Color.Transparent, }; w_grid.Children.Add(dummyButton, 0, 0); dummyButton.Clicked += OnDummyButtonClicked; } catch (Exception ex) { CrossLogger.Current.Error("Floormap", "Widgets.Floormap crashed: " + ex.ToString()); Error(grid, px, py, sx, sy, ex.ToString()); } }