Temperature.xaml.cs
using System.Diagnostics.Metrics;
using System.Text.Json;
using System.Xml.Serialization;
namespace TPMaui3.Views;
public partial class vTemperature : ContentPage
{
public vTemperature()
{
InitializeComponent();
}
private void AfficheTemperature(object sender, EventArgs e)
{
LireTemperature();
}
private async void LireTemperature()
{
Uri uri = new("http://meteorestsrvmobile.lab3il.fr/RestServiceMeteo.svc/xml/1");
try
{
HttpClient client = new();
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
String content = await response.Content.ReadAsStringAsync();
content = content.Substring(content.IndexOf("<XMLDataResult>") + 15);
content = content.Substring(0, content.IndexOf("</XMLDataResult>"));
lbTemp.Text = content + " °C";
}
}
catch (Exception ex)
{
await this.DisplayAlert("Error", ex.Message, "OK");
}
}
}
vMeteo.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TPMaui3.Views.vMeteo"
Title="Météo"
>
<VerticalStackLayout Spacing="20" Margin="10" >
<Label
Text="Météo"
x:Name="lbMeteo"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Primary}"
/>
<Label
Text="?"
x:Name="lbTemperature"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
<Label
Text="?"
x:Name="lbPression"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
<Label
Text="?"
x:Name="lbHumidite"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
<Label
Text="?"
x:Name="lbPrecipitationJour"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
<Label
Text="?"
x:Name="lbTemperatureRessentie"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
<Label
Text="?"
x:Name="lbVentDirection"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
<Label
Text="?"
x:Name="lbVentVitesse"
VerticalOptions="Center"
HorizontalOptions="Center"
FontSize="Medium"
TextColor="{StaticResource Secondary}"
/>
</VerticalStackLayout>
</ContentPage>
vMeteo.xaml.cs
using System.Diagnostics.Metrics;
using System.Text.Json;
using TPMaui3.Models;
namespace TPMaui3.Views;
public partial class vMeteo : ContentPage
{
public vMeteo()
{
InitializeComponent();
Meteo();
}
private async void Meteo()
{
Uri uri = new("http://meteorestsrvmobile.lab3il.fr/RestServiceMeteo.svc/jsontmps");
try
{
HttpClient client = new();
HttpResponseMessage response = await client.GetAsync(uri);
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
result = result.Replace("{\"JSONDataTmpsResult\":", "");
result = result.Replace("}}", "}");
var vMeteo = Newtonsoft.Json.JsonConvert.DeserializeObject<CMeteo>(result);
lbMeteo.Text = "Relevé à " + vMeteo.DateReleve.ToString();
lbTemperature.Text = "Temp: " + vMeteo.Temperature.ToString() + " °C";
lbHumidite.Text = "Hum: " + vMeteo.Humidite.ToString() + " %";
lbPression.Text = "Pres: " + vMeteo.Pression.ToString() + " hPa";
lbTemperatureRessentie.Text = "TR: " + vMeteo.TemperatureRessentie.ToString() + " °C";
lbPrecipitationJour.Text = "PrecJ: " + vMeteo.PrecipitationJour.ToString() + " mm";
lbVentVitesse.Text = "Vit: " + vMeteo.VentVitesse.ToString() + " m/s";
lbVentDirection.Text = "Vent: " + vMeteo.VentDirection.ToString();
}
}
catch (Exception ex)
{
await this.DisplayAlert("Error", ex.Message, "OK");
}
}
}
CMeteo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TPMaui3.Views;
namespace TPMaui3.Models;
internal class CMeteo
{
public CMeteo()
{
}
public string DateReleve { get; set; }
public string Temperature { get; set; }
public string Pression { get; set; }
public string Humidite { get; set; }
public string PrecipitationJour { get; set; }
public string TemperatureRessentie { get; set; }
public string VentDirection { get; set; }
public string VentVitesse { get; set; }
}