Mvvm DesignInstance工作不正常需要帮助

Mvvm DesignInstance工作不正常需要帮助,mvvm,design-time-data,Mvvm,Design Time Data,我用的是一个 public class ObservableDictionary : IObservableMap<string, object> ..用于实例化类,以便为我的主页创建设计时数据 <Grid Background="{ThemeResource AppBackground}" d:DataContext="{Binding DefaultDataModel[0]}" DataContext="{Binding DefaultDataModel[0]}"

我用的是一个

public class ObservableDictionary : IObservableMap<string, object>
..用于实例化类,以便为我的主页创建设计时数据

<Grid Background="{ThemeResource AppBackground}"
  d:DataContext="{Binding DefaultDataModel[0]}"
  DataContext="{Binding DefaultDataModel[0]}">
<Hub Margin="0,40,0,0">
    <HubSection MinWidth="{Binding Width, Source={StaticResource WritingsSize}}"
                Header="WRITINGS"
                x:Name="WritingsHubSection">
        <DataTemplate>
            <ListView
                x:Name="itemGridView"
                ItemsSource="{Binding HubSections}"
                Margin="-9,-14,0,0"
                SelectionMode="Single">
                <ListView.ItemTemplate>
                    <DataTemplate >
                        <Grid Height="250" Width="310" Margin="5,10,5,10" Background="Transparent">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Height="150">
                                <Image Source="{Binding ImagePath}" />
                            </Border>
                            <StackPanel Grid.Row="1" Margin="0,10,0,0">
                                <TextBlock Text="{Binding Name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap"/>
                                <TextBlock Text="{Binding Header}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>

                </ListView.ItemTemplate>

            </ListView>

        </DataTemplate>

    </HubSection>
</Hub>
补充: 在阅读了卢克·布卢比特的文章后,我做了一些改变,因为我们有同样的问题。我添加了绑定和类型属性,但得到了相同的结果

为了更清楚地说明这个问题,我的问题是ObservableDictionary:IObservableMap类,为什么我不能绑定到设计时数据的映射

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;
    }
}
}
它显示数据以及我运行应用程序时的数据,我没有收到任何警告或错误。任何帮助都将不胜感激


谢谢

这并不是问题的答案,但在做了更多的研究之后,我终于意识到我太过技术化,同时也没有完全理解设计时数据的使用

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;
    }
}
}
这是我应该真正寻找的显示结果,而不是我如何获得数据

为浪费的帖子道歉

<Grid Background="{ThemeResource AppBackground}"
  d:DataContext="{Binding DefaultDataModel[0]}"
  DataContext="{Binding DefaultDataModel[0]}">
<Hub Margin="0,40,0,0">
    <HubSection MinWidth="{Binding Width, Source={StaticResource WritingsSize}}"
                Header="WRITINGS"
                x:Name="WritingsHubSection">
        <DataTemplate>
            <ListView
                x:Name="itemGridView"
                ItemsSource="{Binding HubSections}"
                Margin="-9,-14,0,0"
                SelectionMode="Single">
                <ListView.ItemTemplate>
                    <DataTemplate >
                        <Grid Height="250" Width="310" Margin="5,10,5,10" Background="Transparent">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Height="150">
                                <Image Source="{Binding ImagePath}" />
                            </Border>
                            <StackPanel Grid.Row="1" Margin="0,10,0,0">
                                <TextBlock Text="{Binding Name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap"/>
                                <TextBlock Text="{Binding Header}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60" />
                            </StackPanel>
                        </Grid>
                    </DataTemplate>

                </ListView.ItemTemplate>

            </ListView>

        </DataTemplate>

    </HubSection>
</Hub>
d:DataContext="{Binding DefaultDataModel[0]}"
DataContext="{Binding DefaultDataModel[0]}"
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;
    }
}
}
var w = this.DefaultDataModel[0];
ObservableCollection<Models.HubSection> hubSections = w["HubSections"] as ObservableCollection<Models.HubSection>;
d:DataContext="{Binding DefaultDataModel[0], Source={d:DesignInstance Type=designTimeData:MainPageViewModel, IsDesignTimeCreatable=True}}"
...
..
ListView x:Name="itemGridView"  ItemsSource="{Binding HubSections}"