gpt4 book ai didi

c# - Windows Phone 8 MVVM Linq 表在 NotifyPropertyChanging 上创建新实例

转载 作者:太空狗 更新时间:2023-10-29 21:54:09 25 4
gpt4 key购买 nike

我有一个 Linq DataContext 作为应用程序的数据库。我已经设置了 MVVM 模式并且能够将新记录插入到数据库中。但是,当我加载这些记录并尝试更新它们时,会在后台创建一个新的记录实例,并随着属性更改进行更新。因此,当他们的 UI 调用保存命令时,最初加载的记录实例没有更改,也没有保存。

据我所知这是事件的顺序

  • 加载实例 1
  • 开始更新属性
  • 调用 NotifyPropertyChanging
  • 新实例 2 已加载
  • 新实例 2 已更新
  • 从实例 1 的 UI 调用保存更改
  • 未进行任何更改,因为实例 1 尚未更新

下面是我的代码:

/* 这是实体 */

[Table]
public class User : IDisposable, INotifyPropertyChanged, INotifyPropertyChanging
{
private MyDataContext context;

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangingEventHandler PropertyChanging;
private void NotifyPropertyChanging(String propertyName)
{
PropertyChangingEventHandler handler = PropertyChanging;
if (null != handler)
{
handler(this, new PropertyChangingEventArgs(propertyName));
}
}

public void Dispose()
{
context.Dispose();
}

private Guid _id;
[Column(IsPrimaryKey = true, IsDbGenerated = false, DbType = "UNIQUEIDENTIFIER NOT NULL", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public Guid Id
{
get { return _id; }
set
{
if (_id != value)
{
NotifyPropertyChanging("Id");
_id = value;
NotifyPropertyChanged("Id");
}
}
}

private string _name;
[Column(CanBeNull = false)]
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
NotifyPropertyChanging("Name"); // This line creates the new entity
_name = value;
NotifyPropertyChanged("Name");
}
}
}

public User()
{
this.context = MyDataContext.GetContext();
}

public override void SaveChanges()
{
if (_id == Guid.Empty)
{
this.Id = Guid.NewGuid();
context.Users.InsertOnSubmit(this);
context.SubmitChanges();
}
else
{
context.SubmitChanges();
}
}

public static User NewInstance()
{
return new User
{
Name = String.Empty
};
}
}

/* 这是数据上下文 */

public class MyDataContext : DataContext
{
// Specify the connection string as a static, used in main page and app.xaml.
public static string ConnectionString = "Data Source=isostore:/MyApp.sdf;Password=pwd";

public MyDataContext(string connectionString) : base(connectionString) { }

public static MyDataContext GetContext()
{
var context = new MyDataContext(ConnectionString);
return context;
}

public Table<User> Users;
}

/* 这是 View 模型 */

public sealed class UserViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

private User _user;

public UserViewModel(Guid id)
{
using (MyDataContext context = new MyDataContext(MyDataContext.ConnectionString))
{
_User = context.User.First(u => u.Id == id);
}
}

public UserViewModel(User user)
{
_user = user;
}

public UserViewModel()
{
_user = User.NewInstance();
}

public string Name
{
get { return _user.Name; }
set
{
_user.Name = value;
NotifyPropertyChanged("Name");
}
}

private ICommand _saveCommand;
public ICommand SaveCommand
{
get
{
return _saveCommand ?? (_saveCommand = new GenericCommand(() =>
{
_user.SaveChanges();
}, true));
}
}
}

最佳答案

在您的 MyDataContext 中,我认为您需要下面的内容,这是一个基本的单例概念,这样您就可以处理同一个对象,从而保存同一个对象并进行更改。

private static DataContext context = null;

public static MyDataContext GetContext()
{
if(context == null)
context = new MyDataContext(ConnectionString);
return context;
}

edit- 请注意,这会对您的应用程序产生重大影响。可能需要在创建新的时重新设计,以及是否/何时应该专门将其设置为 null。

关于c# - Windows Phone 8 MVVM Linq 表在 NotifyPropertyChanging 上创建新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22343352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com