我有一个地图图块设置我正在通过菜单按钮更新.我有一个奇怪的情况,我只是在发布版本上遇到错误.代码如下:
视图模型
private KnownTileSource _selectedTile;
public KnownTileSource SelectedTile
{
get { return _selectedTile; }
private set
{
_selectedTile = value;
...
OnPropertyChanged("SelectedTile");
}
}
Run Code Online (Sandbox Code Playgroud)
视图
<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>
Run Code Online (Sandbox Code Playgroud)
这在我的开发人员环境中运行良好,但是当我生成发布版本时,我得到以下内容:
错误
System.InvalidOperationException: A TwoWay or OneWay ToSource binding cannot work on the read-only property 'SelectedTile'...
简单的解决方案,改变private set对set上述SelectedTile财产.
那么为什么这不是在调试期间抛出错误而只是在发布期间?在调试模式下,我无法看到它是如何工作的.
这是一个已修复的已知错误:https://connect.microsoft.com/VisualStudio/feedback/details/773682/wpf-property-with-private-setter-is-updated-by-a-twoway-binding
因此,如果您的应用程序面向.NET Framework 4.0但您的开发计算机上安装了.NET Framework 4.5+,则可能会出现此行为.
您应该private从setter中删除关键字以解决问题.