我有prism.forms和propertychanged的问题。
我有settingsmodel,settingsviewmodel和设置页面,显示如下代码,
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
}
和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}" />
我想在用户输入url时比IsEnabled属性为true,当Url为空时IsEnabled属性为false并且如果IsEnabled为false则保存按钮,按钮未启用。
我的主要问题是,我写了Entry url但是propertychanged事件没有被解雇?
我该如何解决这个问题?
谢谢。