2

I have a problem with prism.forms and propertychanged.

I have settingsmodel,settingsviewmodel and settingspage that shown code below,

SettingsModel

public class SettingsModel: BindableBase
{
    public string Url { get; set; }
    public bool IsEnabled{get;set;}
    public string ApiUrl { get; set; }


    public SettingsModel()
    {
        this.Url = string.Empty;
        this.IsEnabled = false;
        this.ApiUrl = string.Empty;
    }
}

SettingsViewModel

[ImplementPropertyChanged]
public class SettingsPageViewModel : ViewModelBase
{

    readonly INavigationService _navigationService;
    readonly IUserDialogs _userDialogs;
    readonly IProductService _productService;

    #region Constructor

    public SettingsPageViewModel(INavigationService navigationService,
                              IUserDialogs userDialogs, IProductService productService)
    {
        _navigationService = navigationService;
        _userDialogs = userDialogs;
        _productService = productService;

        this.Settings = new SettingsModel();

        SaveCommand = new DelegateCommand(Save).ObservesCanExecute((vm) => Settings.IsEnabled);

    }

    #endregion


    #region Model


    SettingsModel _settingsModel;
    public SettingsModel Settings
    {
        get { return _settingsModel; }
        set { 

             if (_settingsModel != null)
                _settingsModel.PropertyChanged -= MyPersonOnPropertyChanged;

            SetProperty(ref _settingsModel, value); 


             if (_settingsModel != null)
                _settingsModel.PropertyChanged += MyPersonOnPropertyChanged;

            Validation();
        }
    }


    public bool IsLoading { get; set; }

    public DelegateCommand SaveCommand { get; set; }
    #endregion


    void MyPersonOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        Validation();
    }

    #region Methods

    async void Save()
    {

        var result = await _productService.GetProducts(Priority.UserInitiated,"","","");

    }

    void Validation()
    {
        Settings.IsEnabled = !string.IsNullOrEmpty(Settings.ApiUrl) ? true : false;
    }




    #endregion
}

And SettingsPage XAML

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
    prism:ViewModelLocator.AutowireViewModel="True" 
    x:Class="XXX.Warehouse.SettingsPage">

<Entry Text="{Binding Settings.ApiUrl}" Margin="0,5,0,5"
       Placeholder="www.example.com" HorizontalOptions="FillAndExpand" />
<Button Text="Save"
        FontSize="16"
        BorderRadius="5"
        TextColor="White"
        BackgroundColor ="#578A17"
        IsEnabled="{Binding Settings.IsEnabled}"
        Command="{Binding SaveCommand}" />

I want to do when user enter url than IsEnabled property will true, when Url is empty than IsEnabled property will false and save button if IsEnabled is false, button not enabled.

My main problem is, i write Entry url but propertychanged event not fired?

How can i solve this?

Thank you.

muhammetsahin
  • 181
  • 4
  • 14

0 Answers0