Как предотвратить доступ ControlTemplate к определенному привязываемому свойству типа ICommand?C#

Место общения программистов C#
Anonymous
Как предотвратить доступ ControlTemplate к определенному привязываемому свойству типа ICommand?

Сообщение Anonymous »

Вот тривиальный сценарий, иллюстрирующий мою проблему.
У меня есть CustomControl (без пользовательского интерфейса).
public class CustomControl : ContentView
{
public static readonly BindableProperty OkCommandProperty
= BindableProperty.Create(
nameof(OkCommand),
typeof(ICommand),
typeof(CustomControl),
propertyChanged: (bindable, old, @new) =>
{
var @this = (CustomControl)bindable;
var triggerCommand = (Command)@this.TriggerCommand;
triggerCommand.ChangeCanExecute();
});

public ICommand OkCommand
{
get => (ICommand)GetValue(OkCommandProperty);
set => SetValue(OkCommandProperty, value);
}

public ICommand TriggerCommand { get; }// it cannot be private

public CustomControl()
{
TriggerCommand = new Command(
execute: async () =>
{
// do other things first and then invoke OkCommand

// Simulating other things
await App.Current!
.MainPage!
.DisplayAlert("From CustomControl", "TriggerCommand's body", "Ok");

if (OkCommand?.CanExecute(null) == true)
OkCommand.Execute(null);
},
canExecute: () =>
{
return OkCommand != null;
});
}
}
  • Я хочу, чтобы OkCommand мог использоваться только ContentPage через . Его нельзя использовать ControlTemplate через должен использовать TriggerCommand через
    {
    await App.Current!
    .MainPage!
    .DisplayAlert("From TestPageViewModel", "OkCommand's body", "Ok");
    },
    canExecute: () =>
    {
    return true; // only for the sake of simplicity
    });
    }
    }

    TestPage.xaml














    Подробнее здесь: https://stackoverflow.com/questions/790 ... g-accessed

Вернуться в «C#»