-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
125 lines (106 loc) · 4.3 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MapaBrasil
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void OnPathTap(object sender, System.Windows.Input.GestureEventArgs e)
{
// Cor padrão dos elementos do mapa
Color cor = ConvertStringToColor("#007acc");
// Recupera todos os elementos "Path" do mapa
var listaPaths = FindControls<Path>(gridMapa);
// Altera a cor de todos os elementos novamente para a cor padrão
if (listaPaths != null)
{
foreach (Path item in listaPaths)
{
item.Fill = new SolidColorBrush(cor);
}
}
// Recupera o elemento Path selecionado
Path path = (Path)sender;
if (path != null)
{
// Altera a cor do elemento
path.Fill = new SolidColorBrush(Colors.Blue);
}
}
/// <summary>
/// Busca uma lista de controles dentro de outro controle.
/// </summary>
/// <typeparam name="T">Tipo a ser buscado.</typeparam>
/// <param name="parentElement">Controle pai.</param>
/// <returns>Controle.</returns>
public List<T> FindControls<T>(DependencyObject targetElement) where T : DependencyObject
{
var result = new List<T>();
var count = VisualTreeHelper.GetChildrenCount(targetElement);
if (count == 0)
return new List<T>();
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targetElement, i);
if (child is T)
{
result.Add(child as T);
}
else
{
result = FindControls<T>(child);
}
}
return result;
}
/// <summary>
/// Conmverte uma cor em Hexadecimal para System.Windows.Media.Color.
/// Referência: http://stackoverflow.com/a/11739523/1509954
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public Color ConvertStringToColor(string hex)
{
//remove the # at the front
hex = hex.Replace("#", "");
byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
int start = 0;
//handle ARGB strings (8 characters long)
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
start = 2;
}
//convert RGB characters to bytes
r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);
return Color.FromArgb(a, r, g, b);
}
// Sample code for building a localized ApplicationBar
//private void BuildLocalizedApplicationBar()
//{
// // Set the page's ApplicationBar to a new instance of ApplicationBar.
// ApplicationBar = new ApplicationBar();
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}