【问题标题】:Xamarin Boolean converter: does not update page despite going through the converterXamarin 布尔转换器:尽管通过转换器,但不更新页面
【发布时间】: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


    【解决方案1】:

    看起来你的两个listView在开关状态改变时加载了相同的数据,所以看起来UI没有改变。也许你可以展示你的viewmodel。这样会更清楚。

    下面是您的代码的简单示例,如果您愿意?

    ConvertModel(您可以替换自己的视图模型):

     class ConvertModel :INotifyPropertyChanged
    {
        ObservableCollection<string> items = new ObservableCollection<string>();
        public ObservableCollection<string> Items { get { return items; } }
        private bool _isPrimaryBin;
        public bool isPrimaryBin
        {
            get { return _isPrimaryBin; }
            set
            {
    
                _isPrimaryBin = value;
                if (_isPrimaryBin)
                {
                    items.Clear();
                    items.Add("aaa");
                    items.Add("bbb");
                    items.Add("ccc");
                    items.Add("ddd");
                    items.Add("eee");
                    items.Add("fff");
                    items.Add("ggg");
                }
                else
                {
                    items.Clear();
                    items.Add("111");
                    items.Add("222");
                    items.Add("333");
                    items.Add("444");
                    items.Add("555");
                    items.Add("666");
                    items.Add("777");
                }
                OnPropertyChanged();
    
            }
        }
    
        public ConvertModel(bool isPr)
        {
            _isPrimaryBin = isPr;
    
            if (isPr)
            {
                items.Add("aaa");
                items.Add("bbb");
                items.Add("ccc");
                items.Add("ddd");
                items.Add("eee");
                items.Add("fff");
                items.Add("ggg");
            }
            else
            {
                items.Add("111");
                items.Add("222");
                items.Add("333");
                items.Add("444");
                items.Add("555");
                items.Add("666");
                items.Add("777");
            }
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    page.xaml:

    <ContentPage.Resources>
        <ResourceDictionary>
            <local:inverseBoolConverter x:Key="inverseBoolConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
    
        <StackLayout Orientation="Vertical">
            <Switch Grid.Column="1" OnColor="Orange" ThumbColor="Gray"  IsToggled="{Binding isPrimaryBin, Mode=TwoWay, Converter={StaticResource inverseBoolConverter}}"></Switch>
    
            <ListView x:Name="ItemsListView" 
            ItemsSource="{Binding Items}"
            VerticalOptions="FillAndExpand"
            HasUnevenRows="true"
            IsPullToRefreshEnabled="true"
            CachingStrategy="RecycleElement"
            IsVisible="{Binding isPrimaryBin}"
             >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding .}"></Label>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <ListView x:Name="ItemsListViewTransfer" 
            ItemsSource="{Binding Items}"
            VerticalOptions="FillAndExpand"
            HasUnevenRows="true"
            IsPullToRefreshEnabled="true"
            CachingStrategy="RecycleElement"
            IsVisible="{Binding isPrimaryBin, Converter={StaticResource inverseBoolConverter}}"
              >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label Text="{Binding .}"></Label>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
    

    page.xaml.cs:

    public ConvertPage()
      {
          InitializeComponent();
          ConvertModel convertModel = new ConvertModel(false);
          BindingContext = convertModel;
      }
    

    效果如下:

    【讨论】:

    • 这就是问题所在。我一直在学习新东西,最后我没有仔细检查它是否显示了正确的东西。
    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    • 2014-03-23
    • 1970-01-01
    • 2011-06-18
    • 2019-06-06
    相关资源
    最近更新 更多