首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >观察-观察性宣言

观察-观察性宣言
EN

Stack Overflow用户
提问于 2016-03-24 11:44:41
回答 2查看 286关注 0票数 0

不知何故,这在运行时不起作用(但它正在编译):

代码语言:javascript
复制
    public static readonly DependencyProperty SelectedVideoFileNamesProperty =
    DependencyProperty.Register("SelectedVideoFileNames", typeof(ObservableCollection<ObservableCollection<string>>), typeof(CMiX_UI), 
        new PropertyMetadata(new[]{new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>(),
                                   new ObservableCollection<string>()}));
    [Bindable(true)]
    public ObservableCollection<ObservableCollection<string>> SelectedVideoFileNames
    {
        get { return (ObservableCollection<ObservableCollection<string>>)this.GetValue(SelectedVideoFileNamesProperty); }
        set { this.SetValue(SelectedVideoFileNamesProperty, value); }
    }

为什么?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-24 12:12:15

它不应该运行,因为您将字符串ObservableCollection的ObservableCollection<string>[]数组指定为ObservableCollection<ObservableCollection<string>>的默认值。如果您像这样编写代码:

代码语言:javascript
复制
        ObservableCollection<ObservableCollection<string>> o = new[]{new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()};

它将给您一个设计时错误,即:

不能隐式地将类型'System.Collections.ObjectModel.ObservableCollection[]‘转换为'System.Collections.ObjectModel.ObservableCollection>’

相反,你可以:

代码语言:javascript
复制
        ObservableCollection<ObservableCollection<string>> o = new ObservableCollection<ObservableCollection<string>> {new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()};
票数 1
EN

Stack Overflow用户

发布于 2016-03-24 12:11:31

类型定义为

代码语言:javascript
复制
ObservableCollection<ObservableCollection<string>>

但是,您将默认设置为:

代码语言:javascript
复制
new[]{new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()}))

这是一组可观察的集合。你需要:

代码语言:javascript
复制
new ObservableCollection<ObservableCollection<String()>> {new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>(),
                               new ObservableCollection<string>()}))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36199394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档