TLDR; 由于 Xamarin Forms 不支持路由事件,因此 Xamarin Forms 中是否有替代模式来执行我在这里要求的操作。我需要做的是从 subview 的 View 模型中触发一个事件,并让它被父 View 的 View 模型捕获。
在我的设置中,我有一个页面根页面,它是 MasterDetail页:
public partial class RootPage : MasterDetailPage
我将master设置为
MainMenuPage
和 child 到MyChildPage1
.var rootPage = new RootPage
{
Master = new MainMenuPage(),
Detail = new MyChildPage1()
};
App.MainPage = rootPage;
我为它们三个都分配了一个 ViewModel:
rootPage.BindingContext = new RootPageViewModel();
rootPage.Master.BindingContext = new MainMenuPageViewModel();
rootPage.Detail.BindingContext = new MyChildPage1ViewModel();
MainMenuPage
中有一个按钮.当您单击它时,它会触发 ICommand
在 MainMenuPageViewModel
.这是正常的 MVVM/Binding 东西。在
MainMenuPageViewModel
在 ICommand
,我想开火。 (路由事件?)我希望 RootPage
捕获此事件. RootPage
然后将更改绑定(bind)到 rootPage.Detail
的 ViewModel 上的属性(如何?)
最佳答案
您可以使用消息中心
Xamarin Messaging Center
关于c# - 在 Xamarin Master-Detail MVVM 设置中,如何从 subview 的 View 模型中触发事件并让它被父 View 的 View 模型捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183078/