I cannot go back

Eduardo Gomez Romero 825 Reputation points
2024-10-28T23:50:35.06+00:00

I am trying to go back when I press the back button

    public partial class AddNewTurbinePageViewModel(TurbinesService turbinesService,
        DeviceLanguageService deviceLanguageService, IFilePicker filePicker,
        IMediaPicker mediaPicker, IGeolocation geolocation)
        : ChargingStationsMapPageViewModel(turbinesService) {

        [ObservableProperty]
        Turbine turbine = new();

        CultureInfo? currentCulture;


        [RelayCommand]
        void OpenDatePicker(SfDateTimePicker datePicker) {

            if (datePicker != null) {

                datePicker.IsOpen = true;
            }

        }

        [RelayCommand]
        void ConfirmDate(SfDateTimePicker datePicker) {

            if (datePicker != null) {

                currentCulture = new CultureInfo(deviceLanguageService.GetDeviceLanguage());

                Turbine!.InstalationDateTime = datePicker.SelectedDate;

                datePicker.IsOpen = false;

                Turbine.StringifyInstalationDate = Turbine.InstalationDateTime?.ToString(
                    "D", currentCulture)!;

            }
        }

        [RelayCommand]
        void CancelDate(SfDateTimePicker views) {

            if (views != null) {

                views.IsOpen = false;
            }
        }

        async Task<Location?> GetLocation(string address) {

            try {
                IEnumerable<Location> locations = await Geocoding.Default.GetLocationsAsync(address);
                return locations?.FirstOrDefault();
            }
            catch {
                // Handle the exception (e.g., log it or show an error message to the user)
                await Shell.Current.DisplayAlert("Error",
                    Resources.AppResource.AddressFound, "OK");
                return null;
            }
        }


        [RelayCommand]
        async Task PickImages() {

            var results = await filePicker.PickMultipleAsync(new PickOptions {

                FileTypes = FilePickerFileType.Images,
            });

            if (results != null) {
                foreach (var result in results) {
                    var fileData = new FileData {
                        FileName = result.FileName,
                        FullPath = result.FullPath
                    };

                    if (!Turbine.Images!.Contains(fileData)) {
                        Turbine.Images.Add(fileData);
                    }
                }

            }
        }

        [RelayCommand]
        async Task TakePhoto() {

            var photo = await mediaPicker.CapturePhotoAsync();

            if (photo != null) {

                // Getting the file result information
                using Stream sourceStream = await photo.OpenReadAsync();

                // Saving the file in your local storage
                string localFilePath = Path.Combine(FileSystem.CacheDirectory, photo.FileName);
            }
        }

        [RelayCommand]
        async Task SaveAndClose() {

            var turbineLocation = await GetLocation(Turbine!.Address!);

            if (turbineLocation != null) {

                _turbinesService.AddTurbinePin(new TurbinePin {

                    Turbine = new Turbine {
                        Name = Turbine.Name,
                        Address = Turbine.Address,
                        StringifyInstalationDate = Turbine.StringifyInstalationDate,
                        Images = Turbine.Images,
                        Location = turbineLocation
                    },
                }, OnPinMarkerClickedCommand!);

                await Shell.Current.GoToAsync("..", true);
            }
        }

        [RelayCommand]
        public async Task GetCurrentLocation() {

            var location = await geolocation.GetLastKnownLocationAsync() ??
                await geolocation.GetLocationAsync(new GeolocationRequest {
                    DesiredAccuracy = GeolocationAccuracy.High,
                    Timeout = TimeSpan.FromSeconds(30)
                });

            var placemarks = await Geocoding.GetPlacemarksAsync(location!.Latitude, location!.Longitude);
            var placemark = placemarks.FirstOrDefault();
            Turbine.Address = placemark?.FeatureName;
        }
    }
}


https://reccloud.com/u/xp513xb

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,581 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.