【问题标题】:Cannot add instance of type '%0' to a collection of type '%1' error in windows phone 8.1 DatePickerwindows phone 8.1 DatePicker 中无法将类型“%0”的实例添加到类型“%1”的集合中
【发布时间】:2015-02-12 10:09:42
【问题描述】:

嘿,我正在使用 MVVM 模式在 windows phone 8.1 中开发应用程序。我想从 viewModel 中的 DateChanged 事件 上的 DatePicker 获取日期。 运行程序后出现此错误:

App1.exe WinRT 中出现了“Windows.UI.Xaml.Markup.XamlParseException”类型的第一次机会异常 信息: 无法将类型“%0”的实例添加到类型的集合 “%1”。 [行:117 位置:97] 类型异常 “Windows.UI.Xaml.Markup.XamlParseException”发生在 App1.exe 但 未在用户代码 WinRT 信息中处理:无法添加实例 类型 '%0' 到类型 '%1' 的集合。 [行:117 位置:97] 附加信息:与此错误代码相关的文本可能 找不到。

我的看法是:

<DatePicker Grid.Row="1" Grid.Column="1"
                VerticalContentAlignment="Center"
                HorizontalContentAlignment="Center"
                HorizontalAlignment="Left"
                VerticalAlignment="Center" Margin="26,-0.333,0,0.5"
                Date="{Binding Dates, Mode=TwoWay}">
        <i:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="DateChanged">
                <core:InvokeCommandAction Command="{Binding InitializeExpenseListCommand}"/>
            </core:EventTriggerBehavior>
        </i:Interaction.Behaviors>
    </DatePicker>

还有视图模型:

    public MainViewModel()
    {
        _dates = new DateTimeOffset(DateTime.Now);
    }

    private DateTimeOffset _dates;
    public DateTimeOffset Dates
    {
        get { return _dates; }
        set
        {
            _dates = value;
            RaisePropertyChanged();
        }
    }
    public ICommand InitializeExpenseListCommand
    {
        get { return new RelayCommand(InitializeExpenseList()); }
    }

    public Action InitializeExpenseList()
    {
        return () => Debug.WriteLine(_dates);
    }

谁能帮我解决这个错误?

【问题讨论】:

  • 不。那里有些不对劲。重新安装视觉工作室。不要担心试图找到解决此问题的方法 - 没有。
  • 为什么?这是唯一的解决方案吗?
  • "找不到与此错误代码相关的文本" 这是个坏消息。严重的坏消息。
  • 我认为错误是因为事件名称中的DateChanged。我已将其更改为Tapped,然后它工作正常。你能说出 DateChanged 事件有什么问题吗?或任何与此相关的活动。
  • 我有一个非常相似的问题绑定到 ColorHexagonPicker ColorChanged 事件。这可能与与这些变化相关的事件有关吗? @您能进一步解释为什么该消息可能是坏消息吗?正如 Utsav 所说,其他绑定正在工作(我有同样的错误)。

标签: c# xaml mvvm datepicker windows-phone-8.1


【解决方案1】:

如果您尝试添加的事件不存在或不受 EventTriggerBehaviour 支持,我的回答可能有点晚,但此错误(仍然)会发生(在 WinRT 8.1 中,消息中仍然带有占位符!)。支持的事件有:TappedPointerPressedLoadedDataContextChangedClickCheckedUncheckedSelectionChangedTextChangedToggledp、@987654332 >

https://msdn.microsoft.com/en-us/library/windows/apps/dn469361.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-11
    • 2015-04-10
    • 2015-12-18
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2013-11-27
    • 1970-01-01
    相关资源
    最近更新 更多