Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#需要WPF帮助:如何通过WPF显示对象中的值_C#_Wpf - Fatal编程技术网

c#需要WPF帮助:如何通过WPF显示对象中的值

c#需要WPF帮助:如何通过WPF显示对象中的值,c#,wpf,C#,Wpf,[已编辑2019-02-23;我希望编辑该问题是正确的] 我完全是WPF的新手,而且很努力。 我看过很多教程等,但没有找到答案(以我理解的方式) 我正在制作一个Yatzy游戏,Yatzy类游戏本身几乎完成了。 现在我想向yatzy类添加一个使用WPF的GUI 感谢雷金纳德·布鲁对我问题的回答。 我正在制作一个视图模型 Mark Whitall做了一个很好的例子,但我无法将其移植到我的需要: 但它也困扰着我。我希望提供的代码足够了,如果没有-问:-) 它看起来像我的视图模型,与我的模型有“联系”。

[已编辑2019-02-23;我希望编辑该问题是正确的]

我完全是WPF的新手,而且很努力。 我看过很多教程等,但没有找到答案(以我理解的方式)

我正在制作一个Yatzy游戏,Yatzy类游戏本身几乎完成了。 现在我想向yatzy类添加一个使用WPF的GUI

感谢雷金纳德·布鲁对我问题的回答。 我正在制作一个视图模型

Mark Whitall做了一个很好的例子,但我无法将其移植到我的需要:

但它也困扰着我。我希望提供的代码足够了,如果没有-问:-)

它看起来像我的视图模型,与我的模型有“联系”。太好了 但是关于我的视图连接到我的viewmodel,似乎有点不对劲

首先,我想说: 1) 何时GameStatus和GameName字段应具有我的模型中的默认值。 在制作我的viewmodel之前,它工作得很好——但我把它搞砸了。 我在maincontrol.cs(在viewmodel中)中声明我的thisYatzy对象。 我应该在app.xaml级别声明它吗? 2) 当我单击“按钮”ShiftState时,我会将我的viewmodel调用为shift状态,并更新视图。但是从视图来看,我不能称之为viewmodel

我希望我已经解释好了,希望你们中有人能帮忙:-)

查看:

app.xalm

<Application x:Class="WPFYatzy.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFYatzy"
             StartupUri="Views/MainWindow.xaml">
    <Application.Resources>

    </Application.Resources>
</Application>
DelegateCommand.cs

using System;
using System.Windows.Input;

namespace WPFYatzy.ViewModel
{
    public class DelegateCommand : ICommand
    {
        private readonly Action _action;

        public DelegateCommand(Action action)
        {
            _action = action;
        }

        public void Execute(object parameter)
        {
            _action();
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

#pragma warning disable 67
        public event EventHandler CanExecuteChanged { add { } remove { } }
#pragma warning restore 67
    }
}
MainControl.cs

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using YatzyGameCL;

namespace WPFYatzy.ViewModel
{
    class MainControl : ObservableObject
    {
        private YatzyGame thisYatzy = new YatzyGame();

        private string _strGameName;
        private string _strGameStatus;

        public string GameName
        {
            get { return _strGameName; }
            set
            {
                _strGameName = value;
                RaisePropertyChangedEvent("GameName");
            }

        }

        public string GameStatus
        {
            get { return _strGameStatus; }
            set {
                _strGameStatus = value;
                RaisePropertyChangedEvent("GameStatus");
            }
        }

        public void ShiftState()
        {
            thisYatzy.GameStatus.NextAround();

            GameStatus = thisYatzy.GameStatus.ToString();

        }
    }
}
型号 YatzyGameCL.DLL

用于使框架“连接在一起”的方法

  • 这是游戏状态
  • thisYatzy.GameName

    • 这很难简明扼要地表达出来。您应该去阅读MVVM(模型-视图-视图-模型)模式。虽然不一定要将MVVM与WPF一起使用,但对于您正在做的事情,它确实是您需要的地方

      具体而言:

      您已经创建了一个模型。那是你的游戏。它知道Yatzy是怎么工作的,有所有的规则。这就是“游戏”

      你可以看到。这是您的XAML文件(以及背后的代码)。它知道如何画一个屏幕,如果有人告诉它,它知道如何进行更新

