gpt4 book ai didi

xamarin.forms - 将 IsVisible 绑定(bind)到 Xamarin Forms 中的 bool 属性

转载 作者:行者123 更新时间:2023-12-02 02:42:20 28 4
gpt4 key购买 nike

我有一个标签,想要绑定(bind)到 bool 属性 (HasNotifications)。但是,当该属性为 false 时,标签保持可见。如果我在 XAML 中将 IsVisible 属性设置为 false,则标签不可见,因此问题似乎与绑定(bind)有关。

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>

我的 View 模型:

public bool HasNotifications
{
get => this.hasNotifications;
set => this.SetProperty(ref this.hasNotifications, value);
}

最佳答案

我认为您没有设置正确的BindingContextHasNotificationsViewModel 的一个属性,而您为标签设置的 BindingContext 是 DashboardPageView。

我写了一个简单的演示,希望你能从中得到一些想法:

在 xaml 中:

<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;
}
}
}

如果有任何问题,请随时问我。

关于xamarin.forms - 将 IsVisible 绑定(bind)到 Xamarin Forms 中的 bool 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63360405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com