Entwickler-Ecke

C# - Die Sprache - Ereignis mit Parameter


ticcoid - Do 14.05.09 10:35
Titel: Ereignis mit Parameter
hallo, ich habe ein eher doofes problem und ich weiß noch nicht genau wie ich da rangehen soll. ich denke mal die lösung wird delegates heißen aber ich versuch erstmal das problem zu beschreiben.

im prinzip hole ich mir aus der datenbank gps koordinaten und sende immer 25 stück per wcf service zum auswerten. (auswerten bedeutet, ich male diese koordinaten um zu zeigen wo ich mich gerade befinde)
nun ist es so, dass man auch mehrere tage auswählen kann. zum beispiel wo war ich am 13.5 und am 14.5! und nun kommt das problem. ich möchte für jeden tag eine andere farbe auswählen. bis jetzt habe ich es über eine globale variable gelöst, was aber nicht wirklich funktioniert, da die wcf aufrufe asynchron sind. d.h. ich muss den tag irgendwie mit dem ereignis mitsenden.

ich hoffe ihr versteht das, als beispiel hänge ich den code mal mit an.



C#-Quelltext
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:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using Microsoft.VirtualEarth.MapControl;
using System.Windows.Media.Imaging;

namespace VESilverlightApp
{
    public partial class Page : UserControl
    {
        public string clientToken;

        public int tag = 0;

        public Page(string token)
        {
            clientToken = token;
            InitializeComponent();

            Calendar.DisplayDateEnd = DateTime.Now;
            Calendar.SelectionMode = CalendarSelectionMode.MultipleRange;
            Calendar.SelectedDatesChanged += new EventHandler<SelectionChangedEventArgs>(Calendar_SelectedDatesChanged);
            Calendar.SelectedDate = DateTime.Today;
        }

        void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            int _countRange = Calendar.SelectedDates.Count;
            DateTime first = Calendar.SelectedDates[0];
            DateTime last = Calendar.SelectedDates[_countRange - 1];

            if (first > last)
            {
                DateTime zwischen = last;
                last = first;
                first = zwischen;
            }

            last = last.AddHours(23);
            last = last.AddMinutes(59);
            last = last.AddSeconds(59);

            ServiceGetData.getDataClient client = new VESilverlightApp.ServiceGetData.getDataClient();
            client.getCoordByDateCompleted += new EventHandler<VESilverlightApp.ServiceGetData.getCoordByDateCompletedEventArgs>(client_getCoordByDateCompleted);
            client.getCoordByDateAsync(first, last);
            client.CloseAsync();
        }

