简体   繁体   English

C#SQLite INotifyPropertyChanged实现

[英]C# SQLite INotifyPropertyChanged implementation

I'm using SQLite in my application to store some Message objects. 我在应用程序中使用SQLite来存储一些Message对象。 I display a list of Message items in "View 1". 我在“视图1”中显示Message项列表。 When I change a property in the Edit View "View 2", I want the property to also change in the list. 当我在“视图2”的“编辑视图”中更改属性时,我希望该属性也在列表中更改。

Classes

Message.cs Message.cs

class Message : INotifyPropertyChanged
{
    private uint _id;
    public uint Id 
    { 
        get
        {
            return _id;
        }
        set
        {
            // Trigger INotifyPropertyChanged
            Set("Id", ref _id, value);
        }
    }

    private string _content;
    public string Content 
    {
        get
        {
            return _content;
        }
        set
        {
            // Trigger INotifyPropertyChanged
            Set("Content", ref _content, value);
        }
    }

    ...
}

MessageViewModel.cs MessageViewModel.cs

class MessageViewModel : INotifyPropertyChanged
{
    private Message message;

    ...

    private string _content;
    public string Content 
    {
        get
        {
            return message.Content;
        }
        set
        {
            // Set value
            message.Content = value;

            // Trigger INotifyPropertyChanged
            RaisePropertyChanged();
        }
    }

    ...
}

View 1 查看1

View1.xaml View1.xaml

The datacontext is View1ViewModel 数据View1ViewModelView1ViewModel

View1ViewModel.cs View1ViewModel.cs

private List<MessageViewModel> _messages;
public List<MessageViewModel> Messages
{
    get
    {
        return _messages;
    }
    set
    {
        // Trigger INotifyPropertyChanged
        Set("Messages", ref _messages, value);
    }
}

...

private async void loadMessages()
{
    // Get the messages from SQLite database
    var messages = await newMessages();
    Messages = new MessageViewModelCollection(messages);
}

View 2 查看2

View2ViewModel.cs View2ViewModel.cs

private MessageViewModel _message;
public MessageViewModel Message
{
    get
    {
        return _message;
    }
    set
    {
        // Trigger INotifyPropertyChanged
        Set("Message", ref _message, value);
    }
}

...

private async void loadMessage()
{
    // Get the message from SQLite database by Id
    var message = await newMessage(messageId);
    Message = new MessageViewModel(message);
}

The functions newMessages and newMessage(uint messageId) return new Message objects from the database. 函数newMessagesnewMessage(uint messageId)从数据库返回new Message对象。

I normally use the INotifyPropertyChanged implementation, but this doesn't work. 我通常使用INotifyPropertyChanged实现,但这不起作用。 I query the database 2 times, once for the list (View 1), and once for the edit page (View 2). 我查询数据库两次,一次查询列表(视图1),一次查询编辑页面(视图2)。 The SQLite returns two different copies of the Message object, therefore the INotifyPropertyChanged meganism will not work (Only in the current page, not the pages in the backstack). SQLite返回Message对象的两个不同副本,因此INotifyPropertyChanged机制将不起作用(仅在当前页面中,而不在后退页面中)。

I could fix the problem by re-using the Message item from the list, but I cannot always do this in all views. 我可以通过重新使用列表中的“ Message项来解决此问题,但是我不能总是在所有视图中都做到这一点。

Is there a way to make the INotifyPropertyChanged work in this scenario? 在这种情况下,有没有办法使INotifyPropertyChanged工作? Or do I need a different approach to update the values? 还是我需要其他方法来更新值?

You need a different approach. 您需要一种不同的方法。 IMHO, the best thing would be to separate the data model from the view model. 恕我直言,最好的办法是将数据模型与视图模型分开。 So have a MessageVm that binds to the WPF view, and put the logic into it to update itself appropriately (along with firing any necessary INotifyPropertyChanged 's) from the Message objects that get passed into it. 因此,有一个绑定到WPF视图的MessageVm ,并放入其中的逻辑以从传递到其中的Message对象适当地更新自身(以及触发任何必要的INotifyPropertyChanged )。

There are some automatic mapping tools like Automapper which might, to some degree, alleviate the pain that comes along with this approach. 有一些自动映射工具,例如Automapper ,可以在某种程度上减轻这种方法带来的痛苦。

But you really should separate the view model from the data model, the decoupling of these layers is one of the principle tenets of WPF programming. 但是您确实应该将视图模型与数据模型分开,这些层的解耦是WPF编程的主要原则之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM