Xamarin의 Button에서 TextColor 변경 및 IsEnabled 명령 사용

Nov 13 2020

Xamarin 앱에서 IsEnabledon Button 을 사용 하고 있는데 할 수없는 두 가지가 있습니다.

  1. 를 변경 TextColor할 때 IsEnabled = false,하지만 난을 변경할 수 있습니다 BackgroundColor.

해결책은 Custom Entry를 사용하는 것이며 이에 대한 훌륭한 기사가 있습니다 => https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/entry

그러나 다음에서만 작동합니다.

public class MyEntry : Entry
{
}

내 페이지 뒤에있는 코드는 다음과 같습니다.

public class MyEntry : ContentPage
{
}

또한 여러 클래스를 사용할 수 없습니다. 를 사용할 수있는 방법이 있나요 EntryContentPagexml.cs 페이지는?

  1. 내가 사용하려는 Command경우에만 IsEnabled = trueICommand에서 의 ViewModel이 유일하게 작동한다, IsEnabled값입니다 true.

전체 코드 샘플 => https://stackoverflow.com/a/64808306/14139029

.xml

<Button
    x:Name="PasswordButton"
    IsEnabled="False"
    TextColor="#4DABFE"
    BackgroundColor = "#FFFFFF"
    Text="Submit"
    Command={Binding PasswordButtonCommand}>
</Button>

.xml.cs

if (Password.Text == ConfirmPassword.Text)
{
    PasswordButton.IsEnabled = true;
    PasswordButton.TextColor = Color.FromHex("004B87");
    PasswordButton.BackgroundColor = Color.FromHex("222222");
}

답변

2 LeoZhu-MSFT Nov 16 2020 at 07:48

Button을 설정할 때와 마찬가지로 버튼 IsEnabled="False"의 기본 스타일을 사용합니다.

사용자 정의 스타일을 사용하고 true가 true 인 Command경우를 처리하려면 IsEnable뷰 모델에서 사용자 정의 속성을 정의한 다음 Command이 속성을 기반으로 트리거 할 수 있습니다.

예를 들면 :

public class ViewModel
{

    public ICommand PasswordButtonCommand => new Command<Button>(Submit);
    public bool IsEnable { get; set; }

    private void Submit(object obj)
    {

        if (IsEnable)
        {
             //do something you want
        }
    
    }
}

그런 다음 Entry TextChanged이벤트에서 :

private void Entry_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (Password.Text == ConfirmPassword.Text)
        {
            viewModel.IsEnable = true;
            PasswordButton.TextColor = Color.FromHex("004B87");
            PasswordButton.BackgroundColor = Color.FromHex("222222");
        }
        else
        {
            viewModel.IsEnable = false;
            PasswordButton.TextColor = Color.FromHex("4DABFE");
            PasswordButton.BackgroundColor = Color.FromHex("FFFFFF");
        }
    }
jai Nov 13 2020 at 16:52
  1. IsEnabled = false 일 때 TextColor를 변경하지만 BackgroundColor는 변경할 수 있습니다. 왜?

예, BackgroundColor를 변경할 수 있지만 내부적으로 재정의하므로 TextColor는 변경할 수 없습니다. 텍스트 색상을 변경하려면 렌더러를 만들어야합니다.

  1. Command를 사용하면 IsEnabled가 true가됩니다. IsEnabled가 작동하지 않았습니다. IsEnabled에 대한 코드는 .xml.cs 페이지에 작성됩니다. IsEnabled와 함께 Command를 사용할 가능성이 있습니까?

명령과 IsEnabled 속성 간에는 관계가 없으므로 작동합니다.

Xaml.cs

<Button
    x:Name="PasswordButton"
            Clicked="PasswordButton_Clicked"
    IsEnabled="False" Command="{Binding PasswordButtonCommand}"
    Text="Submit">
        </Button>

ViewModel.cs

public class ViewModel : INotifyPropertyChanged
    {
        
        public ICommand PasswordButtonCommand => new Command(Submit);

        private void Submit(object obj)
        {
            Debug.WriteLine("Test");
        }

        public ViewModel()
        {
            
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(
        [CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this,
            new PropertyChangedEventArgs(propertyName));
        }
    }