2

在使用 LongListSelector 时,我是 Windows phone 8 的新手。我正在尝试从 MySQL 数据库中获取列表并将其绑定到 LongListSelector 中。在我的代码中,它只显示 MessageBox 对话框而不是获取列表。可能是什么问题呢。或者我是否将用于获取列表的代码放在了错误的方法中。请帮忙..

要绑定到 LongListSelector 的字符串是 f1,ListLongselector name= ListCompanies

public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the LongListSelector control to the sample data
            DataContext = App.ViewModel;

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //if (!App.ViewModel.IsDataLoaded)
            //{
            //    App.ViewModel.LoadData();
            //}

            string url = "http://localhost/taxi/fetch_nrb.php";
            WebClient webclientmenu = new WebClient();
            webclientmenu.DownloadStringCompleted += webclientmenu_DownloadStringCompleted;
            webclientmenu.DownloadStringAsync(new Uri(url));
        }

        // Handle selection changed on LongListSelector
        private void MainLongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //// If selected item is null (no selection) do nothing
            //if (MainLongListSelector.SelectedItem == null)
            //    return;

            //// Navigate to the new page
            //NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + (MainLongListSelector.SelectedItem as ItemViewModel).ID, UriKind.Relative));

            //// Reset selected item to null (no selection)
            //MainLongListSelector.SelectedItem = null;


        }

        void webclientmenu_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            try
            {
                string list = e.Result;
                string[] final = list.Split('#');
                string f1 = final[0];

                for (int i = 0; i < f1.Length; i++)
                {
                    ListCompanies.ItemsSource.Add(f1[i]);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Check Your Internet Connectivity and try again later.\n No Network Connection", "Network Error!", MessageBoxButton.OK);

            }
        }
4

2 回答 2

0

据我了解,您正在尝试为每个列表项显示一个字符?

如果是,我建议将 foreach 循环替换为:

ListCompanies.ItemsSource = f1;

如果要显示最终列表中的每个字符串,每个项目一个字符串:

ListCompanies.ItemsSource = final;

您遇到未实现的异常,因为 ItemsSource 属性被分配给您创建的列表。

于 2013-04-16T11:26:27.893 回答
0

您应该更改此代码;

// Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //if (!App.ViewModel.IsDataLoaded)
            //{
            //    App.ViewModel.LoadData();
            //}

            string url = "http://localhost/taxi/fetch_nrb.php";
            WebClient webclientmenu = new WebClient();
            webclientmenu.DownloadStringCompleted += webclientmenu_DownloadStringCompleted;
            webclientmenu.DownloadStringAsync(new Uri(url));
        }

有了这个 ;

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

并使用“LongListSelector_Loaded”事件制作您的加载事件。为此,请转到您的 xaml 端,单击 longlistselector,单击事件并双击“已加载”框。然后去那里写下你的请求,像这样;

 private void MainLongListSelector_Loaded(object sender, RoutedEventArgs e)
        {
            string url = "http://localhost/taxi/fetch_nrb.php";
            WebClient webclientmenu = new WebClient();
            webclientmenu.DownloadStringCompleted += webclientmenu_DownloadStringCompleted;
            webclientmenu.DownloadStringAsync(new Uri(url));
        }

祝你好运。

于 2013-06-06T11:13:56.137 回答