【问题标题】:Xamarin Forms MVVM - Test source data for blank in listviewXamarin Forms MVVM - 测试列表视图中空白的源数据
【发布时间】:2018-09-10 21:25:33
【问题描述】:

我修改了一些示例代码来显示一个列表视图,并让它为我的目的工作。然而,一些源数据是空白的,这会产生一个空白行,例如,Spouse2 通常是空白的。我想测试空白而不生成该行。具体来说,我如何测试这个语句检索到的源数据 s2Label.SetBinding(Label.TextProperty, new Binding("Spouse2")); 在 CustomTreeBranches 类中绕过 vlayout.Children.Add(s2Label); 陈述?

这里是类:

public partial class MainView : ContentPage
    {
        public ObservableCollection<Models.TreeBranches> branches { get; set; }
        public MainView()
        {
            InitializeComponent();
            branches = new ObservableCollection<Models.TreeBranches>();
            ListView viewList = new ListView();
            this.Title = "Family Tree";
            viewList.ItemTemplate = new DataTemplate(typeof(CustomTreeBranches));
            branches.Add(new Models.TreeBranches {
                BranchNumber = 1,
                FullName = "Melvin Sneerd",
                Birthday ="1/22/1982",
                Father = "Dick Sneerd",
                Mother = "Hazel Haze",
                Spouse1 = "Lucky Lady",
                Spouse2 = "",
                Spouse3 = "",
                Child1 = "John Sneerd",
                Child2 = "Donna Sneerd",
                Child3 = "",
                Child4 = "",
                Residence = "Malibu, California",
                Image = "Photos/filler.jpg" });
            branches.Add(new Models.TreeBranches {
                BranchNumber = 2,
                FullName = "Lucky Lady",
                Birthday ="11/31/1980",
                Father = "Papa Joe",
                Mother = "Mama Bear",
                Spouse1 = "Melvin Sneerd",
                Spouse2 = "",
                Spouse3 = "",
                Child1 = "John Sneerd",
                Child2 = "Donna Sneerd",
                Child3 = "",
                Child4 = "",
                Residence = "Highlands Ranch, Colorado",
                Image = "Photos/fill.jpg" })
public class CustomTreeBranches : ViewCell 
            {
            public CustomTreeBranches()
            {
                var img = new Image();
                var bdLabel = new Label();
                var nameLabel = new Label();
                var bnLabel = new Label();
                var frLabel = new Label();
                var mrLabel = new Label();
                var c1Label = new Label();
                var c2Label = new Label();
                var c3Label = new Label();
                var c4Label = new Label();
                var s1Label = new Label();
                var s2Label = new Label();
                var s3Label = new Label();
                var rdLabel = new Label();
                var sp0Label = new Label()
                {
                    Text = "Branch Member",
                    FontAttributes = FontAttributes.Bold,
                    HorizontalOptions = LayoutOptions.Center
                };
                    var sp1Label = new Label()
                {
                    Text = "Father and Mother",
                    FontAttributes = FontAttributes.Bold,
                    HorizontalOptions = LayoutOptions.Center
                };
                var sp2Label = new Label()
                {
                    Text = "Children",
                    FontAttributes = FontAttributes.Bold,
                    HorizontalOptions = LayoutOptions.Center
                }; 
                var sp3Label = new Label()
                {
                    Text = "Spouse(s)",
                    FontAttributes = FontAttributes.Bold,
                    HorizontalOptions = LayoutOptions.Center
                }; 
                var sp4Label = new Label()
                {
                    Text = "Latest Residence",
                    FontAttributes = FontAttributes.Bold,
                    HorizontalOptions = LayoutOptions.Center
                }; 
                var vLayout = new StackLayout();
                var hLayout = new StackLayout() { BackgroundColor = Color.LavenderBlush };
                img.SetBinding(Image.SourceProperty, new Binding("Image"));
                bdLabel.SetBinding(Label.TextProperty, new Binding("Birthday"));
                nameLabel.SetBinding(Label.TextProperty, new Binding("FullName"));
                //bnLabel.SetBinding(Label.TextProperty, new Binding("BranchNumber"));                
                frLabel.SetBinding(Label.TextProperty, new Binding("Father"));
                mrLabel.SetBinding(Label.TextProperty, new Binding("Mother"));
                c1Label.SetBinding(Label.TextProperty, new Binding("Child1"));
                c2Label.SetBinding(Label.TextProperty, new Binding("Child2"));
                c3Label.SetBinding(Label.TextProperty, new Binding("Child3"));
                c4Label.SetBinding(Label.TextProperty, new Binding("Child4"));
                s1Label.SetBinding(Label.TextProperty, new Binding("Spouse1"));
                s2Label.SetBinding(Label.TextProperty, new Binding("Spouse2"));
                s3Label.SetBinding(Label.TextProperty, new Binding("Spouse3"));
                rdLabel.SetBinding(Label.TextProperty, new Binding("Residence"));
                hLayout.Orientation = StackOrientation.Horizontal;
                hLayout.HorizontalOptions = LayoutOptions.Fill;
                img.HorizontalOptions = LayoutOptions.End;
                img.WidthRequest = 250;
                img.HeightRequest = 250;
                nameLabel.FontSize = 18;

                vLayout.Children.Add(sp0Label);
                vLayout.Children.Add(nameLabel);
                vLayout.Children.Add(bdLabel);
                //vLayout.Children.Add(bnLabel);
                vLayout.Children.Add(sp1Label);
                vLayout.Children.Add(frLabel);
                vLayout.Children.Add(mrLabel);
                vLayout.Children.Add(sp2Label);
                vLayout.Children.Add(c1Label);
                vLayout.Children.Add(c2Label);
                vLayout.Children.Add(c3Label);
                vLayout.Children.Add(c4Label);
                vLayout.Children.Add(sp3Label);
                vLayout.Children.Add(s1Label);
                vLayout.Children.Add(s2Label);
                vLayout.Children.Add(s3Label);            
                vLayout.Children.Add(sp4Label);
                vLayout.Children.Add(rdLabel);
                hLayout.Children.Add(vLayout);
                hLayout.Children.Add(img);
                View = hLayout;
            }
        }
    }
}

