Advertisement
fcamuso

MAUI video 2 (geolocalizzare)

Dec 29th, 2022
1,674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. //CREARE UN PROGETTO 'BLANK' E INCOLLARE
  2.  
  3. //PARTE XAML
  4. <?xml version="1.0" encoding="utf-8" ?>
  5. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  6.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  7.              x:Class="Maui_01.MainPage">
  8.  
  9.     <ScrollView>
  10.         <VerticalStackLayout Spacing="25" Padding="30">
  11.  
  12.             <Label
  13.                 Text="Geolocalizzazione"
  14.                 SemanticProperties.HeadingLevel="Level1"
  15.                 FontSize="32"
  16.                 HorizontalOptions="Center" />
  17.  
  18.      
  19.             <Button
  20.                 Text="Localizzami"
  21.                 FontAttributes="Bold"
  22.                 SemanticProperties.Hint="Counts the number of times you click"
  23.                 Clicked="OnGeolocalizzaClicked"
  24.                 HorizontalOptions="Center" />
  25.  
  26.             <Label
  27.                 Text=""
  28.                 SemanticProperties.HeadingLevel="Level1"
  29.                 FontSize="32"
  30.                 HorizontalOptions="Center" />
  31.            
  32.  
  33.             <Image
  34.                 Source="dotnet_bot.png"
  35.                 SemanticProperties.Description="Cute dot net bot waving hi to you!"
  36.                 WidthRequest="250"
  37.                 HeightRequest="310"
  38.                 HorizontalOptions="Center" />
  39.  
  40.         </VerticalStackLayout>
  41.     </ScrollView>
  42. </ContentPage>
  43. *******************************************************************************************************************************************
  44.  
  45. PARTE CS
  46. namespace Maui_01
  47. {
  48.     public partial class MainPage : ContentPage
  49.     {  
  50.         int count = 0;
  51.  
  52.         public MainPage()
  53.         {
  54.             InitializeComponent();
  55.         }
  56.  
  57.         private async Task Geolocalizza()
  58.         {
  59.             var richiesta = new GeolocationRequest(GeolocationAccuracy.Best);
  60.             var datiGelocalizzazione = await Geolocation.GetLocationAsync(richiesta);
  61.  
  62.             await App.Current.MainPage.DisplayAlert("GEOLOCALIZZATO!",
  63.               $"Longitudine: {datiGelocalizzazione.Longitude} - Latitudine: {datiGelocalizzazione.Latitude}", "OK");
  64.         }
  65.  
  66.  
  67.         private async void OnGeolocalizzaClicked(object sender, EventArgs e)
  68.         {
  69.             var permessi = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
  70.  
  71.             if (permessi == PermissionStatus.Granted)
  72.                 await Geolocalizza();
  73.             else
  74.             {
  75.                 await App.Current.MainPage.DisplayAlert("Diritto negato",
  76.                       "Non avete concesso il permesso di accedere alla tua posizione.", "OK");
  77.                 var richiesti = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
  78.  
  79.                 if (richiesti == PermissionStatus.Granted)
  80.                     await Geolocalizza();
  81.                 else
  82.                     if (DeviceInfo.Platform == DevicePlatform.iOS)
  83.                       await App.Current.MainPage.DisplayAlert("Richiesta geolocalizzazione",
  84.                       "Geolocalizzazione necessaria", "OK");
  85.                   else
  86.                       await App.Current.MainPage.DisplayAlert("Richiesta geolocalizzazione",
  87.                       "Geolocalizzazione necessaria", "OK");
  88.             }
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement