为了账号安全,请及时绑定邮箱和手机立即绑定

从 ViewModel 返回 bool 到 View 绑定

从 ViewModel 返回 bool 到 View 绑定

C#
慕尼黑的夜晚无繁华 2023-09-16 17:11:05
我想要一个按钮来更改标签的可见性,一旦我单击它。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}"/>视图模型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不应该改变布尔值?
查看完整描述

2 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

你不能这样做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>


查看完整回答
反对 回复 2023-09-16
?
BIG阳

TA贡献1859条经验 获得超6个赞

您可以改进您的代码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();

    }

  }

}


查看完整回答
反对 回复 2023-09-16
  • 2 回答
  • 0 关注
  • 51 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信