Cannot add instance of type '%0' to a collection of type '%1' error in windows phone 8.1 DatePicker

谁说胖子不能爱 提交于 2019-12-23 21:42:06

问题


Hey I am developing an app in windows phone 8.1 using the MVVM pattern. I want to get the date from the DatePicker on the DateChanged event in the viewModel. After running the program I am getting this error:

A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App1.exe WinRT information: Cannot add instance of type '%0' to a collection of type '%1'. [Line: 117 Position: 97] An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in App1.exe but was not handled in user code WinRT information: Cannot add instance of type '%0' to a collection of type '%1'. [Line: 117 Position: 97] Additional information: The text associated with this error code could not be found.

My view is:

<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>

And the viewModel:

    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);
    }

Can anyone help me in solving this error?


回答1:


My answer might be a bit late but this error (still) occurs (in WinRT 8.1 still with placeholders in the message!) if the event you try to add either does not exist or is not supported by the EventTriggerBehaviour. Supported events are: Tapped, PointerPressed, Loaded, DataContextChanged, Click, Checked, Unchecked, SelectionChanged, TextChanged, Toggled, NavigationCompleted

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



来源:https://stackoverflow.com/questions/28474634/cannot-add-instance-of-type-0-to-a-collection-of-type-1-error-in-windows-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!