gpt4 book ai didi

ios - MvvmCross iOS UITableView 不会在属性更改时更新

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:34 26 4
gpt4 key购买 nike

只要更改其源属性,我的 TableView 就不会更新。代码如下:

View Controller :

    public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.White;

var table = new UITableView(new RectangleF(0, 80, Device.Width, Device.Height - 80));
Add(table);
var source = new MvxStandardTableViewSource(table, "TitleText SessionInfo");
table.Source = source;

var set = this.CreateBindingSet<JoinSessionViewController, JoinSessionViewModel>();
set.Bind(source).To(vm => vm.AvailableServers);
set.Apply();

table.ReloadData();
}

View 模型:

    private readonly INetworkSessionClient _client;

public JoinSessionViewModel(INetworkSessionClient client)
{
_client = client;

_client.ServerFound += ClientOnServerFound;
_client.BeginSearchingForServers();
}

private void ClientOnServerFound(object sender, ServerFoundEventArgs serverFoundEventArgs)
{
if (AvailableServers.Any(s => s.Identifier == serverFoundEventArgs.ServerInfo.Identifier))
return;

AvailableServers.Add(serverFoundEventArgs.ServerInfo);
RaisePropertyChanged(() => AvailableServers);
}

private List<ServerInfo> _availableServers;
public List<ServerInfo> AvailableServers
{
get { return _availableServers; }
set { _availableServers = value; RaisePropertyChanged(() => AvailableServers); }
}

最佳答案

这是一个棘手的问题。罪魁祸首是 MvxTableViewSource 中的这一行:

https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxTableViewSource.cs#L56

    public virtual IEnumerable ItemsSource
{
get { return _itemsSource; }
set
{
if (_itemsSource == value) // **** This one ****
return;

if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
}

_itemsSource = value;

var collectionChanged = _itemsSource as INotifyCollectionChanged;
if (collectionChanged != null)
{
_subscription = collectionChanged.WeakSubscribe(CollectionChangedOnCollectionChanged);
}

ReloadTableData();
}
}

因为我只是添加到列表而不是将其设置为新列表,所以 setter 提前返回并且没有触发 ReloadTableData()。解决方法是使用 ObservableCollection(或任何其他实现 INotifyCollectionChanged 的​​东西)而不是 List。

    private void ClientOnServerFound(object sender, ServerFoundEventArgs serverFoundEventArgs)
{
if (AvailableServers.Any(s => s.Identifier == serverFoundEventArgs.ServerInfo.Identifier))
return;

// CollectionChanged event is not automatically martialed to UI thread
InvokeOnMainThread(() => AvailableServers.Add(serverFoundEventArgs.ServerInfo));
}

private ObservableCollection<ServerInfo> _availableServers;
public ObservableCollection<ServerInfo> AvailableServers
{
get { return _availableServers; }
set { _availableServers = value; RaisePropertyChanged(() => AvailableServers); }
}

请注意,由于我的特殊情况,将在单独的线程上调用 ServerFound 事件。这意味着 INotifyCollectionChanged.CollectionChanged 事件也将在单独的线程上调用,因此必须使用 InvokeOnMainThread() 将服务器添加到列表的操作执行到主线程上,以便 UI 正确更新。

如果 CollectionChanged 事件能在 UI 线程上自动处理就好了,类似于 RaisePropertyChanged() 的工作方式。

关于ios - MvvmCross iOS UITableView 不会在属性更改时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161149/

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