Значение не обновляется в привязываемом свойстве пользовательского элемента управления - формы Xamarin / Prism - PullRequest
0 голосов
/ 23 октября 2018

Я создал собственный элемент управления, в котором есть элемент Entry и метка внутри StackControl.Затем я открыл свойство IsEnabled Entry и привязываемое свойство IsEntryEnabled , которое я хотел связать со свойством bool моей виртуальной машины.Но это никогда не срабатывает.Любая идея, что я делаю здесь не так?

Пользовательский элемент управления - Xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:effects="clr-namespace:VMSTablet.Effects;assembly=VMSTablet"
             x:Class="VMSTablet.Controls.StandardFormEntry">
    <StackLayout >
        <Label  Text="{Binding LabelText}" Style="{StaticResource DefaultLabelStyle}" TextColor="{StaticResource DarkGreenColor}"/>
        <Entry x:Name="CustomEntry" Text="{Binding Text}" 
               IsEnabled="{Binding IsEntryEnabled}" 
               Keyboard="{Binding Keyboard}" 
               Behaviors="{Binding Behaviors}" 
               TextColor="{StaticResource DarkGreenColor}"
               Placeholder="{Binding Placeholder}" Style="{StaticResource DefaultEntryStyle}" >
            <Entry.Effects>
                <effects:EntryBarColorEffect/>
            </Entry.Effects>
        </Entry>
    </StackLayout>
</ContentView>

Пользовательский элемент управления - код позади

namespace VMST.Controls
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class StandardFormEntry
    {
        public event EventHandler OnTextChanged;
        public event EventHandler OnUnfocused;
        public event EventHandler OnFocused;

        public static readonly BindableProperty PlaceholderProperty =
            BindableProperty.Create("Placeholder", typeof(string), typeof(StandardFormEntry), string.Empty);
        public static readonly BindableProperty LabelTextProperty =
            BindableProperty.Create("LabelText", typeof(string), typeof(StandardFormEntry), string.Empty);

        public static readonly BindableProperty EntryTextProperty =
            BindableProperty.Create("EntryText", typeof(string), typeof(StandardFormEntry), string.Empty);

        public static BindableProperty IsEntryEnabledProperty =
            BindableProperty.Create("IsEntryEnabled", typeof(bool), typeof(StandardFormEntry), true);   

        public static readonly BindableProperty KeyboardProperty =
            BindableProperty.Create("Keyboard", typeof(Keyboard), typeof(StandardFormEntry), Xamarin.Forms.Keyboard.Default);
        public static readonly BindableProperty BehaviorsProperty =
            BindableProperty.Create(nameof(Behaviors), typeof(IList<Behavior>), typeof(StandardFormEntry));


        public string LabelText
        {
            set
            {
                SetValue(LabelTextProperty, value);
            }
            get => (string)GetValue(LabelTextProperty);
        }

        public string EntryText
        {
            set => SetValue(EntryTextProperty, value);
            get => (string)GetValue(EntryTextProperty);
        }

        public string Placeholder
        {
            set => SetValue(PlaceholderProperty, value);
            get => (string)GetValue(PlaceholderProperty);
        }

        public bool IsEntryEnabled
        {
            set
            {
                SetValue(IsEntryEnabledProperty, value);
            }
            get => (bool)GetValue(IsEntryEnabledProperty);

        }

        public Keyboard Keyboard
        {
            set => SetValue(KeyboardProperty, value);
            get => (Keyboard)GetValue(KeyboardProperty);
        }

        public IList<Behavior> Behaviors
        {
            set => SetValue(BehaviorsProperty, value);
            get => (IList<Behavior>)GetValue(BehaviorsProperty);
        }

        public StandardFormEntry()
        {
            InitializeComponent();
            BindingContext = this;
            CustomEntry.BindingContext = this;
            PropertyChanged += StandardFormEntry_PropertyChanged;

            CustomEntry.Unfocused += CustomEntry_Unfocused;
            CustomEntry.TextChanged += CustomEntry_TextChanged;
            CustomEntry.Focused += CustomEntry_Focused;
        }

        private void StandardFormEntry_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == IsEntryEnabledProperty.PropertyName)
            {
                **//This never happens!**
            }
        }

        protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            base.OnPropertyChanged(propertyName);
        }

        private void CustomEntry_Focused(object sender, FocusEventArgs e)
        {
            OnFocused?.Invoke(sender, e);
        }

        private void CustomEntry_TextChanged(object sender, TextChangedEventArgs e)
        {
            OnTextChanged?.Invoke(sender, e);
        }

        private void CustomEntry_Unfocused(object sender, FocusEventArgs e)
        {
            OnUnfocused?.Invoke(sender, e);
        }        
    }
}

