Hello, I can not figure out the addition of an object in the Model.
namespace SnakeGame.Model { [DataContract] public class DataModel { [DataMember] public List<Player> Players { get; set; } public static string DataPath = "score.dat"; public DataModel() { Players = new List<Player>() { new Player() {Score = 133, Name = "Ivan", Number = 6, Status = Player.ScoreStatus.Use} }; } public static DataModel Load() { if (File.Exists(DataPath)) return DataSerializer.DeserializeItem(DataPath); return new DataModel(); } public void Save() { DataSerializer.SerializeData(DataPath,this); } } } Next on the button is adding a new player, I do so.
public partial class AddUserControll : UserControl { [DataMember] public IEnumerable<Player> Players { get; set; } public static DataModel _model; public static DataViewModel _viewModel; public static string namePlayer; private int count = 5; public AddUserControll() { InitializeComponent(); } private void button_Click(object sender, RoutedEventArgs e) { namePlayer = name.Text; var tmp = new Player() { Score = 0, Name = namePlayer, Number = count, Status = Player.ScoreStatus.Use }; _model.Players.Add(tmp); count++; try { _model = Mapper.Map<DataViewModel, DataModel>(_viewModel); _model.Save(); } catch (Exception) { } } } System.NullReferenceException gets out
By MVVM, everything should be correct, all View inherited by ViewModelBase: INotifyPropertyChanged
[DataMember]? Do you use some kind of framework? - VladDbutton_Clickis inDataModel _model)? This is not MVVM-ovski. - VladDNullReferenceException? Show call stack. - VladD