【问题讨论】:

    标签: listview xamarin mvvm xamarin.forms


    【解决方案1】:

    我的建议是为标签的 IsVisible 属性添加一个新绑定,然后使用 value converter 来切换可见性,类似于 s2Label.SetBinding(IsVisibleProperty, "Spouse2", BindingMode.Default, new MyValueConverter());,您的值转换器可能看起来像

    public class MyValueConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var myString = value as string;
            return (myString == null) ? false : !string.IsNullOrWhiteSpace(myString);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    • 我可能误解了,因为我收到此代码的错误“SetBinding 没有重载需要 4 个参数”:s2Label.SetBinding(Label.IsVisibleProperty, new Binding("Spouse2"), BindingMode.Default, new MyValueConverter());
    • 对不起。我犯了两个错误,已在答案中更正。第一个参数是IsVisibleProperty,第二个参数就是"Spouse2"。请注意,如果可能的话,您可能想做nameof(Spouse2) 之类的事情,这样您就不会让所有额外的字符串四处飘荡。
    • 谢谢,我现在可以使用了,但我仍然需要帮助测试标签。我使用这些语句s2Label.SetBinding(IsVisibleProperty, "Spouse2", BindingMode.Default, new MyValueConverter()); string str = s2Label.GetValue(Label.TextProperty).ToString(); ,但它返回一个异常,“对象引用未设置为对象的实例。”有什么想法吗?
    • 不确定。通常最好针对新问题提出新问题,以便更多人看到它。如果此答案对您有用,请随时投票并标记为答案。
    猜你喜欢
    • 2018-07-18
    • 2021-06-25
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2018-10-06
    相关资源
    最近更新 更多