Xamarin의 Button에서 TextColor 변경 및 IsEnabled 명령 사용
Xamarin 앱에서 IsEnabled
on Button 을 사용 하고 있는데 할 수없는 두 가지가 있습니다.
- 를 변경
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
{
}
또한 여러 클래스를 사용할 수 없습니다. 를 사용할 수있는 방법이 있나요 Entry
과 ContentPage
에 xml.cs 페이지는?
- 내가 사용하려는
Command
경우에만IsEnabled = true
예ICommand
에서 의 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");
}
답변
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");
}
}
- IsEnabled = false 일 때 TextColor를 변경하지만 BackgroundColor는 변경할 수 있습니다. 왜?
예, BackgroundColor를 변경할 수 있지만 내부적으로 재정의하므로 TextColor는 변경할 수 없습니다. 텍스트 색상을 변경하려면 렌더러를 만들어야합니다.
- 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));
}
}