cannot change my Checkbox.

Eduardo Gomez 3,431 Reputation points
2023-12-07T21:24:45.51+00:00

I am missing something.

I have a property IsChecked.

public partial class NewLecturePageViewModel(IAppService appService, IDataService<User> dataService,
    IAuthenticationService authenticationService) : BaseViewModel {

    [ObservableProperty]
    bool isChecked;

    public ObservableCollection<User> Users { get; set; } = [];

    public ObservableCollection<User> Invited { get; set; } = [];

    [RelayCommand]
    void Appearing() {
        GetUsers();
    }

    private async void GetUsers() {
        Users.Clear();

        var currenUser = await authenticationService.GetLoggedInUser();

        if (currenUser != null) {

            var data = await dataService.GetAllAsync<User>("Users");

            var filterUsers = data
                .Where(u => u.Object.Uid != currenUser?.Uid)
                .Select(u => u.Object)
                .ToList();

            foreach (var filterUser in filterUsers) {

                Users.Add(filterUser);
            }
        }
    }

    [RelayCommand]
    void HandleCheckBox(User user) {

        if (IsChecked is false) {
            appService.DisplayToast("False", ToastDuration.Short, 18);
        } else {
            appService.DisplayToast("true", ToastDuration.Short, 18);

        }

    }
}

and is not checking the box

   <CheckBox
       Grid.RowSpan="3"
       Grid.Column="2"
       IsChecked="{Binding Source={RelativeSource AncestorType={Type vm:NewLecturePageViewModel}}, Path=IsChecked}"
       HorizontalOptions="End"
       VerticalOptions="End">
       <CheckBox.GestureRecognizers>
           <TapGestureRecognizer
               Command="{Binding Source={RelativeSource AncestorType={Type vm:NewLecturePageViewModel}}, Path=HandleCheckBoxCommand}"
               CommandParameter="{Binding .}" />
       </CheckBox.GestureRecognizers>
   </CheckBox>

and I need to be able to check it, in order to send the selected user to the collection.

code

https://github.com/eduardoagr/DemyAI

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,586 Reputation points Microsoft Vendor
    2023-12-08T06:07:50.28+00:00

    Hello,

    ===============Update=============

    It appears to work well in Windows, but not Android

    Yes, the click event is consumed by DisplayAlert and cannot be transmitted to the checkbox in the Android/iOS platform.

    For fix this issue, you can implement this ObservableObject object and change it to partial class, then add [ObservableProperty] to the isInvited property, then this property can be changed at the runtime.

    public partial class User:ObservableObject
    {
        [ObservableProperty]
        public bool isInvited;
    ...
    }
    

    After that, we can use Conditional compilation to fix this different platform issue. For iOS and Android, we can change this IsInvited value by ourselves, for windows, you can keep this implement.

       [RelayCommand]
        void HandleCheckBox(User user) {
    
    
    #if ANDROID || IOS
    
    
           if (user.IsInvited is false)
            {
                user.IsInvited=true;
                appService.DisplayAlert("Info", "true", "ok");
            }
            else
            {
                user.IsInvited = false;
                appService.DisplayAlert("Info", "false", "ok");
    
           }
    #elif WINDOWS
             if (user.IsInvited is false) {
                appService.DisplayAlert("Info", "False", "ok");
            } else {
                appService.DisplayAlert("Info", "Yes", "ok");
    
           }
    #endif
    
    
       }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

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.