-1

我是 Windows 手机应用程序开发的新手。我制作了一个应用程序,我使用 Web 客户端向 MySQL 中的数据库请求数据。我从该请求中得到响应,但问题是我使用带有项目模板和数据模板的列表框在列表框中绑定响应数据。当我更改传递给 Web 客户端的 URL 时,它无法在列表框中绑定数据。即使只是文本块的文本也不会显示。但是当我从中删除数据模板和项目模板时列表框将显示静态数据但不显示绑定数据。请任何人帮助我...

在此先感谢。我希望有人能尽快帮助我...我的 C# 代码是:

public partial class chat_match_settings : PhoneApplicationPage
{
    public chat_match_settings()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(profile_loaded);           
    }
    void profile_loaded(object sender, RoutedEventArgs e)
    {
        WebClient wc = new WebClient();

        var URI = new Uri("<http://www.mylocalipaddress.com/eyematch/rest1-7/api/>?");

        wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";

        wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);

        wc.UploadStringAsync(URI, "POST", "action=login&uName=hiren&uPass=hiren123");         
    }       
    private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
       try
       {
           var jsondata = JsonConvert.DeserializeObject<RootObject>(e.Result);

           DisplayList list = new DisplayList(jsondata.profile.DisplayData);

           //profilelistview.DataContext = this;

           profilelistview.ItemsSource = list;

       }
        catch (Exception exc)

        {}        
       }

    public class DisplayList : List<DisplayData>
    {
        public DisplayList(DisplayData data)

        {
                          Add(new DisplayData
            {
                Username = data.FullName.ToString(),

                countryname = data.countryname.ToString(),

                Sex = data.Sex.ToString(),

                City = data.City.ToString(),

                AboutMyself = data.AboutMyself.ToString(),

                Job = data.Job.ToString(),

                Location = data.Location.ToString(),

                MatchSex = data.MatchSex.ToString(),

                Iam = data.Iam.ToString(),

                StartWorkHour = data.StartWorkHour.ToString(),

                Age = data.Age,

                EndWorkHour = data.EndWorkHour.ToString(),

                Language = data.Language,

                MessageMe = data.MessageMe.ToString(),

                Ilove = data.Ilove.ToString(),

                LookingFor = data.LookingFor.ToString(),

                BodyDescription = data.BodyDescription });


        }
    }
    public class DisplayList1 : List<ProfileImage>
    {
        public DisplayList1(ProfileImage profileImage)
        {
            // TODO: Complete member initialization

            Add(new ProfileImage
                    { Value = "<http://localipaddress.com/eyematch/upload/images/2/>?" + profileImage.Value });

        }

和xml代码是:

                                 <Image x:Name="matchimage"/><TextBlock x:Name="matchpercentage"/>

                                <TextBlock Text="{Binding Path=UserFeedBackForDisplay}"></TextBlock>
                                <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path= Sex}" Foreground="Gray"  FontSize="16" Margin="0"/>
                                    <TextBlock Text="/" FontSize="16"/>
                                    <TextBlock Text="{Binding Path=Sex}" Foreground="Gray" FontSize="16"></TextBlock>
                                    <TextBlock Text="/" FontSize="16"/>
                                    <TextBlock Text="{Binding Path=Job}" Foreground="Gray" FontSize="16"></TextBlock>
                                    <TextBlock Text="/" FontSize="16"/>
                                    <!--<ListBox Name="language"    Foreground="Gray" FontSize="16">
                                        <ListBox.ItemTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding Path=contactdata.Title}" Foreground="Gray" FontSize="16"></TextBlock>
                                            </DataTemplate>
                                        </ListBox.ItemTemplate>
                                    </ListBox>-->
                                </StackPanel>


                                <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
                                    <TextBlock Text="{Binding Path=City}" Foreground="Gray" FontSize="16"></TextBlock>
                                    <TextBlock Text="/" FontSize="16"/>
                                    <TextBlock Text="{Binding Path=countryname}" Foreground="Gray" FontSize="16"></TextBlock>
                                </StackPanel>

                                <StackPanel Margin="0,10,0,0">

                                </StackPanel>

                                <StackPanel Orientation="Vertical" Margin="0,10,0,0">
                                    <TextBlock Text="About MySelf"  FontSize="30" ></TextBlock>
                                    <TextBlock Text="{Binding Path=AboutMyself}" Foreground="Gray" Height="60" Width="Auto" TextWrapping="Wrap"  FontSize="16"></TextBlock>

                                </StackPanel>
                                <StackPanel Orientation="Vertical" Margin="0,10,0,0">
                                    <TextBlock Text="I Am"  FontSize="30" ></TextBlock>

                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="I 'm " Foreground="Gray" FontSize="16" />
                                        <TextBlock Text="{Binding Path=BodyDescription.BodyStyle}" Foreground="Gray"  Width="Auto" TextWrapping="Wrap"  FontSize="16"></TextBlock>
                                        <TextBlock Text="/" FontSize="16"/>
                                        <TextBlock Text="{Binding Path=BodyDescription.Hair}" Foreground="Gray"  Width="Auto" TextWrapping="Wrap" FontSize="16"></TextBlock>
                                        <TextBlock Text=" Hair " Foreground="Gray" FontSize="16" />
                                        <TextBlock Text="/" FontSize="16"/>
                                        <TextBlock Text="{Binding Path=BodyDescription.Eyes}" Foreground="Gray" FontSize="16"></TextBlock>
                                        <TextBlock Text=" Eyes " Foreground="Gray" FontSize="16" />
                                    </StackPanel>
                                    <TextBlock Text="{Binding Path=Iam}" Foreground="Gray" FontSize="16"></TextBlock>
                                </StackPanel>
                                <StackPanel Margin="0,10,0,0">
                                    <TextBlock Text="I Love" FontSize="30"></TextBlock>
                                    <TextBlock Text="{Binding Path=Ilove}" Foreground="Gray" FontSize="16"></TextBlock>
                                </StackPanel>
                                <StackPanel Orientation="Vertical" Margin="0,10,0,0">
                                    <TextBlock Text="I'm Looking For" FontSize="26"></TextBlock>
                                    <TextBlock Text="{Binding Path=LookingFor}"  Foreground="Gray" FontSize="16"></TextBlock>
                                </StackPanel>
                                <StackPanel Orientation="Vertical" Margin="0,10,0,0">
                                    <TextBlock Text="Message Me" FontSize="30"></TextBlock>
                                    <TextBlock Text="{Binding Path=MessageMe}"  Foreground="Gray" FontSize="16"></TextBlock>
                                </StackPanel>
                                <StackPanel Orientation="Vertical" Margin="0,10,0,0">
                                    <TextBlock Text="Match Sex"  FontSize="30" ></TextBlock>
                                    <TextBlock Text="{Binding Path=MatchSex}" Foreground="Gray" FontSize="16"></TextBlock>
                                </StackPanel >
                                <StackPanel  Orientation="Vertical" Margin="0,10,0,0">
                                    <TextBlock Text="Start Working Hour"  FontSize="30" ></TextBlock>
                                    <TextBlock Text="{Binding Path=StartWorkHour}" Foreground="Gray"  FontSize="16" ></TextBlock>
                                </StackPanel>
                                <StackPanel Margin="0,10,0,0">
                                    <TextBlock Text="End Working Hour"  FontSize="30" ></TextBlock>
                                    <TextBlock Text="{Binding Path=EndWorkHour}" Foreground="Gray"  FontSize="16"></TextBlock>
                                </StackPanel>
                                <TextBlock Text="{Binding  Username}" FontSize="16"></TextBlock>
                                <TextBlock Text="{Binding Path=Location}" Foreground="Gray" FontSize="16"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
