【问题标题】:ObservableCollection populated but will not show up in android displayObservableCollection 已填充但不会显示在 android 显示中
【发布时间】:2020-08-24 16:08:44
【问题描述】:

我在使用 Visual Studio 2019 中的 Android 代码方面还很陌生。这里想要的最终结果是能够在手机附近显示各种 wifi 接入点。我已经让 wifi 扫描工作,我可以在“WifiScans”集合中看到扫描结果,但 listWifiScan ListView 没有在显示屏上显示结果。我可以在显示屏上看到应该显示的项目数量的线条。如果我触摸其中一个项目,我可以看到它变成纯色(橙色)但不显示信息。谁能告诉我我做错了什么?感谢您提供的任何帮助。

using Android;
using Android.Content.PM;
using Android.Support.V4.App;
using Android.Support.V4.Content;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Xamarin.Forms;


namespace MobileWifi
{
    public partial class MainPage : ContentPage
    {
        ObservableCollection<WifiScan> WifiScans { get; set; }

        Label lblTitle;
        ListView listWifiScan;
        readonly Button btnWifiScan;
        public static bool response = false;

        public MainPage()
        {
            // add code to handle the components here
            WifiScans = new ObservableCollection<WifiScan>();
            this.Padding = new Thickness(20,20,20,20);    // wall padding for page

            // Listview data template
            var scanDataTemplate = new DataTemplate(() =>
            {
                var grid = new Grid();
                var bssidLabel = new Label { FontAttributes = FontAttributes.Bold };
                var ssidLabel = new Label();
                var levelLabel = new Label { HorizontalTextAlignment = TextAlignment.End };
                bssidLabel.SetBinding(Label.TextProperty, "BSSID");
                ssidLabel.SetBinding(Label.TextProperty, "SSID");
                levelLabel.SetBinding(Label.TextProperty, "Level");

                grid.Children.Add(bssidLabel);
                grid.Children.Add(ssidLabel, 1, 0);
                grid.Children.Add(levelLabel, 2, 0);

                return new ViewCell { View = grid };
            });
            
            // setup StackLayout for controls and set spacing of controls within the layout
            StackLayout panel = new StackLayout
            {
                Spacing = 15,
                Margin = new Thickness(20),
            };
            panel.Children.Add(lblTitle = new Label
            {
                Text = "Mobile Wifi Scanner",
                HorizontalTextAlignment = TextAlignment.Center,
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                FontAttributes = FontAttributes.Bold,
            });
            panel.Children.Add(btnWifiScan = new Button
            {
                Text = "Start Scan"
            });
            panel.Children.Add(listWifiScan = new ListView
            {
                ItemsSource = WifiScans,
                HasUnevenRows = true,
                ItemTemplate = scanDataTemplate,
                Margin = new Thickness(0, 20, 0, 0),
            });


            btnWifiScan.Clicked += OnBtnWifiScanClicked;
            this.Content = panel;
        }

        private void OnBtnWifiScanClicked(object sender, EventArgs e)
        {

            btnWifiScan.Text = "Scanning...";
            WifiScans.Clear();  // clear out results from previous scan

            try
            {
                IWifiScan service = DependencyService.Get<IWifiScan>().GetObj();
                service.Start();
                service.Finished += WifiScanDone;
            }
            catch (Exception ex)
            {
                btnWifiScan.Text = "Start Scan";
                DisplayAlert("Alert", ex.Message, "OK");
            }
        }

        public void WifiScanDone(object sender,EventArgs e)
        {
            IWifiScan service = DependencyService.Get<IWifiScan>();
            List<string> WifiBSSID = service.GetBSSID();
            List<string> WifiSSID = service.GetSSID();
            List<int> WifiLevel = service.GetLevel();
            int count = WifiBSSID.Count;

            // add logic here to display the data from the scan
            // add results of the scan
            if(count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    WifiScans.Add(new WifiScan { WifiBSSID = WifiBSSID[i], WifiSSID = WifiSSID[i], WifiLevel = WifiLevel[i].ToString()});
                }
            }else
            {
                WifiScans.Add(new WifiScan { WifiBSSID = "None Found", WifiSSID = "", WifiLevel = "0"});
            }
            
            btnWifiScan.Text = "Start Scan";
            // finalize the scan, etc.
            service.Done();
        }

    }
}

【问题讨论】:

    标签: android observablecollection


    【解决方案1】:

    我终于找到了问题所在。事实证明我没有在绑定中指定正确的属性。数据模板中绑定的正确代码应该是:

                    bssidLabel.SetBinding(Label.TextProperty, "WifiBSSID");
                    ssidLabel.SetBinding(Label.TextProperty, "WifiSSID");
                    levelLabel.SetBinding(Label.TextProperty, "WifiLevel");
    

    我不确定我从哪里得到原始值,但在研究了数据绑定属性后,我意识到它们是错误的。此外,我没有在原始问题中包含有助于识别问题的来源,因此我在此处添加它,以防它对可能遇到类似问题的其他人有所帮助。

        public class WifiScan
        {
            public string WifiSSID { get; set; }
            public string WifiBSSID { get; set; }
            public string WifiLevel { get; set; }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2013-01-19
      相关资源
      最近更新 更多