简体   繁体   English

有没有办法为 VS2015 实现类似的 Raise 属性更改?

[英]Is there a way to implement similar Raise Property Changed for VS2015?

Seems I have syntax errors in VS2015 for the code below.似乎我在 VS2015 中有以下代码的语法错误。 The error sounds like:错误听起来像:

A value of type '?' '?' 类型的值cannot be used as a default parameter because there are no standard conversions to type 'T'不能用作默认参数,因为没有到类型“T”的标准转换

Is there a way to implement similar "Raise property changed" in VS2015?有没有办法在 VS2015 中实现类似的“Raise property changed”?

 public class ViewModel : ViewModelBase
    {

        // Property        
        public const string SelectedItemPropertyName = "SelectedItem";

        private int? _selectedItem;       
        public int? SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                Set(SelectedItemPropertyName, ref _selectedItem, value);
            }
        }

        // Method
        private void Load()
        {
            int number = SelectedItem ?? -1;

            // do your work
        }


       // Raise property changed
        public override void RaisePropertyChanged<T>([CallerMemberName] string propertyName = null, T oldValue = default, T newValue = default, bool broadcast = false)
        {
            base.RaisePropertyChanged(propertyName, oldValue, newValue, broadcast);
            if (propertyName == nameof(SelectedItem))
                Load();
        }
    }


It looks like you are using a C# version under 7.1.看起来您使用的是 7.1 以下的 C# 版本。 In that case, you will need to use default(T) , rather than simply default .在这种情况下,您将需要使用default(T) ,而不仅仅是default Here is a link to the docs on default value expressions. 是有关默认值表达式的文档的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM