我是单身类的,
public sealed class MainWindow
{
public Form MainWindowContainer { get; set; }
private static readonly Lazy<MainWindow> _mainWindow = new Lazy<MainWindow>(() => new MainWindow());
// Error on this line
public static MainWindow Instance => _mainWindow.Value;
private MainWindow()
{
MainWindowContainer = Host.Local.FindSingle<Form>(GenericProperties.MainWindow);
}
}
在编译时,我收到错误&#34;无效的令牌&#39 ;;&#39;在类struct或接口成员声明&#34;。请帮忙。
答案 0 :(得分:3)
你不能将属性getter定义为lambda,只需更改为getter属性
public static MainWindow Instance
{
get { return _mainWindow.Value; }
}
或更改为Lambda函数
public static Func<MainWindow> Instance => _mainValue.Value;
取决于您想要如何访问它
var win = MainWindow.Instance;
// or
var win = MainWindow.Instance();