implant view model

main
syneffort 1 year ago
parent 921060e412
commit 0908a0b9f4
  1. 15
      MyFirstMauiApp/WeatherClient/MainPage.xaml
  2. 11
      MyFirstMauiApp/WeatherClient/MainPage.xaml.cs
  3. 32
      MyFirstMauiApp/WeatherClient/ViewModels/WeatherViewModel.cs

@ -2,7 +2,12 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:cvt="clr-namespace:WeatherClient.Converters"
xmlns:vm="clr-namespace:WeatherClient.ViewModels"
x:Class="WeatherClient.MainPage">
<ContentPage.BindingContext>
<vm:WeatherViewModel/>
</ContentPage.BindingContext>
<ContentPage.Resources>
<cvt:WeatherConditionToImageConverter x:Key="WeatherToImage"/>
</ContentPage.Resources>
@ -20,15 +25,15 @@
<Rectangle BackgroundColor="Black" HeightRequest="4" Margin="2,10" />
<Grid Grid.Row="2" RowDefinitions="Auto, Auto, Auto, Auto, Auto" ColumnDefinitions="Auto, Auto" Margin="0,5,0,0">
<Label Grid.Row="0" Grid.Column="0" Text="Condition" VerticalOptions="Center" />
<Image Grid.Row="0" Grid.Column="1" HeightRequest="25" WidthRequest="25" Source="{Binding Condition, Converter={StaticResource WeatherToImage}}" HorizontalOptions="Start" />
<Image Grid.Row="0" Grid.Column="1" HeightRequest="25" WidthRequest="25" Source="{Binding WeatherData.Condition, Converter={StaticResource WeatherToImage}}" HorizontalOptions="Start" />
<Label Grid.Row="1" Grid.Column="0" Text="Temperature" Margin="0,0,20,0" />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Temperature}" />
<Label Grid.Row="1" Grid.Column="1" Text="{Binding WeatherData.Condition}" />
<Label Grid.Row="2" Grid.Column="0" Text="Humidity" Margin="0,0,20,0" />
<Label Grid.Row="2" Grid.Column="1" Text="{Binding Humidity}" />
<Label Grid.Row="2" Grid.Column="1" Text="{Binding WeatherData.Humidity}" />
<Label Grid.Row="3" Grid.Column="0" Text="Precipitation" Margin="0,0,20,0" />
<Label Grid.Row="3" Grid.Column="1" Text="{Binding Precipitation}" />
<Label Grid.Row="3" Grid.Column="1" Text="{Binding WeatherData.Humidity}" />
<Label Grid.Row="4" Grid.Column="0" Text="Wind" Margin="0,0,20,0" />
<Label Grid.Row="4" Grid.Column="1" Text="{Binding Wind}" />
<Label Grid.Row="4" Grid.Column="1" Text="{Binding WeatherData.Wind}" />
</Grid>
</VerticalStackLayout>
</Grid>

@ -1,8 +1,11 @@
namespace WeatherClient
using WeatherClient.Models;
using WeatherClient.ViewModels;
namespace WeatherClient
{
public partial class MainPage : ContentPage
{
int count = 0;
private WeatherViewModel _context;
public MainPage()
{
@ -14,7 +17,9 @@
btnRefresh.IsEnabled = false;
actIsBusy.IsRunning = true;
BindingContext = await Services.WeatherServer.GetWeather(txtPostalCode.Text);
WeatherData weatherData = await Services.WeatherServer.GetWeather(txtPostalCode.Text);
_context = new WeatherViewModel() { WeatherData = weatherData };
BindingContext = _context;
btnRefresh.IsEnabled = true;
actIsBusy.IsRunning = false;

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeatherClient.Models;
namespace WeatherClient.ViewModels
{
public class WeatherViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private WeatherData _weatherData;
public WeatherData WeatherData
{
get { return _weatherData; }
set
{
_weatherData = value;
OnPropertyChanged(nameof(WeatherData));
}
}
private void OnPropertyChanged(string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Loading…
Cancel
Save