MvvmCross;如何从另一个ViewModel进行RaisePropertyChange [英] MvvmCross; How to RaisePropertyChange from another ViewModel

查看:110
本文介绍了MvvmCross;如何从另一个ViewModel进行RaisePropertyChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ShoppingCart listView ,其中绑定 ShopingCartViewModel .当我单击到一个项目时,它带我到 ItemInfoFragment ,它与 ItemInfoViewModel 绑定强>.

I have a ShoppingCart listView with items that is bound to ShopingCartViewModel. When I click to an item it takes me to the ItemInfoFragment which is bound to ItemInfoViewModel.

ItemInfoFragment 中,我有一个按钮,该按钮删除 项目删除购物车 列表视图中选择.

In ItemInfoFragment I have a button which deletes the item and removes it from the ShoppingCart listview.

我的问题是;在我删除 项目按下 后退按钮后,返回到我的先前 >活动购物车 列表视图仍会显示删除的项目 强>.

My problem is; After i delete the item and press backbutton to return to my previously activity, the ShoppingCart listView still shows the Item that I deleted.

我的问题是;当我退出ItemInfoFragment时,如何在ShoppingCartViewModel中进行RaisePropertyChange?

My Question is; How to RaisePropertyChange in ShoppingCartViewModel when i exit the ItemInfoFragment?

推荐答案

我相信您有几个选择:

共享持久存储

如果您使用 SQLite 这样的存储/缓存解决方案,或领域等.可用于读取和修改同一购物页面之间的购物车数据.然后,您可以使用视图生命周期事件(OnResume [Android]或ViewWillAppear [iOS])从缓存中检索最新的事件.

If you use a storage/caching solution like SQLite or Realm etc. Which can be used to read and modify the same shopping cart data between pages. You can then use view life cycle events (OnResume[Android] or ViewWillAppear[iOS]) to retrieve the latest from the cache.

或者,如果购物车数据量较小,则可以将其读/写到 MvvmCross设置插件.您只需要序列化和反序列化对象,因为您只能保存基本类型,例如字符串,bools,int等.

Alternatively if the shopping cart data size is small you could read/write it to MvvmCross Settings Plugin. You will just have to serialize and deserialize your objects as you can only save basic types like strings, bools, int etc.

依赖注入共享实例

您可以通过使用共享的类实例来创建内存缓存,并且可以在多个ViewModel之间共享它们.该类的属性可以直接绑定到您的各种视图.对列表的任何更改将更新绑定到列表的所有视图.需要注意的一件事是,如果您需要此实例类占用的内存空间,则必须手动进行清理.

You can create an in memory cache via using a shared class instance the can be shared between multiple ViewModels. This classes properties can bind directly to your various views. Any changes to the list will update all views that bind to it. One thing to note is that you will have to manually handle clean up if you require the memory space occupied by the this instance class.

示例:

示例模型

public class ItemInfo
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

共享的类实例和接口

public interface ISharedShoppingCart
{
    MvxObservableCollection<ItemInfo> ShoppingCartItems { get; set; }
}

public class SharedShoppingCart : MvxNotifyPropertyChanged, ISharedShoppingCart
{
    MvxObservableCollection<ItemInfo> _shoppingCartItems;
    public MvxObservableCollection<ItemInfo> ShoppingCartItems
    {
        get { return _shoppingCartItems; }
        set { SetProperty(ref _shoppingCartItems, value); }
    }
}

请确保注册类和接口

public class App : MvxApplication
{
    public override void Initialize()
    {
        /* Other registerations*/

        Mvx.LazyConstructAndRegisterSingleton<ISharedShoppingCart, SharedShoppingCart>();
    }
}

共享ViewModel中的用法示例

Example usage in shared ViewModels

public class ShopingCartViewModel : MvxViewModel
{
    readonly ISharedShoppingCart _sharedShoppingChart;

    public ShopingCartViewModel(ISharedShoppingCart sharedShoppingChart)
    {
        _sharedShoppingChart = sharedShoppingChart;
    }

    public MvxObservableCollection<ItemInfo> ShoppingCartItems
    {
        get { return _sharedShoppingChart.ShoppingCartItems; }
        set { _sharedShoppingChart.ShoppingCartItems = value; }
    }
}

public class ItemInfoViewModel : MvxViewModel
{
    readonly ISharedShoppingCart _sharedShoppingCart;

    public ItemInfoViewModel(ISharedShoppingCart sharedShoppingCart)
    {
        _sharedShoppingCart = sharedShoppingCart;
    }

    void RemoveItemFromCart(int id)
    {
        _sharedShoppingCart.ShoppingCartItems
            .Remove(_sharedShoppingCart.ShoppingCartItems.Single(x => x.Id == id));
    }
}

发布/订阅

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