4

2 回答 2

1

如果数据在 e.Result 中以您希望在列表框中显示的方式正确出现。您可以查看以下内容。这里我从 wcf 服务获取数据。

假设在您的 mainPage.xaml.cs 中,我们可以添加以下代码

因为这里我使用的是 wcf 服务,所以我们需要编写以下内容

    // Constructor
             public MainPage()
             {
                 InitializeComponent();


                  UserServiceReference.ServiceClient client = new ServiceClient();

                   //Event handler after webservice completes operation.
                  client.UserDetailsCompleted += new EventHandler<UserDetailsCompletedEventArgs>                                            

                                  (serviceClient_UserDetailsCompleted);
                  client.UserDetailsAsync();

             }    
         }


    //Completed event of the UserDetails
         public void serviceClient_UserDetailsCompleted(Object sender,UserDetailsCompletedEventArgs e )
             {
                  try
                 {

                     var lsdUser = e.Result.ToList();

                     foreach (var userData in lsdUser)
                     {

                         User userObj = new User();
                         userObj.UserName =userData[0].ToString(); 
                         userObj. userCity = userData[1].ToString();
                         userObj. userState=userData[2].ToString();
                         userObj. userGender=userData[3].ToString();
                         userObj. userAge=userData[4].ToString();
                         userObj. userDescription=userData[5].ToString();

                         userList.Add(userObj);

                     }

                     //Binding Data to the userListBox
                     userListBox.ItemsSource = userList;

                 }
                 catch (Exception ex)
                 {
                     string message = ex.ToString();
                 }

             }

