java model举例_如何从另一个视图模型实例化和显示ViewModel

我是MVVM的新手,我跟着josh smith article,我正在努力开发我的第一次尝试 . 在我的例子中,我有一个主窗口,它有一个主视图模型:

var vm = new MainVM();

MainWindow window = new MainWindow();

window.DataContext = vm;

我有两个viewmodels ItemSuppliersViewModel , SuppliersViewModel 绑定到两个视图 ItemSuppliers , SuppliersView 在主窗口 resourcedictionary 中使用 datatemplate ,如下所示:

在主窗口中,我有一个列表框,显示Binded to:

AllItems 是主视图模型公开的公共属性:

public IList AllItems { get { return (IList)_itemsRepository.FindAll(DetachedCriteria.For()); } }

当用户从列表框中选择一个项目时,显示与该项目相关的一些数据的列表,由 ItemSuppliers View模型和 ItemSuppliersView 表示,并使用 itemscontrol 显示到网格中:

ItemSuppliersVM 在主视图模型中公开如下:

ItemSuppliersViewModel itemSuppliersVM;

public ItemSuppliersViewModel ItemSuppliersVM

{

get

{

return _itemSuppliersVM;

}

set

{

_itemSuppliersVM = value;

OnPropertyChanged("ItemSuppliersVM");

}

}

以下是绑定到列表框所选项目的selecteditem属性:

public Item SelectedItem

{

get

{

return _selectedItem;

}

set

{

_selectedItem = value;

OnPropertyChanged("SelectedItem");

ShowItemSuppliers();

}

}

创建itemsuppliers视图模型的 showItemSuppliers :

void ShowItemSuppliers()

{

_itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30));

}

问题是当选择列表框中的任何项目时没有发生任何事情,但是 itemsrepository 已经过测试并且工作正常,当我的断点所有绑定都正常工作并且它遍历 selecteditem 属性然后是 showitemsuppliers() 方法 .

I think the problem is in this method, so what's wrong and is this method is the right way to instantiate the ItemSuppliersViewModel in the mainwindow view model?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java ViewModel 是一种用于管理 UI 组件数据的架构组件。ViewModel 与 Activity 或 Fragment 等 UI 组件相关联,并存储 UI 组件需要的数据,以便在旋转屏幕或配置更改等情况下保持数据。 以下是一个简单的 ViewModel 实例,它管理一个整数变量: ``` import androidx.lifecycle.ViewModel; public class MyViewModel extends ViewModel { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } ``` 此 ViewModel 包含一个名为 `count` 的整数变量,以及两个公共方法:`increment` 和 `getCount`。`increment` 方法用于递增 `count` 变量的值,`getCount` 方法返回当前 `count` 变量的值。 在 Activity 或 Fragment 使用此 ViewModel: ``` public class MyActivity extends AppCompatActivity { private MyViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 MyViewModel 实例 viewModel = new ViewModelProvider(this).get(MyViewModel.class); // 使用 ViewModel 数据更新 UI updateUI(); // 设置一个按钮点击事件,当用户点击按钮时递增 count 变量 Button incrementButton = findViewById(R.id.increment_button); incrementButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewModel.increment(); updateUI(); } }); } private void updateUI() { // 更新 UI 的文本视图显示当前 count 变量的值 TextView countTextView = findViewById(R.id.count_text_view); countTextView.setText(String.valueOf(viewModel.getCount())); } } ``` 在 `onCreate` 方法,我们获取 `MyViewModel` 实例,并使用 `updateUI` 方法更新 UI 的文本视图,以显示 `count` 变量的值。我们还设置了一个按钮点击事件,当用户点击按钮时,我们递增 `count` 变量并再次调用 `updateUI` 方法,以便在 UI 更新 `count` 变量的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值