0

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

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

这是我迷路的地方:

#1,ModalDialog 需要来自我的 ViewModel 的数据。#2,ModalDialog 需要在 ViewModel 上调用 1+ 个方法,具体取决于用户单击的按钮。

我的 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
}

}

壳牌.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 的视图中,但这不起作用;该视图位于外壳内,这意味着 ModalDialog 下的所有内容都未被禁用。据我所知,它必须在外壳中。

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

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

4

1 回答 1

0

参考搜索示例,在实际部分中有要处理的委托,请查看 LoginPart 的代码隐藏......

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

编辑

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

于 2016-06-03T05:39:41.013 回答