        /*void client_getCoordByDateCompleted(object sender, VESilverlightApp.ServiceGetData.getCoordByDateCompletedEventArgs e)
        {
            myMap.Children.Clear();

            if (e.Result.Count < 2)
            {
                txtStatus.Text = "Status: Es liegen keine Daten vor!";
            }
            else
            {
                txtStatus.Text = "Status: Daten liegen vor!";

                int _countPoints = e.Result.Count - 1;

                VERouteService.RouteRequest routeRequest = new VESilverlightApp.VERouteService.RouteRequest();

                // Set the credentials using a valid Virtual Earth token
                routeRequest.Credentials = new VESilverlightApp.VERouteService.Credentials();
                routeRequest.Credentials.Token = clientToken;

                // Set the start, stop, and end points
                ObservableCollection<VERouteService.Waypoint> Waypoints = new ObservableCollection<VESilverlightApp.VERouteService.Waypoint>();
                VERouteService.Waypoint waypoint = new VESilverlightApp.VERouteService.Waypoint();

                double _counter = 0;

                _counter = (e.Result.Count / 25.0);
                _counter = Math.Round(_counter + 0.5);

                int k = 0;
                int l = 25;

                for (int j = 0; j < _counter; j++)
                {
                    if (j == _counter - 1)
                    {
                        for (int i = k; i < e.Result.Count; i++)
                        {
                            waypoint = new VERouteService.Waypoint();
                            waypoint.Location = new VERouteService.Location();
                            waypoint.Location.Latitude = e.Result[i].lat;
                            waypoint.Location.Longitude = e.Result[i].lon;
                            Waypoints.Add(waypoint);
                        }
                    }
                    else
                    {
                        for (int i = k; i < l; i++)
                        {
                            waypoint = new VERouteService.Waypoint();
                            waypoint.Location = new VERouteService.Location();
                            waypoint.Location.Latitude = e.Result[i].lat;
                            waypoint.Location.Longitude = e.Result[i].lon;
                            Waypoints.Add(waypoint);
                        }
                    }

                    routeRequest.Waypoints = Waypoints;

                    routeRequest.Options = new VESilverlightApp.VERouteService.RouteOptions();
                    routeRequest.Options.RoutePathType = VESilverlightApp.VERouteService.RoutePathType.Points;


                    VERouteService.RouteServiceClient routeService = new VESilverlightApp.VERouteService.RouteServiceClient();
                    routeService.CalculateRouteCompleted += new EventHandler<VESilverlightApp.VERouteService.CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted);
                    routeService.CalculateRouteAsync(routeRequest);

                    routeRequest.Waypoints.Clear();
                    k = k + 25;
                    l = l + 25;
                }

                // Create MapLayer 
                MapLayer myLayer = new MapLayer();


                // Create Image 
                Image image = new Image();
                // Set the Image Source 
                image.Source = new BitmapImage(new Uri("ich.png", UriKind.Relative));
                // Set Image Display Properties 
                image.Opacity = 1.0;
                image.Stretch = Stretch.Fill;
                image.Height = 55;
                image.Width = 40;
                // Create a Location object that defines where the "Shape" will be plotted 
                Location location = new Location() { Latitude = e.Result[_countPoints].lat, Longitude = e.Result[_countPoints].lon };
                // Center the image around the location specified 
                PositionMethod position = PositionMethod.Center;
                // Add Image to the Layer 
                myLayer.AddChild(image, location, position);
                myMap.AddChild(myLayer);

                myMap.Center = new Location(e.Result[_countPoints].lat, e.Result[_countPoints].lon);
                myMap.ZoomLevel = 14;
            }
        }*/







        void client_getCoordByDateCompleted(object sender, VESilverlightApp.ServiceGetData.getCoordByDateCompletedEventArgs e)
        {
            myMap.Children.Clear();

            if (e.Result.Count < 2)
            {
                txtStatus.Text = "Status: Es liegen keine Daten vor!";
            }
            else
            {
                txtStatus.Text = "Status: Daten liegen vor!";

                int _countPoints = e.Result.Count - 1;

                VERouteService.RouteRequest routeRequest = new VESilverlightApp.VERouteService.RouteRequest();

                // Set the credentials using a valid Virtual Earth token
                routeRequest.Credentials = new VESilverlightApp.VERouteService.Credentials();
                routeRequest.Credentials.Token = clientToken;

                // Set the start, stop, and end points
                ObservableCollection<VERouteService.Waypoint> Waypoints = new ObservableCollection<VESilverlightApp.VERouteService.Waypoint>();
                VERouteService.Waypoint waypoint = new VESilverlightApp.VERouteService.Waypoint();

                int zaehler = 0;
                int day = 0;
                int counter = 0;

                day = e.Result[zaehler].date.Day;


                do
                {
                    counter = 0;
                    
                    while (((day == e.Result[zaehler].date.Day) && (counter < 25)))
                    {
                        day = e.Result[zaehler].date.Day;

                        waypoint = new VERouteService.Waypoint();
                        waypoint.Location = new VERouteService.Location();
                        waypoint.Location.Latitude = e.Result[zaehler].lat;
                        waypoint.Location.Longitude = e.Result[zaehler].lon;
                        Waypoints.Add(waypoint);

                        counter++;
                        zaehler++;
                        if (zaehler == e.Result.Count)
                        {
                            break;
                        }
                    }
                    tag = day;
                    if (zaehler < e.Result.Count)
                    {
                        day = e.Result[zaehler].date.Day;
                    }

                    

                    routeRequest.Waypoints = Waypoints;

                    routeRequest.Options = new VESilverlightApp.VERouteService.RouteOptions();
                    routeRequest.Options.RoutePathType = VESilverlightApp.VERouteService.RoutePathType.Points;


                    VERouteService.RouteServiceClient routeService = new VESilverlightApp.VERouteService.RouteServiceClient();
                    routeService.CalculateRouteCompleted += new EventHandler<VESilverlightApp.VERouteService.CalculateRouteCompletedEventArgs>(routeService_CalculateRouteCompleted);
                    routeService.CalculateRouteAsync(routeRequest);

                    routeRequest.Waypoints.Clear();
                    

                } while (zaehler != e.Result.Count);


                // Create MapLayer 
                MapLayer myLayer = new MapLayer();

                // Create Image 
                Image image = new Image();
                // Set the Image Source 
                image.Source = new BitmapImage(new Uri("ich.png", UriKind.Relative));
                // Set Image Display Properties 
                image.Opacity = 1.0;
                image.Stretch = Stretch.Fill;
                image.Height = 55;
                image.Width = 40;
                // Create a Location object that defines where the "Shape" will be plotted 
                Location location = new Location() { Latitude = e.Result[_countPoints].lat, Longitude = e.Result[_countPoints].lon };
                // Center the image around the location specified 
                PositionMethod position = PositionMethod.Center;
                // Add Image to the Layer 
                myLayer.AddChild(image, location, position);
                myMap.AddChild(myLayer);

                myMap.Center = new Location(e.Result[_countPoints].lat, e.Result[_countPoints].lon);
                myMap.ZoomLevel = 14;
            }
        }