在 Xaml 中,我们可以使用 itemTemplate 和 dataTemplate 绑定数据

    <ListBox Height="279"
                          Name="userListBox"
                          Background="White"
                          ScrollViewer.VerticalScrollBarVisibility="Auto"
                          HorizontalAlignment="Stretch"
                          Margin="0,5,0,0"
                          >
                     <ListBox.ItemsPanel>
                         <ItemsPanelTemplate>
                             <StackPanel Orientation="Vertical"></StackPanel>
                         </ItemsPanelTemplate>
                     </ListBox.ItemsPanel>
                     <ListBox.ItemTemplate>
                         <DataTemplate>
                             <StackPanel Orientation="Horizontal"
                                         x:Name="user"
                                         Width="600">
                                 <TextBlock x:Name="txtUserName"
                                            Text="{Binding UserName}"
                                            Foreground="Black"
                                            Width="100"
                                            Height="65"
                                            TextWrapping="Wrap"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Margin="20,0,0,0">
                                 </TextBlock>

                                 <TextBlock x:Name="txtUserCity"
                                            Text="{Binding UserCity}"
                                            Foreground="Black"
                                            Width="100"
                                            Height="65"
                                            TextWrapping="Wrap"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Margin="20,0,0,0">
                                 </TextBlock>

                                 <TextBlock x:Name="txtUserState"
                                            Text="{Binding UserState}"
                                            Foreground="Black"
                                            Width="100"
                                            Height="65"
                                            TextWrapping="Wrap"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Margin="20,0,0,0">
                                 </TextBlock>

                                 <TextBlock x:Name="txtUserGender"
                                            Text="{Binding UserGender}"
                                            Foreground="Black"
                                            Width="100"
                                            Height="65"
                                            TextWrapping="Wrap"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Margin="20,0,0,0">
                                 </TextBlock>

                                 <TextBlock x:Name="txtUserAge"
                                            Text="{Binding UserAge}"
                                            Foreground="Black"
                                            Width="100"
                                            Height="65"
                                            TextWrapping="Wrap"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Margin="20,0,0,0">
                                 </TextBlock>

                                 <TextBlock x:Name="txtUserDescription"
                                            Text="{Binding UserDescription}"
                                            Foreground="Black"
                                            Width="100"
                                            Height="65"
                                            TextWrapping="Wrap"
                                            HorizontalAlignment="Center"
                                            VerticalAlignment="Center"
                                            Margin="20,0,0,0">
                                 </TextBlock>

                             </StackPanel>
                         </DataTemplate>
                     </ListBox.ItemTemplate>
                 </ListBox>

希望它在某种程度上对您有所帮助,唯一的区别是我从 wcf 服务获取数据。

于 2013-09-06T04:44:24.523 回答
0

在上面的示例中,我使用了 wcf 服务,对于与数据库的连接,我们使用了 ADO.Net 实体数据模型。

然后在 IService.cs 中我们需要声明一个方法

          [OperationContract]
          List<string[]> UserDetails();

然后在Service.cs中我们需要定义这个Method

        public List<string[]> UserDetails()
        {
            List<string[]> lsdUserInfoDetails =
                                    new List<string[]>();
            string[] allUserList;

            try
            {
                userDataConnection userContext = new
                                                   userDataConnection();

                var query = (from Info in userContext.UserInfo
                               select Info).ToList();

                foreach(var userdetails in query)
                   {
                       allUserList = new string[8];
                       allUserList[0] = userdetails.UserName.ToString();
                       allUserList[1] = userdetails.UserCity.ToString();
                       allUserList[2] = userdetails.UserState.ToString();
                       allUserList[3] = userdetails.UserGender.ToString();
                       allUserList[4] = userdetails.UserAge.ToString();
                       allUserList[5] = userdetails.UserDescription.ToString();

                       lsdUserInfoDetails.Add(allUserList);  
                   }


            }
            catch (Exception es)
            {

            }

            return lsdUserInfoDetails;
        }

并在 MainPage.xaml.cs 中添加了以下代码片段

public partial class MainPage : PhoneApplicationPage
    {
       List<User> userList = new List<User>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();


             UserServiceReference.ServiceClient client = new ServiceClient();

              //Event handler after webservice completes operation.
             client.UserDetailsCompleted += new EventHandler<UserDetailsCompletedEventArgs>                                           

                             (serviceClient_UserDetailsCompleted);
             client.UserDetailsAsync();

        }   
    }

    //Completed event of the UserDetails
    public void serviceClient_UserDetailsCompleted(Object sender,UserDetailsCompletedEventArgs e )
        {
             try
            {

                var lsdUser = e.Result.ToList();

                foreach (var userData in lsdUser)
                {

                    User userObj = new User();
                    userObj.UserName =userData[0].ToString();
                    userObj. userCity = userData[1].ToString();
                    userObj. userState=userData[2].ToString();
                    userObj. userGender=userData[3].ToString();
                    userObj. userAge=userData[4].ToString();
                    userObj. userDescription=userData[5].ToString();

                    userList.Add(userObj);

                }

                //Binding Data to the userListBox
                userListBox.ItemsSource = userList;

            }
            catch (Exception ex)
            {
                string message = ex.ToString();
            }

        }







    /// <summary>
    /// User class
    /// </summary>
    public class User
    {

        private string userName;
        private string userID;
        private string userState;
        private string userCity;
        private string userGender;
        private string userAge;


        public string UserID
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
            }
        }

        public string UserState
        {
            get
            {
                return userState;
            }
            set
            {
                userState = value;
            }
        }

        public string UserCity
        {
            get
            {
                return userCity;
            }
            set
            {
                userCity = value;
            }
        }

        public string UserAge
        {
            get
            {
                return userAge;
            }
            set
            {
                userAge = value;
            }
        }

        public string UserGender
        {
            get
            {
                return userGender;
            }
            set
            {
                userGender = value;
            }
        }

    }
于 2013-09-06T12:13:34.373 回答