      但是。。。您没有ViewModel。这是两个世界之间的桥梁。它知道如何通知视图进行更新。您的模型不(也不应该)知道如何做到这一点,因此视图(屏幕)永远不会改变

      对于您的问题,非常具体的答案是您需要发送属性更改通知,以便视图知道属性(例如GameStatus)已更改。在您的模型中,您可能会尝试这样做。不要那样做


      祝你好运

      嗨,雷金纳德,所以我需要建立一个视图模型(并找出如何做)。我会用谷歌搜索:-)谢谢,我需要的正是一个lotHi谷歌视图模型(希望如此)。我找到了Mark Withall的一个简单示例:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Documents;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Imaging;
      using System.Windows.Navigation;
      using System.Windows.Shapes;
      using YatzyGameCL;
      
      namespace WPFYatzy
      {
          /// <summary>
          /// Interaction logic for MainWindow.xaml
          /// </summary>
          public partial class MainWindow : Window
          {
              //YatzyGame thisYatzy = new YatzyGame();
      
              public MainWindow()
              {
                  //InitializeComponent();
              }
      
              private void OnInit(object sender, EventArgs e)
              {
      
              }
      
              private void Button_Click(object sender, RoutedEventArgs e)
              {
      
              }
          }
      
      }
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Documents;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Imaging;
      using System.Windows.Navigation;
      using System.Windows.Shapes;
      
      namespace WPFYatzy.Views
      {
          /// <summary>
          /// Interaction logic for Page1.xaml
          /// </summary>
          public partial class Page1 : Page
          {
              public Page1()
              {
                  InitializeComponent();
              }
      
              private void Button_Click(object sender, RoutedEventArgs e)
              {
                       }
          }
      }
      
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Documents;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Imaging;
      using System.Windows.Navigation;
      using System.Windows.Shapes;
      
      namespace WPFYatzy.Views
      {
          /// <summary>
          /// Interaction logic for Page1.xaml
          /// </summary>
          public partial class Page1 : Page
          {
              public Page1()
              {
                  InitializeComponent();
              }
      
              private void Button_Click(object sender, RoutedEventArgs e)
              {
                       }
          }
      }
      
      using System.ComponentModel;
      
      namespace WPFYatzy.ViewModel
      {
          public abstract class ObservableObject : INotifyPropertyChanged
          {
              public event PropertyChangedEventHandler PropertyChanged;
      
              protected void RaisePropertyChangedEvent(string propertyName)
              {
                  var handler = PropertyChanged;
                  if (handler != null)
                      handler(this, new PropertyChangedEventArgs(propertyName));
              }
          }
      }
      
      using System;
      using System.Windows.Input;
      
      namespace WPFYatzy.ViewModel
      {
          public class DelegateCommand : ICommand
          {
              private readonly Action _action;
      
              public DelegateCommand(Action action)
              {
                  _action = action;
              }
      
              public void Execute(object parameter)
              {
                  _action();
              }
      
              public bool CanExecute(object parameter)
              {
                  return true;
              }
      
      #pragma warning disable 67
              public event EventHandler CanExecuteChanged { add { } remove { } }
      #pragma warning restore 67
          }
      }
      
      using System.Collections.Generic;
      using System.Collections.ObjectModel;
      using System.Windows.Input;
      using YatzyGameCL;
      
      namespace WPFYatzy.ViewModel
      {
          class MainControl : ObservableObject
          {
              private YatzyGame thisYatzy = new YatzyGame();
      
              private string _strGameName;
              private string _strGameStatus;
      
              public string GameName
              {
                  get { return _strGameName; }
                  set
                  {
                      _strGameName = value;
                      RaisePropertyChangedEvent("GameName");
                  }
      
              }
      
              public string GameStatus
              {
                  get { return _strGameStatus; }
                  set {
                      _strGameStatus = value;
                      RaisePropertyChangedEvent("GameStatus");
                  }
              }
      
              public void ShiftState()
              {
                  thisYatzy.GameStatus.NextAround();
      
                  GameStatus = thisYatzy.GameStatus.ToString();
      
              }
          }
      }