亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 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 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號