Private setter throwing error only on release buil

2019-02-22 09:20发布

I have a map-tile setting I'm updating through a menu-button. I've got an odd situation where I was only hitting an error on release builds. Code is as follows:

View-Model

private KnownTileSource _selectedTile;
public KnownTileSource SelectedTile
{
    get { return _selectedTile; }
    private set
    {
        _selectedTile = value;
        ...
        OnPropertyChanged("SelectedTile");
    }
}

View

<Window ...
 xmlns:predefined="clr-namespace:BruTile.Predefined;assembly=BruTile">
...
    <MenuItem Header="_Bing Aerial" Command="{Binding ChangeTileCommand}" CommandParameter="{x:Static predefined:KnownTileSource.BingAerial}" IsChecked="{Binding Path=SelectedTile, Mode=TwoWay, Converter={local:EnumToBooleanConverter}, ConverterParameter=BingAerial}"/>
...
</Window>

This was all working fine in my developer environment but when I generated a release build I was getting the following:

Error

System.InvalidOperationException: A TwoWay or OneWay ToSource binding cannot work on the read-only property 'SelectedTile'...

Simple solution, change private set to set in the above SelectedTile property.

So how come this was not throwing the error during debug and only during release? I can't see how this was ever working during debug mode.

标签: c# wpf mvvm
1条回答
劳资没心,怎么记你
2楼-- · 2019-02-22 09:51

This is a known bug that has been fixed: https://connect.microsoft.com/VisualStudio/feedback/details/773682/wpf-property-with-private-setter-is-updated-by-a-twoway-binding

So you may get this behaviour if your app targets .NET Framework 4.0 but .NET Framework 4.5+ is installed on your development machine.

You should remove the private keyword from the setter to fix the issue.

查看更多
登录 后发表回答