2

我无法让我的 ListView 绑定到 ObservableDictionary 类型的集合,该集合直接取自 Windows 商店应用程序的 Common 文件夹中的 LayoutAwarePage 中使用的 ObservableDictionary。如果我将 ObservableDictionary 换成 Dictionary,那么我的项目在列表中正确可见,但显然不会根据需要进行更新。

尝试绑定时,我在“输出”面板中得到以下信息

Error: Cannot get 'Screens' value (type 'Object') from type 'Test.DataModel.Project, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='Project.Screens' DataItem='ObservableDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.ListView' (Name='itemListView'); target property is 'ItemsSource' (type 'Object').

我的项目类看起来像这样

public class Project : Common.BindableBase
{
    private ObservableDictionary<Guid, Screen> _Screens;
    public ObservableDictionary<Guid, Screen> Screens
    {
        get { return _Screens; }
        set { this.SetProperty(ref _Screens, value); }
    }

    public Project()
    {
        Screens = new ObservableDictionary<Guid, Screen>();
        //Test screen
        Screens.Add(...);
    }
}

在 projectview 屏幕的代码隐藏文件中

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
    project = DataModel.ExampleProject.Project;
    this.DefaultViewModel["Project"] = project;
}

在 xaml 绑定中我有

<!-- Vertical scrolling item list -->
        <ListView
            x:Name="itemListView"
            AutomationProperties.AutomationId="ItemsListView"
            AutomationProperties.Name="Items"
            TabIndex="1"
            Margin="10,130,0,0"
            Padding="50,0,0,60"
            IsSwipeEnabled="False"
            SelectionChanged="ItemListView_SelectionChanged"
            <!-- Bind the list of screens in the project -->
            ItemsSource="{Binding Project.Screens}"
            ItemTemplate="{StaticResource ScreenListItemTemplate}" Grid.RowSpan="2">

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>

        </ListView>
<!-- Item template which works if I use just the non-observable Dictionary<K,V> implementation -->
 <DataTemplate x:Key="ScreenListItemTemplate">
        <Grid Height="110" Margin="6" Background="#FF06AAFF" HorizontalAlignment="Stretch" DataContext="{Binding Value}">

            <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="10,-10,0,0" TextWrapping="NoWrap" FontSize="30" Foreground="White"/>
        </Grid>
    </DataTemplate>

以及取自 LayoutAwarePage.cs 的类

 /// <summary>
    /// Implementation of IObservableMap that supports reentrancy for use as a default view
    /// model.
    /// </summary>
    public class ObservableDictionary<K, V> : IObservableMap<K, V>
    {
        private class ObservableDictionaryChangedEventArgs : IMapChangedEventArgs<K>
        {
            public ObservableDictionaryChangedEventArgs(CollectionChange change, K key)
            {
                this.CollectionChange = change;
                this.Key = key;
            }

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

        private Dictionary<K, V> _dictionary = new Dictionary<K, V>();
        public event MapChangedEventHandler<K, V> MapChanged;

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

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

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

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

        public bool Remove(KeyValuePair<K, V> item)
        {
            V 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 V this[K 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<K> Keys
        {
            get { return this._dictionary.Keys; }
        }

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

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

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

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

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

        public bool IsReadOnly
        {
            get { return false; }
        }

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

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

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

0 回答 0