具有不同参数的MVVM构造函数调用ViewModel

时间:2018-11-18 14:40:58

标签: c# wpf mvvm

我正坐在MVVM代码上,并且它们彼此独立工作,但是我将其连接在一起。

问题是我不能调用GameViewModel,这是由于构造函数中的参数不同。

这似乎是一个简单的问题,但我找不到解决方法。

赌场玩家(姓名和硬币必须提供给二十一点玩家)

字符串名称;     整数芯片;

public Player(string n, int c)
{
    name = n;
    chips = c;
}

BlackJack Player

  public BlackJackPlayer(string name, int money, BitmapImage backSide, int currentImage)
    {
        Images = new List<Image>();
        Name = name;
        Money = money;
        ImageBack = backSide;
        CurrentImage = currentImage;
    }

GameViewModel.cs( 认为问题出在这个班上,因为我想将赌场玩家的姓名和硬币放到二十一点游戏中

public GameViewModel(IView view, string name)
    {
        View = view;
        Random = new Random();
        Commands = new Commands(this);

        BlackJackPlayer = new BlackJackPlayer(name, 1000, ImagesHelper.CreateImage("player"), 2);
        BlackJackPlayer.CreateSplitDeck();
        view.AddSplitDeckCards(BlackJackPlayer);
        Computer = new BlackJackPlayer("Computer", 1000, ImagesHelper.CreateImage("computer"), 2);
        view.DisplayMoney(BlackJackPlayer, Computer);
        view.DisplayName(name);

        CardImages = ImagesHelper.GetBlackJackCards();
        view.AddCards(BlackJackPlayer, Computer);
        BlackJackPlayer.ShowBackside();
        Computer.ShowBackside();
        BetAmount = "100";
    }

** BlackJackScreen.Xaml.cs(这里我尝试访问GameViewModel,但是当然不可能,因为有不同的参数-> p是播放器的类型,我必须是字符串)-> * *

  public BlackjackScreen(Player p)
    {
        InitializeComponent();
        _player = p;
        defaultButtons = new[] { Hit, Stand, Split };
        splitButtons = new[] { HitLeft, HitRight, StandLeft, StandRight };
        DataContext = new GameViewModel(this, p);
    }

MainWindow.xaml.cs( 这是我调用所使用的不同窗口的方式,除了blackJackScreen之外,其他方法都可以正常工作

    public void SetContent(Player p, int contentId)
    {
        switch (contentId)
        {
            case 0: ContentHolder.Content = new GamePicker(p); break;
            case 1: ContentHolder.Content = new BlackjackScreen(p); break;
            //case 2: ContentHolder.Content = new SlotsScreen(p); break;
            //case 3: ContentHolder.Content = new VideoPokerScreen(p); break;
            //case 4: ContentHolder.Content = new RouletteScreen(p); break;
        }
    }

我衷心希望您能给出一个代码示例,说明如何解决此问题。 预先感谢

0 个答案:

没有答案