使用 RelayCommand进行导航在 MVVM 轻量级中

发布于 2024-11-23 15:41:45 字数 929 浏览 0 评论 0原文

我一直在关注 Jesse Liberty 的 tutorial on MVVM Light for Windows Phone 7,但我陷入了这个问题。我需要从主页导航到详细信息页面。按照本教程,我在 MainViewModel 中使用 RelayCommand:

public RelayCommand<Customer> DetailsPageCommand { get; private set;}

然后在构造函数中初始化它:

DetailsPageCommand = new RelayCommand<Customer>((msg) => GoToDetailsPage(msg));

最后实现 GoToDetailsPage 方法:

private object GoToDetailsPage(Customer msg)
{
    System.Windows.MessageBox.Show("Go to details page with: " +
        msg.First +
        " " +
        msg.Last );
        return null;
}

显示消息框有效,但我不确定如何导航到详细信息页面。在教程的前面部分中,页面导航是通过如下方式处理的:

var msg = new GoToPageMessage {PageName = "DetailPage"};
Messenger.Default.Send(msg);

I have been following Jesse Liberty's tutorial on MVVM Light for Windows Phone 7, but I'm stuck on this problem. I need to navigate from a main page to a detail page. Following the tutorial, I'm using a RelayCommand in the MainViewModel:

public RelayCommand<Customer> DetailsPageCommand { get; private set;}

I then initialize it in the constructor:

DetailsPageCommand = new RelayCommand<Customer>((msg) => GoToDetailsPage(msg));

Finally you implement the GoToDetailsPage method:

private object GoToDetailsPage(Customer msg)
{
    System.Windows.MessageBox.Show("Go to details page with: " +
        msg.First +
        " " +
        msg.Last );
        return null;
}

Showing the message box works, but I'm not sure how to navigate to the detail page instead. In previous sections of the tutorial page navigation was handled with something like this:

var msg = new GoToPageMessage {PageName = "DetailPage"};
Messenger.Default.Send(msg);

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

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

发布评论

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

评论(1

伤痕我心 2024-11-30 15:41:45

您需要注册才能接收该类型的消息,然后进行适当的导航。
以下假定页面名称,并且您通过在查询字符串中传递特定客户的 ID 来导航到特定客户的详细信息。

Messenger.Default.Register<Customer>(
    this,
    c => NavigationService.Navigate("/Pages/CustomerDetails.xaml?cid=" + c.Id));

然后,您可以相应地调整您的代码:

private void GoToDetailsPage(Customer msg)
{
    Messenger.Default.Send(msg);
}

我希望这会有所帮助。

You'll need to register to receive messages of that type and then navigate appropriately.
The following assumes a page name and that you're navigating to details of the specific customer by passing their Id in the query string.

Messenger.Default.Register<Customer>(
    this,
    c => NavigationService.Navigate("/Pages/CustomerDetails.xaml?cid=" + c.Id));

You'd then adjust your code accordingly:

private void GoToDetailsPage(Customer msg)
{
    Messenger.Default.Send(msg);
}

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文