首页 > 解决方案 > 如何在 Xamarin 中使用开关删除标签和条目

问题描述

我正在尝试使用 x:Name="MS" 开关删除标签首付和条目首付,并将条目的文本/值更改为“0”。这是我的 *.xaml 代码

'''

   <Label Text="Purchased price"
            Margin="0,100,0,0" Padding="13,0,0,0" />
    <Entry Placeholder="Price $$"
           x:Name="price"
           Keyboard="Numeric"
           Margin="130,-30,40,0"
           Opacity="1" Rotation="0"
           TranslationX="16" />

    <Label  Text="Mortgage ??"
            Margin="0,10,0,0" Padding="13,0,0,0"/>
    <Switch IsToggled="false"
            Margin="130,-30,40,0"
            x:Name="MS"/>


    <Label Text="Downpayment"
           Margin="0,10,0,0" Padding="13,0,0,0"/>
    <Entry Placeholder="Down Payment"
           x:Name="Downpayment"
           Text="DP"
           Keyboard="Numeric"
           Margin="130,-30,40,0"
           Opacity="1" Rotation="0"
           TranslationX="16" />

'''

标签: xamarinxamarin.formsxamarin.ios

解决方案


Toggled为事件添加处理程序

<Switch IsToggled="false" Toggled="SwitchToggled" ... />

然后

protected void SwitchToggled(object sender, ToggledEventArgs e)
{
  // true if toggled
  if (e.Value)
  {
    Downpayment.IsVisible = false;
    DownpaymentLabel.IsVisible = false;
    price.Text = "0";
  }
}

推荐阅读