2012-05-29 44 views
0

我有一個從遠程XML更新Windows Phone 7的名單

public class Archive 
{ 
    public string fromsms { get; set; } 
    public string sms { get; set; } 
    public string tosms { get; set; } 
    public string status { get; set; } 
    public string date { get; set; } 
} 

<TextBlock Text="From: " TextAlignment="Left" FontSize="16" Foreground="White" FontWeight="Normal" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
<TextBlock Text="{Binding fromsms}" TextWrapping="Wrap" FontSize="16" FontWeight="Bold" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
<TextBlock Text="Date: " FontSize="16" TextWrapping="Wrap" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2" TextAlignment="Left" Margin="0" /> 
<TextBlock Text="{Binding date}" FontSize="16" TextAlignment="Left" Foreground="White" FontWeight="Bold" Grid.Row="0" Grid.Column="3" /> 
<TextBlock Text="To: " TextAlignment="Left" FontSize="16" Foreground="White" Margin="0" Grid.Row="1" Grid.Column="0"/> 
<TextBlock Text="{Binding tosms}" TextWrapping="Wrap" FontSize="16" FontWeight="Bold" Grid.Row="1" Grid.Column="1"/> 
<TextBlock Text="Status:" FontSize="16" TextWrapping="Wrap" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="2" Margin="0" /> 
<TextBlock Text="{Binding status}" FontSize="16" TextAlignment="Left" Foreground="White" FontWeight="Bold" Grid.Row="1" Grid.Column="3"/> 
<TextBlock Text="{Binding sms}" FontSize="20" TextWrapping="Wrap" Width="Auto" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4"/> 

var xmlHistory = XElement.Parse(e.Result); 
listbox1.ItemsSource = from history in xmlHistory .Descendants("archive") 
select new Archive 
{ 
    fromsms = history.Element("fromn").Value, 
    tosms = history.Element("ton").Value, 
    status = history.Element("smsstatus").Value, 
    date = history.Element("time").Value, 
    sms = history.Element("sms").Value, 
}; 

listbox1.Visibility = Visibility.Visible; 
PerformanceProgressBar.Visibility = Visibility.Collapsed; 

「fromn」和「噸」填入以下列表中的電話號碼我要搜索聯繫人,並在列表中更換號碼如果找到聯繫人姓名。

有人可以給我一個示例代碼?

謝謝!

+0

我沒有嘗試過任何東西,因爲我不知道該怎麼做。我知道我可以使用SearchAsync搜索聯繫人名稱(searchterm,FilterKind.PhoneNumber,null);但我不知道如何將結果分配給列表。 –

回答

0

Here你可以找到Windows Phone平臺包含Contacts and Calendar Sample項目和很好的話題代碼樣本約Contacts APIHow to: Access Contact Data for Windows Phone

UPD:

  ObservableCollection<User> users = new ObservableCollection<User>() 
      { 
       new User() { phone = "345342645" }, 
       new User() { phone = "134523534" } , 
       new User() { phone = "345435432" } 
      }; 

      Contacts contacts = new Contacts(); 
      contacts.SearchCompleted += (s, e) => 
      { 
       var u = (from Contact con in e.Results 
         from ContactPhoneNumber phones in con.PhoneNumbers 
         from user in users 
         where phones.PhoneNumber.Replace("+", "").Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "").Equals(user.phone) 
         select new User() { name = con.DisplayName, phone = user.phone }).ToList(); 

       foreach (User user in u) 
       { 
        Debug.WriteLine(user.name + " " + user.phone); 
       } 
      }; 
      contacts.SearchAsync(String.Empty, FilterKind.None, String.Empty); 

User實現是未來:

public class User 
{ 
    public string phone { get; set; } 
    public string name { get; set; } 
} 
+0

嗨,我閱讀了API。我知道如何用數字搜索,但我不知道如何從循環中獲取數字並將其分配給列表框而不是數字。 –

+0

'Contacts_SearchCompleted'中的'listbox2.ItemsSource = e.Results'回調 – Ku6opr

+0

嗨,我不明白可以這樣做,你能否舉個例子。 –