【发布时间】:2020-10-20 14:09:04
【问题描述】:
我正在尝试为我的页面使用转换器,但无济于事。
页面的重要部分是一个开关和两个列表视图。这些 Listview 一个在另一个之上,根据开关的状态,应该只显示其中一个。在少数情况下,开关的状态也应该可以通过应用程序进行更改。
为此,我有一个布尔属性 (isPrimaryBin),当它为 true 时,第一个列表视图 (primaryBinListView) 应该是可见的,而第二个应该是隐藏的 (secondaryBinListView)。
当 isPrimaryBin 更改为 false 时,secondaryBinListview 变为可见,primaryListView 变为隐藏。
最后一件事,为了使其更直观,重要的是,当 isPrimaryBin 为 true 时不应切换开关,而当 isPrimaryBin 为 false 时不应切换开关。 (由于标签位于开关的两侧,它象征着从一个垃圾箱到另一个垃圾箱的切换)
起初我试图创建第二个属性,它会输出与 isPrimaryBin 相反的结果,但我永远不会点击 onPropertyChanged。所以我学会了如何制作转换器并制作了一个。起初,它不会撞到它。我摆弄它,意识到我必须将它添加到资源字典中。然后它开始自我循环。页面陷入无限循环。我设法阻止了这种行为,但现在它不再更新页面。尽管更改了开关,但primaryBinListview 始终可见,而secondaryBinListView 始终隐藏。开关改变状态。它确实通过了财产。它确实通过转换器。我不知道为什么在它清楚地完成所有步骤时没有任何变化。
现在进入代码。让我们从属性开始:
public bool isPrimaryBin {
get { return _isPrimaryBin; }
set {
if (_isPrimaryBin != value) {
_isPrimaryBin = value;
OnPropertyChanged();
}
}
}
然后是转换器
public class inverseBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !((bool)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
}
最后是相关的 Xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Picking.Views.Inv2Page"
Title="{Binding Title}"
xmlns:local="clr-namespace:Picking.Helpers">
<ContentPage.Resources>
<ResourceDictionary>
<local:inverseBoolConverter x:Key="inverseBoolConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid RowSpacing="0">
<Grid Grid.Row="0">
<Switch Grid.Column="1" OnColor="Orange" ThumbColor="Gray" IsVisible="{Binding isMoveGoods}" IsToggled="{Binding isPrimaryBin, Mode=TwoWay, Converter={StaticResource inverseBoolConverter}}"/
</Grid>
<ListView x:Name="ItemsListView" Grid.Row="1"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemTapped="ItemsListView_ItemTapped"
Grid.RowSpan="2"
IsVisible="{Binding isPrimaryBin}"
>
</ListView>
<ListView x:Name="ItemsListViewTransfer" Grid.Row="1"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemTapped="ItemsListView_ItemTapped"
Grid.RowSpan="2"
IsVisible="{Binding isPrimaryBin, Converter={StaticResource inverseBoolConverter}}"
>
</ListView>
</ContentPage.Content>
</ContentPage>
我已删除与问题无关的任何内容(例如,列表视图的内容、额外标签或与我的问题无关的任何内容)
这是我第一次使用转换器。我查看了有关如何使它们工作的多个指南,当没有任何工作但至少没有任何问题时,它使我陷入了这种奇怪的困境。
感谢您的帮助。
【问题讨论】:
标签: c# android xamarin.forms model-binding converters