我正在Visual Studio中使用C#制作逆向游戏。我有一个带有MainViewModel.cs类的ViewModel命名空间,在其中创建了一个界面Screen。当我想在同一类名称空间中的WelcomeClassModel.cs之类的其他类中使用此接口时,它无法识别Screen。下面是我的一些代码示例。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Model.Reversi;
using View;
namespace ViewModel
{
class MainViewModel: INotifyPropertyChanged
{
private Screen screen;
public event Action ExitApp;
public event PropertyChangedEventHandler PropertyChanged;
public Screen Screen
{
get { return this.screen; }
set { this.screen = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Screen))); }
}
internal void Exit()
{
ExitApp?.Invoke();
}
public MainViewModel()
{
this.Screen = new WelcomeViewModel(this);
}
}
public abstract class Screen
{
protected readonly MainViewModel mainModel;
protected Screen(MainViewModel mainModel)
{
this.mainModel = mainModel;
}
protected void Switch(Screen screen)
{
this.mainModel.Screen = screen;
}
}
public class ScreenWelcome : Screen
{
public ScreenWelcome(MainViewModel navigator) : base(navigator)
{
GoToGame = new EasyCommand(() => Switch(new ScreenGame(navigator)));
}
public ICommand GoToGame { get; }
}
public class ScreenGame : Screen
{
public ScreenGame(MainViewModel navigator) : base(navigator)
{
GoToWelcome = new EasyCommand(() => Switch(new ScreenWelcome(navigator)));
}
public ICommand GoToWelcome { get; }
}
}
WelcomeViewModel
类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ViewModel.Screen;
using Model.Reversi;
namespace ViewModel
{
class WelcomeViewModel: Screen
{
private int width;
private int height;
public int Width
{
get { return width; }
set {
if (ReversiBoard.IsValidWidth(value))
this.width = value;
}
}
public int Height
{
get { return height; }
set
{
if (ReversiBoard.IsValidHeight(value))
this.height = value;
}
}
public WelcomeViewModel(MainViewModel mainModel) : base(mainModel)
{
this.Width = 9;
this.Height = 9;
this.Options = new PlayerOptionsViewModel();
Start = new EasyCommand(() =>
{
Switch(new GameViewModel(this.mainModel, Options, Width, Height));
});
}
public PlayerOptionsViewModel Options { get; set; }
public ICommand Start { get; }
}
}
the:屏幕保持白色,这意味着它无法识别Screen类。这怎么可能?
有人解决吗? [在此处输入图片描述] [1]
答案 0 :(得分:0)
您最有可能得到
错误CS0051可访问性不一致:参数类型'MainViewModel'的访问权限比方法'ScreenWelcome.ScreenWelcome(MainViewModel)'少
在公共类ScreenWelcome
中,您具有构造函数public ScreenWelcome(MainViewModel navigator)
。该构造函数在其公共接口中公开MainViewModel
,因此类MainViewModel
也必须是公共的。公开
public class MainViewModel : INotifyPropertyChanged
{
...
}