4

Windows Phone 7のナビゲーションフレームワークは、Silverlightにあるものの縮小版です。Uriに移動することはできますが、ビューを渡すことはできません。NavigationServiceはビューに関連付けられているため、どのようにしてこれをMVVMに適合させることができますか。例えば:

public class ViewModel : IViewModel
{
    private IUnityContainer container;
    private IView view;

    public ViewModel(IUnityContainer container, IView view)
    {
        this.container = container;
        this.view = view;
    }

    public ICommand GoToNextPageCommand { get { ... } }

    public IView { get { return this.view; } }

    public void GoToNextPage()
    {
        // What do I put here.
    }
}

public class View : PhoneApplicationPage, IView
{
    ...

    public void SetModel(IViewModel model) { ... }
}

UnityIOCコンテナを使用しています。最初にビューモデルを解決してから、Viewプロパティを使用してビューを取得してから表示する必要があります。ただし、NavigationServiceを使用して、ビューUriを渡す必要があります。最初にビューモデルを作成する方法はありません。これを回避する方法はありますか?

4

4 に答える 4

4

コンストラクターを介してビューを渡す代わりに。最初に NavigationService を介してビューを構築し、それをビューモデルに渡すことができます。そのようです:

public class ViewModel : IViewModel
{
    private IUnityContainer container;
    private IView view;

    public ViewModel(IUnityContainer container)
    {
        this.container = container;
    }

    public ICommand GoToNextPageCommand { get { ... } }

    public IView 
    { 
        get { return this.view; } 
        set { this.view = value; this.view.SetModel(this); }
    }

    public void GoToNextPage()
    {
        // What do I put here.
    }
}

PhoneApplicationFrame frame = Application.Current.RootVisual;
bool success = frame.Navigate(new Uri("View Uri"));

if (success)
{
    // I'm not sure if the frame's Content property will give you the current view.
    IView view = (IView)frame.Content;
    IViewModel viewModel = this.unityContainer.Resolve<IViewModel>();
    viewModel.View = view;
}
于 2010-05-11T08:14:24.217 に答える
2

Mvvm Light を使用している場合は、次を試すことができます。

Windows Phone 7 — MVVM Light Messaging を使用したページ間のナビゲーション

(同様の投稿を参照してください: Mvvm-light(oobe)+MEF を使用した Silverlight ナビゲーション? )

于 2010-07-28T15:28:23.927 に答える
0

私の意見では、アプリケーションの起動時にビューモデルを作成して登録する必要があります。ルート DataContext 内に配置することで、コード ビハインドや IoC のトリックなしで、すべてのページが自動的に参照を取得します。

    // Code to execute when the application is launching (eg, from Start)
    // This code will not execute when the application is reactivated
    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
        m_ViewModel = new PrimaryViewModel(RootFrame) ;
        RootFrame.DataContext = m_ViewModel;
    }

    // Code to execute when the application is activated (brought to foreground)
    // This code will not execute when the application is first launched
    private void Application_Activated(object sender, ActivatedEventArgs e)
    {
        m_ViewModel = new PrimaryViewModel(RootFrame) ;
        m_ViewModel.Activated(PhoneApplicationService.Current.State);
        RootFrame.DataContext = m_ViewModel;
    }
于 2012-01-09T23:35:49.040 に答える