xamarin.forms - 将 IsVisible 绑定到 Xamarin 表单中的 bool 属性
问题描述
我有一个要绑定到布尔属性 ( HasNotifications
) 的标签。但是,当属性为 false 时,标签保持可见。如果我在 XAML 中将 IsVisible 属性设置为 false,则标签不可见,因此问题似乎与绑定有关。
XAML:
<AbsoluteLayout
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label
Text="Title"
HorizontalOptions="Center"
VerticalOptions="Center"
TextColor="White"
FontSize="Large"
FontAttributes="Bold"
Margin="5"
BindingContext="{x:Reference DashboardPageView}"
Grid.Row="0" />
<Label
Text="Notifications"
HorizontalOptions="Start"
VerticalOptions="Center"
TextColor="White"
FontSize="Medium"
FontAttributes="Bold"
Margin="3"
BindingContext="{x:Reference DashboardPageView}"
IsVisible="{Binding HasNotifications}"
Grid.Row="1" />
</Grid>
</AbsoluteLayout>
我的视图模型:
public bool HasNotifications
{
get => this.hasNotifications;
set => this.SetProperty(ref this.hasNotifications, value);
}
解决方案
我认为您没有设置正确的BindingContext
. 这HasNotifications
是您的属性,ViewModel
而BindingContext
您设置为标签的是 DashboardPageView。
我写了一个简单的演示,希望你能从中得到一些想法:
在xml中:
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label
Text="Title"
HorizontalOptions="Center"
VerticalOptions="Center"
TextColor="Black"
FontSize="Large"
FontAttributes="Bold"
Margin="5"
Grid.Row="0" />
<Label
Text="Notifications"
HorizontalOptions="Start"
VerticalOptions="Center"
TextColor="Black"
FontSize="Medium"
FontAttributes="Bold"
Margin="3"
IsVisible="{Binding HasNotifications}"
Grid.Row="1" />
<Button Text="change HasNotifications" Clicked="Button_Clicked" Grid.Row="2"/>
</Grid>
在 cs 中:
public partial class MainPage : ContentPage
{
ViewModel myViewModel;
public MainPage()
{
InitializeComponent();
myViewModel = new ViewModel();
BindingContext = myViewModel;
}
private void Button_Clicked(object sender, EventArgs e)
{
myViewModel.HasNotifications = !myViewModel.HasNotifications;
}
}
public class ViewModel : INotifyPropertyChanged
{
bool _HasNotifications;
public event PropertyChangedEventHandler PropertyChanged;
public ViewModel()
{
}
public bool HasNotifications
{
set
{
if (_HasNotifications != value)
{
_HasNotifications = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("HasNotifications"));
}
}
}
get
{
return _HasNotifications;
}
}
}
如果您有任何问题,请随时问我。
推荐阅读
- javascript - Javascript/React 中的 API 请求分页?
- javascript - Angular 路由器对 Cognito 重定向没有反应?
- c# - Epplus - 在添加之前检查命名样式是否已经存在
- docker - 我可以从 DockerOperator 中设置 Airflow 变量吗?
- flutter - Dart 用相对值替换字符串中的花括号
- python - 在 Python 中禁用 subprocess.Popen 回显到命令提示符窗口标题
- android - 在 Android 中接收多个特征的通知
- excel - 将变量定义为列中值的总和时,对象定义错误
- calendar - Vaadin Framework 14+ 的完整日历 4.x 未以 24 小时格式显示
- amazon-web-services - 使用度量数学时 GetMetricData Api 调用的成本?