2013-12-18 38 views
1

我從VS2013生成的項目中複製了ObservableDictionary實現。它是一個強類型實現(鍵是字符串,值是對象)。但是,我必須得到綁定錯誤,因爲我沒有看到任何數據,我得到一個BindingExpression路徑錯誤。 我注意到的一件事是MapChangedEventHandler始終爲空,這意味着沒有人爲每個事件註冊! 1.誰或什麼註冊MapChangedEventHandler? 2.我的綁定應該是什麼?我嘗試過幾種不同的綁定。一切都無濟於事,但這裏的代碼的最新排列:在Windows Phone中綁定ObservableDictionary時遇到問題

public class ObservableDictionary : IObservableMap<string, object> 
{ 
    private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<string> 
    { 
     public ObservableDictionaryChangedEventArgs(CollectionChange change, string key) 
     { 
      this.CollectionChange = change; 
      this.Key = key; 
     } 

     public CollectionChange CollectionChange { get; private set; } 
     public string Key { get; private set; } 
    } 

    private Dictionary<string, object> _dictionary = new Dictionary<string, object>(); 
    public event MapChangedEventHandler<string, object> MapChanged; 

    private void InvokeMapChanged(CollectionChange change, string key) 
    { 
     var eventHandler = MapChanged; 
     if (eventHandler != null) 
     { 
      eventHandler(this, new ObservableDictionaryChangedEventArgs(change, key)); 
     } 
    } 

    public void Add(string key, object value) 
    { 
     this._dictionary.Add(key, value); 
     this.InvokeMapChanged(CollectionChange.ItemInserted, key); 
    } 

    public void Add(KeyValuePair<string, object> item) 
    { 
     this.Add(item.Key, item.Value); 
    } 

    public bool Remove(string key) 
    { 
     if (this._dictionary.Remove(key)) 
     { 
      this.InvokeMapChanged(CollectionChange.ItemRemoved, key); 
      return true; 
     } 
     return false; 
    } 

    public bool Remove(KeyValuePair<string, object> item) 
    { 
     object currentValue; 
     if (this._dictionary.TryGetValue(item.Key, out currentValue) && 
      Object.Equals(item.Value, currentValue) && this._dictionary.Remove(item.Key)) 
     { 
      this.InvokeMapChanged(CollectionChange.ItemRemoved, item.Key); 
      return true; 
     } 
     return false; 
    } 

    public object this[string key] 
    { 
     get 
     { 
      return this._dictionary[key]; 
     } 
     set 
     { 
      this._dictionary[key] = value; 
      this.InvokeMapChanged(CollectionChange.ItemChanged, key); 
     } 
    } 

    public void Clear() 
    { 
     var priorKeys = this._dictionary.Keys.ToArray(); 
     this._dictionary.Clear(); 
     foreach (var key in priorKeys) 
     { 
      this.InvokeMapChanged(CollectionChange.ItemRemoved, key); 
     } 
    } 

    public ICollection<string> Keys 
    { 
     get { return this._dictionary.Keys; } 
    } 

    public bool ContainsKey(string key) 
    { 
     return this._dictionary.ContainsKey(key); 
    } 

    public bool TryGetValue(string key, out object value) 
    { 
     return this._dictionary.TryGetValue(key, out value); 
    } 

    public ICollection<object> Values 
    { 
     get { return this._dictionary.Values; } 
    } 

    public bool Contains(KeyValuePair<string, object> item) 
    { 
     return this._dictionary.Contains(item); 
    } 

    public int Count 
    { 
     get { return this._dictionary.Count; } 
    } 

    public bool IsReadOnly 
    { 
     get { return false; } 
    } 

    public IEnumerator<KeyValuePair<string, object>> GetEnumerator() 
    { 
     return this._dictionary.GetEnumerator(); 
    } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this._dictionary.GetEnumerator(); 
    } 

    public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) 
    { 
     int arraySize = array.Length; 
     foreach (var pair in this._dictionary) 
     { 
      if (arrayIndex >= arraySize) break; 
      array[arrayIndex++] = pair; 
     } 
    } 
} 

而XAML:

<custom:RESTAPHandler 
x:Class="K1MobilePhone.Views.HomePageAdmin" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:custom="clr-namespace:K1MobilePhone.Utilities" 
xmlns:cells="clr-namespace:K1MobilePhone.Common" 
xmlns:wptoolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
mc:Ignorable="d" 
shell:SystemTray.IsVisible="True" Loaded="phoneApplicationPage_Loaded" Margin="0,6,0,-6"> 
<custom:RESTAPHandler.Resources> 
    <CollectionViewSource 
     x:Name="sd_source" Source="{Binding Path=SDSummaries, Mode=TwoWay}" /> 
    <CollectionViewSource 
     x:Name="ticketsViewSource" Source="{Binding Path=Tickets, Mode=TwoWay}" /> 
</custom:RESTAPHandler.Resources> 

而現在的xaml.cs:

public sealed partial class HomePageAdmin : RESTAPHandler 
{ 
    private ObservableDictionary defaultViewModel = new ObservableDictionary(); 
    public ObservableDictionary DefaultViewModel { get { return this.defaultViewModel; } } 

    public HomePageAdmin() : base() 
    { 
     this.InitializeComponent(); 
     DataContext = this.DefaultViewModel; 
     this.DefaultViewModel["SDSummaries"] = new SDSummaries(); 
     this.DefaultViewModel["Tickets"] = new RecentlyViewedTickets(); 
    } 

回答

0

好吧我沒」無法弄清楚爲什麼我無法在我的xaml代碼中訪問基於字符串的密鑰。默認的Windows商店應用程序效果很好。我清盤做的是這樣的:

Page.xaml:

<custom:RESTAPHandler.Resources> 
    <CollectionViewSource 
     x:Name="sd_source"/> 
    <CollectionViewSource 
     x:Name="ticketsViewSource"/> 
</custom:RESTAPHandler.Resources> 

我建了一切隱藏代碼:

public sealed partial class HomePageAdmin : RESTAPHandler 
{ 
    private Grid visibleGrid; 
    private SDSummaries _sdSummaries = new SDSummaries(); 
    public SDSummaries SDSummaries { get { return this._sdSummaries; } } 

    public HomePageAdmin() : base() 
    { 
     this.InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     this.sd_source.Source = this.SDSummaries; 
     base.OnNavigatedTo(e); 
    } 


    private void phoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     this.Address = this.NavigationContext.QueryString["URL"]; 
    } 

    private void TicketsStatsResponse (TicketsStatsResponse response) 
    { 
     Deployment.Current.Dispatcher.BeginInvoke(() => 
     { 
      PlainTitleDisclosureCellItem cell = (PlainTitleDisclosureCellItem)this.SDSummaries[1]; 
      cell.Label = response.NewTicketCount.ToString(); 
      cell = (PlainTitleDisclosureCellItem)this.SDSummaries[2]; 
      cell.Label = response.UnassignedTicketCount.ToString(); 
     }); 
    } 

似乎有點一個黑客,但我米不確定。