【问题标题】:Page navigation in WP7 Mango?WP7 Mango 中的页面导航?
【发布时间】:2011-11-03 10:05:20
【问题描述】:

在以下用于处理页面导航的方案中寻找 cmets。使用 Mvvm Light Messenger 以广播方式发送消息,因此如果多页面解决方案中的所有 ViewModel 都侦听相同类型的消息,则每个人都将收到 所有 消息。过滤掉当前 ViewModel 需要处理的是 HandleIncomingMessage()

我还想知道在哪里存储流经应用程序的“全球化”数据,到目前为止,我已经使用 App.xaml.cs 中为 currentCustomerId 等定义的静态属性。但是我是否也应该将对象图与来自数据库在这里?

另一种方法是扩展或重载 PageTransitionMessageType() 并提供属性以将特定消息发送到每个页面。这样,您就不必担心上述传入消息的过滤。

感谢任何cmets!

// 在 ViewModelLocator 中

public static readonly Uri Page1Uri = new Uri("/Views/Page1.xaml", UriKind.Relative);
public static readonly Uri Page2Uri = new Uri("/Views/Page2.xaml", UriKind.Relative);
public static readonly Uri Page3Uri = new Uri("/Views/Page3.xaml", UriKind.Relative);

//为Page2创建类似的页面def

public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
    InitializeComponent();
    Messenger.Default.Register<PageTransitionMessageType>(this, (action) => NavigationHandler(action));

}

private void NavigationHandler(PageTransitionMessageType action)
{
    NavigationService.Navigate(action.PageUri);
}
}

//为Page3创建类似的VM

public class Page2ViewModel : ViewModelBase
{
public Page2ViewModel ()
{
    Messenger.Default.Register<PageTransitionMessageType>(this, (s) => HandleIncomingMessage(s));
}

private void HandleIncomingMessage(PageTransitionMessageType s)
{
    // check for page2 message
    if (s.PageUri == ViewModelLocator.Page2Uri)
    {
        // do cunning page2 stuff...
    }
}
}

//为Page2创建类似的VM

public class Page1ViewModel : ViewModelBase
{
public RelayCommand GotoPage2Cmd { get; private set; }

public Page1ViewModel()
{
    GotoPage2Cmd = new RelayCommand(() => ExecuteGoToPage2(), () => CanExecuteGoToPage2());
}

private void ExecuteGoToPage2()
{
    var message = new PageTransitionMessageType() { PageUri = ViewModelLocator.Page2Uri };
    Messenger.Default.Send<PageTransitionMessageType>(message);
}
}       


public class PageTransitionMessageType
{
    public Uri PageUri { get; set; }
    // e.g. put props with data you'd like to pass from one page to another here

}       

【问题讨论】:

    标签: windows-phone-7 mvvm mvvm-light


    【解决方案1】:

    我建议将“全局”变量保存在 IsolatedStorage

    【讨论】:

    • 谢谢!离开这个项目有一段时间了,但现在事情已经解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    相关资源
    最近更新 更多