Silverlight MVVM 将结果从子窗口 ViewModel 移动到父窗口 ViewModel

发布于 2025-01-08 22:32:52 字数 789 浏览 0 评论 0原文

您好,我有一个使用 MVVM light 的 silverlight MVVM 应用程序。 当我打开应用程序时,应弹出一个子窗口,在子窗口中指定条件并单击“确定”按钮后,主窗口应显示详细信息。

 public MainPage()
    {
        ChildPage cp = new ChildPage();
        cp.Show();
        InitializeComponent();
    }

单击子窗口上的“确定”按钮后,该窗口应消失并在主窗口上显示对象列表。在子窗口的视图模型中,我有一个 RelayCommand OKCommand。

 private void WireCommands()
    {
        OKCommand = new RelayCommand(GetEmployees);
    }

    private void GetEmployees()
    {
        IEnumerable<Employees> employees;
                       employees = from employee in Employees where employee.Name == selectedEmployee.Name select employee;

        Employees= new ObservableCollection<Employee>(employees);
    }

员工获得了所需的结果。但我不知道如何关闭子窗口并将结果移动到父窗口。提前致谢。

Hi I have a silverlight MVVM application using MVVM light.
When I open the application a child window should popup and upon specifying the condition in the child window and clicking OK button the main window should display the details.

 public MainPage()
    {
        ChildPage cp = new ChildPage();
        cp.Show();
        InitializeComponent();
    }

upon hitting OK button on the child window this window should disappear and display a list of objects on the main window. In the View Model of the child window I have a RelayCommand OKCommand.

 private void WireCommands()
    {
        OKCommand = new RelayCommand(GetEmployees);
    }

    private void GetEmployees()
    {
        IEnumerable<Employees> employees;
                       employees = from employee in Employees where employee.Name == selectedEmployee.Name select employee;

        Employees= new ObservableCollection<Employee>(employees);
    }

The Employees has the required result. But I dont know how to close the chils window and move the result to the parent window. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海不蓝 2025-01-15 22:32:52

您可以使用(按解耦的递增顺序):

  1. 当您在 MainPage 中拥有对 ChildPage 的引用时,您可以访问其属性。
  2. 使用标准 .NET 事件,其中事件位于子页面上,并且订阅在 MainPage 中完成
  3. 使用 事件聚合器模式。多个 MVVM 框架实现了事件聚合器模式。

使用 .NET 事件

ChildPage cp = new ChildPage();
cp.NameReceived += NameReceived;
cp.Show();

private void NameRecieved(object sender, NameReceivedEventArgs eventArgs)
{
  // retrieve employees using eventargs.Name
}

使用 Caliburn.Micro 的事件聚合器

public class MainPage : Screen, IHandle<NameReceivedMessage>
{
  public MainPage(IEventAggregator eventAggregator)
  {
    eventAggregator.Subscribe(this);
  }

  public void Handle(NameReceivedMessage message)
  {
    // retrieve employees using message.Name which is the inputted name
  }
}

这里,我们在从 ChildPage 接收到姓名后,在 MainPage 中进行员工检索。或者,您可以检索 ChildPage 中的员工,并将它们传递到事件参数/消息中。

You can use (in increasing order of decoupling):

  1. As you have a reference to the ChildPage in MainPage, you can access its properties.
  2. Use standard .NET events, where the event is on the child page, and the subscribing is done in the MainPage
  3. Use an event aggregator pattern. Several MVVM frameworks implement the event aggregator pattern.

Using .NET Events

ChildPage cp = new ChildPage();
cp.NameReceived += NameReceived;
cp.Show();

private void NameRecieved(object sender, NameReceivedEventArgs eventArgs)
{
  // retrieve employees using eventargs.Name
}

Using Event Aggregator from Caliburn.Micro

public class MainPage : Screen, IHandle<NameReceivedMessage>
{
  public MainPage(IEventAggregator eventAggregator)
  {
    eventAggregator.Subscribe(this);
  }

  public void Handle(NameReceivedMessage message)
  {
    // retrieve employees using message.Name which is the inputted name
  }
}

Here we are doing the employee retrieval in the MainPage, after receiving the name from the ChildPage. Alternatively, you could retrieve the employees in the ChildPage, and pass them in the event args/message.

红墙和绿瓦 2025-01-15 22:32:52

要关闭子窗口,您可以使用 ChildWindow 的 Close() 方法,也可以将 DialogResult 属性设置为 true 或 false,这也会将其关闭。
您必须在 ChildPage 的 OK 按钮的 OnClick 事件的隐藏代码中执行此操作。

要访问 ChildPage 的 ViewModel 的 Members 属性,您可以执行类似的操作:

public MainPage()
{
    ChildPage cp = new ChildPage();
    cp.Closed += (s,e) =>
    {
      //Do something with (cp.DataContext as ChildPageViewModel).Employees
    }
    cp.Show();
    InitializeComponent();
}

To close the child window, you can either use the Close() Method of the ChildWindow or you can set the DialogResult property to true or false which also close it.
You have to do it in the code-behind of ChildPage on the OnClick event of the OK Button.

To access the Employees property of the ChildPage's ViewModel you can do something like that :

public MainPage()
{
    ChildPage cp = new ChildPage();
    cp.Closed += (s,e) =>
    {
      //Do something with (cp.DataContext as ChildPageViewModel).Employees
    }
    cp.Show();
    InitializeComponent();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文