[英]how to pass ObservableCollection from one page1 to page2 without use MVVM
I want to pass ObversableCollecion temp from page1 to page2 ,when I click the button in page1.But I don't wan't to use MVVM,just be simnple.My code are as follows: 当我单击page1中的按钮时,我想将ObversableCollecion临时从page1传递到page2。但是我不想使用MVVM,请简单地说。我的代码如下:
page1: 第1页:
public ObservableCollection<Staff> Staff_Collection { get; set; }
private void button_click1(object sender, RoutedEventArgs e)
{
Page1 page1= new Page1();
page1.Staff_Show = Staff_Collection;
this.NavigationService.Navigate(page1);}
page2: 第2页:
public ObservableCollection<Staff> Staff_Show = new ObservableCollection<Staff>();
public TaxBefore_Sum()
{
InitializeComponent();
DataContext = this;
Staff_Show = new ObservableCollection<Staff>();
}
but it doesn't work. 但这不起作用。
var whateverPage = new WhateverPage(whateverDataYouWantToPass);
this.NavigationService.Navigate(whateverPage);
Just make sure, the constructor for WhateverPage
(the page you are navigating to) is overloaded to accept the item you are passing into it like this: 只需确保,
WhateverPage
(导航到的页面)的构造函数已超载,以接受您要传递给它的项目,如下所示:
public WhateverPage(WhateverData data)
{
// code here
}
So in your case, if you are navigating to a page called Page2
, then you will create a constructor in that page like this: 因此,在您的情况下,如果导航到名为
Page2
的页面,则将在该页面中创建一个构造函数,如下所示:
public Page2(ObservableCollection<Staff> staff)
{
// Now you have staff in Page2. You can assign it to a property or field
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.