如何通过参数来导航视图模型与WinRT的Caliburn.Micro? [英] How to pass parameter to navigated view model with WinRT Caliburn.Micro?
问题描述
我开发一个Windows应用商店使用的的WinRT Caliburn.Micro 的应用程式的游戏,我依靠导航框架。
I am developing a Windows Store apps game using WinRT Caliburn.Micro, and I am relying on the navigation framework.
我有视图模型的游戏的设置(确定玩家)和实际的游戏。当从设置导航的游戏,我想玩家收集传递给游戏视图模型。我怎样才能做到这一点。
I have view models for the game setup (define players) and the actual game. When navigating from the setup to the game, I want to pass the collection of players to the game view model. How can I do this?
示意性地,我的看法车型目前看起来是这样的:
Schematically, my view models currently look like this:
public class SetupGameViewModel : NavigationViewModelBase
{
public SetupGameViewModel(INavigationService ns) : base(ns) { }
public IObservableCollection<Player> Players { get; set; }
public void StartGame()
{
// This is as far as I've got...
base.NavigationService.NavigateToViewModel<GameViewModel>();
// How can I pass the Players collection from here to the GameViewModel?
}
}
public class GameViewModel : NavigationViewModelBase
{
public GameViewModel(INavigationService ns) : base(ns) { }
public ScoreBoardViewModel ScoreBoard { get; private set; }
public void InitializeScoreBoard(IEnumerable<Player> players)
{
ScoreBoard = new ScoreBoardViewModel(players);
}
}
在理想情况下,我想打电话给 InitializeScoreBoard
GameViewModel 构造函数中>,但据我已经能够告诉它是不可能通过 SetupGameViewModel.Players
收集到 GameViewModel
构造。
Ideally, I would like to call InitializeScoreBoard
from within the GameViewModel
constructor, but as far as I have been able to tell it is not possible to pass the SetupGameViewModel.Players
collection to the GameViewModel
constructor.
INavigationService.NavigateToViewModel< T>
(扩展)方法可以选择将一个 [对象]参数
的说法,但这个参数似乎并没有达到视图模型构造导航到。我无法弄清楚如何显式调用 GameViewModel.InitializeScoreBoard
从 SetupGameViewModel.StartGame
方法的方法,因为相关的 GameViewModel
并没有在这一阶段被初始化。
The INavigationService.NavigateToViewModel<T>
(extension) method optionally takes an [object] parameter
argument, but this parameter does not seem to reach the view model constructor navigated to. And I cannot figure out how to explicitly call the GameViewModel.InitializeScoreBoard
method from the SetupGameViewModel.StartGame
method either, since the GameViewModel
has not been initialized at this stage.
推荐答案
在年底我通过实施临时事件处理程序解决了这个。原来,我可以使用 NavigateToViewModel< T>(对象)
超载通过玩家收集。
In the end, I solved this by implementing a temporary event handler. It turned out that I could use the NavigateToViewModel<T>(object)
overload to pass the player collection.
从卡利微论坛和 MSDN文档我得到的印象是,这种做法是只有保证了原始类型的工作,虽然在我的情况我迄今没有发现任何问题,它。
From the Caliburn Micro discussion forum and MSDN documentation I get the impression that this approach is only guaranteed to work for "primitive" types, although in my scenario I have so far not detected any problems with it.
我的 SetupGameViewModel.StartGame
方法现在已经实现如下:
My SetupGameViewModel.StartGame
method is now implemented as follows:
public void StartGame()
{
base.NavigationService.Navigated += NavigationServiceOnNavigated;
base.NavigationService.NavigateToViewModel<GameViewModel>(Players);
base.NavigationService.Navigated -= NavigationServiceOnNavigated;
}
以及非常的临时连接 NavigationServiceOnNavigated
事件处理程序的实现如下:
And the very temporarily attached NavigationServiceOnNavigated
event handler is implemented as follows:
private static void NavigationServiceOnNavigated(object sender, NavigationEventArgs args)
{
FrameworkElement view;
GameViewModel gameViewModel;
if ((view = args.Content as FrameworkElement) == null ||
(gameViewModel = view.DataContext as GameViewModel) == null) return;
gameViewModel.InitializeScoreBoard(args.Parameter as IEnumerable<Player>);
}
不是真正的干净的解决方案我为努力,但至少它似乎的工作。
Not really the clean solution I had striven for, but at least it seems to work.
这篇关于如何通过参数来导航视图模型与WinRT的Caliburn.Micro?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!