Просмотр модели

Я пытаюсь вызвать свойство IsEntryEnabled в методе EditForm () ниже.Но это не работает.

public class PassRegistrationPageViewModel : ViewModelBase
    {
        public DelegateCommand AddressCommand { get; set; }
        public DelegateCommand EditCommand { get; set; }
        public DelegateCommand ConfirmCommand { get; set; }
        public PassRegistrationPageViewModel(INavigationService navigationService) : base(navigationService)
        {
            Title = "Page Title";
            AddressCommand = new DelegateCommand(ShowBuildings);
            EditCommand = new DelegateCommand(EditForm);
            ConfirmCommand = new DelegateCommand(ConfirmPass);
            //IsQrVisible = false;
        }

        private bool _isQrVisible;
        public bool IsQrVisible
        {
            get { return _isQrVisible; }
            set {
                SetProperty(ref _isQrVisible, value);
            }
        }

        private bool _isEditingEnabled;
        public bool IsEditingEnabled //Bound to the view below
        {
            get { return _isEditingEnabled; }
            set { SetProperty(ref _isEditingEnabled, value); }
        }

        private string  _text;
        public string Text
        {
            get { return _text; }
            set
            {
                SetProperty(ref _text, value);
            }
        }

        private async void ShowBuildings()
        {
            await NavigationService.NavigateAsync(nameof(BuildingListPage));
        }

        public void EditForm()
        {
            IsEditingEnabled = IsEditingEnabled ? false : true;
        }

        public void ConfirmPass()
        {

        }


    }

Просмотр

Я связываю свойство IsEditingEnabled с IsEntryEnabled свойством вмой пользовательский элемент управления, который должен вызывать свойство IsEnabled в Entry внутри.Но это никогда не срабатывает.

 <controls:StandardFormEntry IsEntryEnabled="{Binding IsEditingEnabled}" EntryText="{Binding Text}" LabelText="Name" Grid.Column="0" Grid.Row="3"/>

1 Ответ

0 голосов
/ 29 ноября 2018

В вашем пользовательском элементе управления вам нужно дать имя представлению содержимого и передать имя источнику свойства привязки, как показано ниже:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:effects="clr-namespace:VMSTablet.Effects;assembly=VMSTablet"
         x:Class="VMSTablet.Controls.StandardFormEntry"
         x:Name="CustomEntryControl">
<StackLayout >
    <Label  Text="{Binding LabelText}" Style="{StaticResource DefaultLabelStyle}" TextColor="{StaticResource DarkGreenColor}"/>
    <Entry x:Name="CustomEntry" Text="{Binding Text}" 
           IsEnabled="{Binding IsEntryEnabled, Source={x:Reference CustomEntryControl}}" 
           Keyboard="{Binding Keyboard}" 
           Behaviors="{Binding Behaviors}" 
           TextColor="{StaticResource DarkGreenColor}"
           Placeholder="{Binding Placeholder}" Style="{StaticResource DefaultEntryStyle}" >
        <Entry.Effects>
            <effects:EntryBarColorEffect/>
        </Entry.Effects>
    </Entry>
</StackLayout>

...