【问题标题】:Move controls on View from ViewModel从 ViewModel 移动视图上的控件
【发布时间】:2019-01-03 20:23:20
【问题描述】:

我正在尝试使用 WPF、Prism 和 MVVM 制作垄断游戏。 在我的视图中,我有一个分为几行和几列的网格。每个单元格都是一张单独的游戏卡,例如一个城市。

我还有一个内容控件,可以将我的玩家显示为筹码。

任务是移动我的玩家,相对于网格中的单元格。 我需要从我的 ViewModel 中执行此操作。(我希望我的 ViewModel 以某种方式告诉我的 View 在哪里找到播放器)。请帮我解决这个问题。

我的观点:

            <!--bottom row-->
            <ContentControl Grid.Row="10" Grid.Column="10" Content="{Binding Cards[0]}" ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            <ContentControl Grid.Row="10" Grid.Column="9" Content="{Binding Cards[1]}" ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            ...

            <!--left column-->
            <ContentControl Grid.Row="9" Grid.Column="0" Content="{Binding Cards[11]}"  ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            <ContentControl Grid.Row="8" Grid.Column="0" Content="{Binding Cards[12]}"  ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            ...

            <!--top row-->
            <ContentControl Grid.Row="0" Grid.Column="0" Content="{Binding Cards[20]}"  ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            ...

            <!--right column-->
            ...

            <!--player chips-->
            <ContentControl Grid.Row="10" Grid.Column="10" Content="{Binding Players[0]}"  ContentTemplate="{StaticResource PlayerTemplate1}"/>
            ...

我的期望: 1)模型掷骰子(实际上我已经这样做了) 2)ViewModel 得到这个结果并将其转换为网格上的位置(有一些想法) 3)View 必须以某种方式获得这个位置并移动我的播放器。

【问题讨论】:

  • 您可以尝试将播放器的内容控件放置在每个单元格中,然后使用可见性转换器仅在当前位置显示它们。
  • My answer to this question 应该为您提供足够的入门知识。
  • 谢谢@MarkFeldman!你能帮助另一个人吗?

标签: wpf mvvm prism


【解决方案1】:

感谢 MarkFeldman!您的方法帮助了我。但是我现在有另一个问题))

此刻,我的玩家可以在游戏板上四处移动。这很棒!但是,我想一张一张地模拟卡片的通过,而不是立即模拟到最后一张卡片。首先,我尝试迈出一步,thread.sleep(100),然后再次迈出一步,直到在正确的位置。但这导致程序完全冻结,直到芯片进入最后一张卡片。

如果再给我一个提示,我将不胜感激。

【讨论】:

  • 考虑为新问题发布新问题,而不是将其作为对原始问题的答案发布。
【解决方案2】:
    internal void MakeStep()
    {
        this.CardIndex++;
    }

    internal void MakeStep(int count)
    {
        Task.Factory.StartNew(() => this.StepByStep(count));

    }

    private void StepByStep(int count)
    {
        for (int i = count; i > 0; i--)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.MakeStep();
                Debug.Print(this.CardIndex.ToString());
            }), DispatcherPriority.Background);
            Thread.Sleep(100);
        }
    }

这是我一张一张模拟卡片通过的解决方案。问题出在线程上!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2011-07-03
    • 1970-01-01
    • 2018-12-27
    相关资源
    最近更新 更多