在 ModalDialog 中使用来自 ViewModel 的数据,即使它位于 Shell

Using data from a ViewModel in a ModalDialog even though it lives in the Shell

我正在编写 UWP 并使用模板 10。

我创建了一个 ModalDialog,它应该向用户显示一些刚刚在 ViewModel 中计算的数据。

这是我迷路的地方:

#1, the ModalDialog needs data from my ViewModel. #2, the ModalDialog needs to call 1+ method(s) on the ViewModel depending on which button the user clicks.

我的Shell.xaml.cs:

public sealed partial class Shell : Page
{
    public static Shell Instance { get; set; }
    public static HamburgerMenu HamburgerMenu => Instance.MyHamburgerMenu;


    public Shell()
    {
        Instance = this;
        InitializeComponent();
        if (App.MobileService.CurrentUser == null)
            LoginModal.IsModal = true;
    }

    public Shell(INavigationService navigationService) : this()
    {
        SetNavigationService(navigationService);
    }

    public void SetNavigationService(INavigationService navigationService)
    {
        MyHamburgerMenu.NavigationService = navigationService;
    }

    #region Login

    private void LoginLoggedIn(object sender, EventArgs e)
    {
       MyHamburgerMenu.NavigationService.Navigate(typeof(Views.MainPage));
       LoginModal.IsModal = false;
    }

    #endregion
}

}

Shell.xaml

<Controls:ModalDialog x:Name="ScoreModal" Grid.RowSpan="3"
                          CanBackButtonDismiss="False"
                          DisableBackButtonWhenModal="True">
        <Controls:ModalDialog.ModalContent>
            <myControls:QuizScorePart
                    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
        </Controls:ModalDialog.ModalContent>
    </Controls:ModalDialog>

我尝试过的:

我尝试将 ModalDialog 的控件放在使用我希望与之交谈的 ViewwModel 的视图中,但这不起作用;该视图位于 shell 内,这意味着 ModalDialog 下的所有内容都未禁用。据我所知,它必须在 Shell 中。

我尝试在 Shell.xaml.cs 文件中设置一个方法,将我的对话框的 IsModal 设置为 true/false;这有效,但它没有解决我与 ViewModel 交互的问题。

我迷路了。感谢任何人的帮助。

参考Search的例子,实际部分有delegates要处理,看LoginPart的codebehind....

我在下面的评论中指定的是我如何使用 LoginPage,而不是用户控件。其中有一个 LoginPageViewModel,后者又引用了一个 SettingsService 实例。

编辑

这样想...ScoreModal 只不过是另一个视图控件。 QuizScorePart 是您的视图,我假设 QuizScorePartViewModal 存在。从那里它变成了视图模型之间的消息传递练习。至少这是我在最后一条评论后看到的。你需要知道用户点击了什么按钮。假设以上内容为真,那么 QuizViewModel 将对它正在侦听的消息做出反应。 Shell 只是一个完整屏幕覆盖的保持位置,因为它只对 IsModal 做出反应。如果这是一个问题,请考虑一个服务来保存按钮选择,类似于 SettingsService 的工作方式。没有说 QuizScorePart 不能将其数据上下文设置为 QuizViewModel 的数据上下文,但此时它可能是一个测试问题。