我希望按钮单击后即可更改标签的可见性。
xaml视图:
<local:ButtonRenderer Text="Connect" BackgroundColor="#6DCFF6" TextColor="White" Command="{Binding viewTemperature}" CornerRadius="10" WidthRequest="200" IsVisible="{Binding !isConnecting}"/>
<Label Text="PlaceholderText" TextDecorations="Underline" TextColor="White" Margin="0,5,0,0" HorizontalTextAlignment="Center" IsVisible="{Binding !isConnecting}"/>
ViewModel
viewTemperature = new Command(async () =>
{
isConnecting = true;
await _navigation.PushModalAsync(new TemperaturePage());
}) ;
public bool isConnecting
{
get
{
return _isConnecting;
}
set
{
_isConnecting = value;
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(_isConnecting.ToString()));
}
}
我在代码中放置了断点,并且在我的视图模型中将isConnected更改为true。但是,我的标签的可见性未更改。我怀疑PropertyChanged
是否不应该更改bool值?
答案 0 :(得分:3)
您无法执行IsVisible="{Binding !isConnecting}"
,此操作将无效。
您可以创建一个InvertBoolConverter,也可以使用更简单的选项“触发器”。这是一个示例:
<Label Text="PlaceholderText" TextDecorations="Underline" TextColor="White" Margin="0,5,0,0" HorizontalTextAlignment="Center"
IsVisible="{Binding isConnecting}">
<Label.Triggers>
<DataTrigger TargetType="Label" Binding="{Binding isConnecting}" Value="True">
<Setter Property="IsVisible" Value="False" />
</DataTrigger>
<DataTrigger TargetType="Label" Binding="{Binding isConnecting}" Value="False">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</Label.Triggers>
</Label>
答案 1 :(得分:0)
您可以在ViewModel
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool isconnecting ;
public bool isConnecting
{
get
{
return isconnecting;
}
set
{
if (isconnecting != value)
{
isconnecting = value;
NotifyPropertyChanged();
}
}
}