Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

[PRISM] Why my button is enable whereas my command should be false!

$
0
0

Hello,

I only want that my update button is active if entries are edited AND it's not the first load.
When i go on my page, data is loaded in entries and IsEdited switch to True, result my update button is active.
I have try to switch IsEdited = False; after the data load but it's not work...
Come on @NMackay ! :smile:

Best regards

My XAML:

            <StackLayout Grid.Row="0" Margin="20, 0, 20, 20" >
                <Label Style="{DynamicResource FormLabel}" Text="Nom"/>
                <Entry Style="{DynamicResource FormEntry}" Text="{Binding Lastname}" Keyboard="Default"/>
                <Label Style="{DynamicResource FormLabel}" Text="Prénom"/>
                <Entry Style="{DynamicResource FormEntry}" Text="{Binding Firstname}" Keyboard="Default"/>
                <Label Style="{DynamicResource FormLabel}" Text="Adresse électronique"/>
                <Entry Style="{DynamicResource FormEntry}" Text="{Binding Email}" Keyboard="Email"/>
                <Label Style="{DynamicResource FormLabel}" Text="Téléphone"/>
                <Entry Style="{DynamicResource FormEntry}" Text="{Binding Phone}" Keyboard="Telephone"/>
            </StackLayout>
            <StackLayout Grid.Row="1" Style="{DynamicResource BotActionBar}">
                <Button Style="{DynamicResource FormButton}" Text="Mettre à jour" Command="{Binding UpdateCommand}" HorizontalOptions="EndAndExpand"/>
            </StackLayout>

My ViewModel:

    public class ProfileEditViewModel : BindableBase
    {
        #region // Fields
        private bool _isUpdating;
        private bool _isEdited;
        private string _lastname;
        private string _firstname;
        private string _email;
        private string _phone;
        #endregion

        #region // Properties
        public INavigationService NavigationService { get; private set; }
        public DelegateCommand<string> NavigateCommand { get; private set; }
        public DelegateCommand UpdateCommand { get; private set; }

        public bool IsUpdating { get { return _isUpdating; } set { SetProperty(ref _isUpdating, value); } }
        public bool IsEdited { get { return _isEdited; } set { SetProperty(ref _isEdited, value); } }
        public string Lastname { get { return _lastname; } set { SetProperty(ref _lastname, value.NameFormat()); IsEdited = true; } }
        public string Firstname { get { return _firstname; } set { SetProperty(ref _firstname, value.NameFormat()); IsEdited = true; } }
        public string Email { get { return _email; } set { SetProperty(ref _email, value.ToLower()); IsEdited = true; } }
        public string Phone { get { return _phone; } set { SetProperty(ref _phone, value.PhoneFormat()); IsEdited = true; } }
        #endregion

        #region // Methods
        public ProfileEditViewModel(INavigationService navigationService)
        {
            NavigationService = navigationService;
            NavigateCommand = new DelegateCommand<string>(Navigate);
            UpdateCommand = new DelegateCommand(Update).ObservesCanExecute((p) => IsEdited);

            Lastname = App.API.User.Lastname;
            Firstname = App.API.User.Firstname;
            Email = App.API.User.Email;
            Phone = App.API.User.Phone;
            IsEdited = false;
        }

        private async void Update()
        {
            IsUpdating = true;
            if (await App.API.EditUserInfos(Lastname, Firstname, Email, Phone))
            {
                App.API.User.Lastname = Lastname;
                App.API.User.Firstname = Firstname;
                App.API.User.Email = Email;
                App.API.User.Phone = Phone;
                IsEdited = false;
            }
            IsUpdating = false;
        }

        private void Navigate(string s)
        {
            NavigationService.NavigateAsync(s);
        }
        #endregion
    }

Best regards


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>