Skip to content

RelayCommand<T> throws NullReferenceException when T is a ValueType and CanExecute is used #3619

@sonnemaf

Description

@sonnemaf

Describe the bug

If you open a View (Page or Window) and you have DataBound the Command of a Button to a RelayCommand<T> where T is a ValueType and the command has a CanExecute action you get a NullReferenceException. Tested it in a UWP and in a WPF app. Both have this problem.

Without the CanExecute it works ok. If T is a reference type it also works ok.

Steps to Reproduce

Take this xaml

<Page
    x:Class="App19.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App19"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid RowSpacing="8" Padding="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Button Content="Test"
                Command="{x:Bind TestCommand, Mode=OneTime}"
                CommandParameter="{x:Bind listViewDemo.SelectedIndex, Mode=OneWay}"/>
        <ListView x:Name="listViewDemo">
            <ListViewItem Content="A" />
            <ListViewItem Content="B" />
            <ListViewItem Content="C" />
        </ListView>
    </Grid>
</Page>

And this codebehind

using Microsoft.Toolkit.Mvvm.Input;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App19 {
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page {

        private RelayCommand<int> TestCommand { get; }

        public MainPage() {
            this.InitializeComponent();
            TestCommand = new RelayCommand<int>(OnTest, index => index > 0);
        }

        private void OnTest(int index) {
            _ = new MessageDialog($"Test: {index}").ShowAsync();
        }
    }
}

Start the app using F5. You get the Exception. You don't get the Page/Window it happens from the constructor of the page/window.

image

This also happens in WPF apps, not only UWP.

Expected behavior

No Exception, just a working app.

Environment

NuGet Package(s): 
Microsoft.Toolkit.Mvvm
Package Version(s): 
7.0.0.0-preview4

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [X] May 2020 Update (19041)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] May 2020 Update (19041)
- [ ] Insider Build (xxxxx)

Device form factor:
- [X] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [ ] 2017 (version: )
- [X] 2019 (version: ) 
- [X] 2019 Preview (version: )

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

bug 🐛An unexpected issue that highlights incorrect behaviormvvm-toolkit 🧰Issues/PRs for the Microsoft.Toolkit.Mvvm package

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions