If I write this:
private string boundText;
public String BoundText
{
get { return boundText; }
set
{
boundText = value;
OnPropretyChanged("BoundText");
}
}
I get this warning:
Warning CS8618 Non-nullable field 'boundText' must contain a non-null value when exiting the constructor. Consider declaring the field as nullable.
And if I write this:
private string boundText;
public String BoundText
{
get { return boundText; }
set
{
boundText = value;
OnPropretyChanged("BoundText");
}
} = string.Empty;
I get this error :
Error CS8050 Only automatically implemented properties can have initializers.
If I write this:
private string boundText;
public String BoundText
{
get { return boundText; }
set
{
boundText = value;
OnPropretyChanged("BoundText");
}
}
I get this warning:
Warning CS8618 Non-nullable field 'boundText' must contain a non-null value when exiting the constructor. Consider declaring the field as nullable.
And if I write this:
private string boundText;
public String BoundText
{
get { return boundText; }
set
{
boundText = value;
OnPropretyChanged("BoundText");
}
} = string.Empty;
I get this error :
Share Improve this question edited Mar 13 at 16:11 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 13 at 14:41 guilherme franceschiguilherme franceschi 454 bronze badges 9 | Show 4 more commentsError CS8050 Only automatically implemented properties can have initializers.
1 Answer
Reset to default 1You need to assign boundText
(not BoundText
) a default value.
Should be something like that
private string boundText = string.Empty;
public string BoundText
{
get => boundText;
set
{
boundText = value;
OnPropertyChanged(nameof(BoundText));
}
}
It doesn't really address the issue but i also advice you to use nameof
instead of hardcoding string value
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744695428a4588462.html
boundText
? – MakePeaceGreatAgain Commented Mar 13 at 14:43private string boundText = string.Empty;
- here is where you assign the default value. – Clemens Commented Mar 13 at 14:45