反向关系和UI更新不起作用?

时间:2017-03-07 11:29:38

标签: c# xamarin realm

我目前正在开发一个信使应用程序。因此,我使用realm将数据存储在设备上。我的应用程序包含一个页面,用于表示Listview中的所有Chatthread。因此,我将ListView绑定到IRealmCollection。如果我在Messagethread中更改了属性,我可以看到Litview正在相应地更新。但是当我在线程中插入一条消息时,UI却没有。

public class Messagethread : RealmObject
{
    [PrimaryKey]
    public string Guid { get; set; }

    public IList<User.User> Users { get;  }

    [Backlink(nameof(Message.Message.MessageThread))]
    public IQueryable<Message.Message> Messages { get; }

    public User.User Owner { get; set; }

    public string Title { get; set; }

    public int CountOfUnreadMessages { get; set; }
    public bool IsPrivate { get; set; }

    public DateTimeOffset LastSyncDate { get; set; }
    public DateTimeOffset LastSingleSyncDate { get; set; }
    public int ItemSyncStatus { get; set; }

    public void CallPropertyChanged(string name)
    {
        base.OnPropertyChanged(name);
    }
}

public class Message : RealmObject
{
    [PrimaryKey]
    public string Guid { get; set; }

    public Messagethread MessageThread { get; set; }
    public User.User Author { get; set; }

    public string Body { get; set; }

    [Backlink(nameof(MessageAttachement.MessageAttachement.Message))]
    public IQueryable<MessageAttachement.MessageAttachement> Attachements { get; }

    public string Title { get; set; }
    public DateTimeOffset SendDate { get; set; }
    public bool SystemMessage { get; set; }
    public DateTimeOffset LocalSyncDate { get; set; }
    public DateTimeOffset ChangeTime { get; set; }

    public bool IsSynced { get; set; }
}

我在插入新邮件时尝试在Property“Messages”上调用PropertyChanged。但这也行不通。

如果更改了反向链接属性,是否有办法实现自动更新UI的行为?

我感谢任何帮助 儒略

1 个答案:

答案 0 :(得分:1)

快速的One2Many示例,没有MVVM / Viper / MVC / MVP,也没有XAML来保持尽可能简单。

数据模型(您的简化版本):

One to Many导航(MessageThread ->* 消息)

public class MessageThread : RealmObject
{
    [PrimaryKey]
    public string Guid { get; set; }

    public IList<Message> Messages { get; }
}

public class Message : RealmObject
{
    [PrimaryKey]
    public string Guid { get; set; }
    public string Body { get; set; }

    [Backlink(nameof(MessageThread.Guid))]
    public MessageThread Thread { get; }
}

Simple Master / Detail Forms UI:

enter image description here

Xamarin.Forms应用示例:

public class BackLinkApp : Application
{
    readonly Realm realm;
    readonly IRealmCollection<MessageThread> allMessageThreads;
    readonly Button addMessageThread;
    readonly Button addMessageToThread;
    IRealmCollection<MessageThread> currentMessageThread;
    string currentMessageThreadPriKey;

    public BackLinkApp()
    {
        realm = Realm.GetInstance("backlinks.realm");
        allMessageThreads = (IRealmCollection<MessageThread>)realm.All<MessageThread>();

        var messageCell = new DataTemplate(typeof(TextCell));
        messageCell.SetBinding(TextCell.TextProperty, "Body");
        messageCell.SetBinding(TextCell.DetailProperty, "Guid");
        var messageListView = new ListView
        {
            ItemTemplate = messageCell,
        };
        var threadCell = new DataTemplate(typeof(TextCell));
        threadCell.SetBinding(TextCell.TextProperty, "Guid");
        var messageThreadListView = new ListView
        {
            ItemsSource = allMessageThreads,
            ItemTemplate = threadCell
        };

        messageThreadListView.ItemSelected += (sender, e) =>
        {
            currentMessageThreadPriKey = (messageThreadListView.SelectedItem as MessageThread).Guid;
            currentMessageThread = (IRealmCollection<MessageThread>)realm.All<MessageThread>().Where((messageThread) => messageThread.Guid == currentMessageThreadPriKey);
            messageListView.ItemsSource = currentMessageThread?.FirstOrDefault()?.Messages;
            addMessageToThread.IsEnabled = currentMessageThread?.Count() > 0;
        };

        addMessageThread = new Button { Text = "Add New MessageThread" };
        addMessageThread.Clicked += (sender, e) =>
        {
            realm.Write(() =>
            {
                var messageThread = new MessageThread { Guid = Guid.NewGuid().ToString() };
                realm.Add(messageThread);
            });
        };

        addMessageToThread = new Button { Text = "Add New Message to Thread", IsEnabled = false };
        addMessageToThread.Clicked += (sender, e) =>
        {
            realm.Write(() =>
            {
                currentMessageThread = (IRealmCollection<MessageThread>)realm.All<MessageThread>().Where((messageThread) => messageThread.Guid == currentMessageThreadPriKey);
                var counter = currentMessageThread.FirstOrDefault().Messages.Count() + 1;
                var message = new Message { Guid = Guid.NewGuid().ToString(), Body = $"StackoverFlow:{counter}" };
                currentMessageThread?.FirstOrDefault()?.Messages.Add(message);
            });
        };

        MainPage = new ContentPage
        {
            Content = new StackLayout
            {
                VerticalOptions = LayoutOptions.Center,
                Children = {
                    addMessageThread, messageThreadListView, addMessageToThread, messageListView
                }
            }
        };
    }
}

通过将messageListView.ItemsSource的绑定源设置为当前选定的Messages对象的MessageThread属性,您的用户界面将自动&#34;自动&#34 ;将新消息添加到该线程时更新:

messageListView.ItemsSource = currentMessageThread?.FirstOrDefault()?.Messages;

enter image description here