        void routeService_CalculateRouteCompleted(object sender, VESilverlightApp.VERouteService.CalculateRouteCompletedEventArgs e)
        {
            VERouteService.RouteResponse routeResponse = e.Result;

            if (routeResponse.Result.RoutePath.Points.Count > 0)
            {
                LocationCollection _Locations = new LocationCollection();

                for (int i = 0; i < routeResponse.Result.RoutePath.Points.Count; i++)
                {
                    _Locations.Add(new Location(routeResponse.Result.RoutePath.Points[i].Latitude, routeResponse.Result.RoutePath.Points[i].Longitude));
                }

                MapPolyline polyline = new MapPolyline();

                switch (tag)
                {
                    case 4:
                        polyline.Stroke = new SolidColorBrush(Colors.Black);
                        break;
                    case 5:
                        polyline.Stroke = new SolidColorBrush(Colors.Blue);
                        break;
                    case 13:
                        polyline.Stroke = new SolidColorBrush(Colors.Brown);
                        break;
                    case 14:
                        polyline.Stroke = new SolidColorBrush(Colors.Cyan);
                        break;
                    default:
                        polyline.Stroke = new SolidColorBrush(Colors.Cyan);
                        break;
                }

                polyline.StrokeThickness = 5;
                polyline.Opacity = 0.5;

                polyline.Locations = _Locations;

                myMap.AddChild(polyline);
            }
        }


    }
}


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt


JüTho - Do 14.05.09 11:31

Puh, so ein langer Code, und die Lösung des Problems führt erheblich weiter. Du brauchst vor allem eine Ableitung von EventArgs mit den von dir benötigten Parametern, also eine eigene Klasse CoordinatesEventArgs, und dann einen CoordinatesEventHandler (also in der Tat einen Delegaten), der diese CoordinatesEventArgs benutzt.

Schau einmal in die SDK-Doku/MSDN. Unter EventArgs-Klasse ist ein Beispiel zu beiden Teilen zu finden.

Gruß Jürgen


Kha - Do 14.05.09 11:48

user profile iconJüTho hat folgendes geschrieben Zum zitierten Posting springen:
[...] und dann einen CoordinatesEventHandler
Oder einfacher einen EventHandler<CoordinatesEventArgs>.
Wenn der EventArgs-Artikel das schon nicht verwendet, hätten sie wenigstens den Delegate nicht als Nested Type deklarieren müssen ;) . Die Qualität der Beispiele schwankt eben doch stark...