在Windows通用应用程序上从ViewModel调用框架控件 [英] Call frame control from viewmodel on Windows universal app

查看:59
本文介绍了在Windows通用应用程序上从ViewModel调用框架控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MainPage.xaml代码:

my code of MainPage.xaml:

<Page
x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProject"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x1="using:System"
mc:Ignorable="d">
<Frame HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Name="frameMainPage"></Frame>

我的LoginViewModel.cs中的代码

My code from LoginViewModel.cs

...
if (m_login.Username == "username" && m_login.Password == "password")
                        {
                            Singleton.User = m_login;
                            App.log.Info("Success! Login done!");
                            //load in frame the dashboard page after login.
                        }
...

如何在Dashboard.xaml页面中获取我的框架并加载?可以通过viewmodel做到吗?

How to get my frame and load in Dashboard.xaml page ? It's possible do this from viewmodel ?

我的目标是在MainPage.xaml框架中加载页面.谢谢

My target is load a pages in the MainPage.xaml frame. Thanks

推荐答案

我想从viewmodel调用frameMainPage并在其中加载新页面.

I want call frameMainPage from viewmodel and load new page inside it frame.

在这种情况下,您可以使用框架

For this scenario, you could use Frame SourcePageType property to realize. If you want to the Frame loads a new page, you could modify SourcePageType's binding item like the follow.

internal class MainPageViewModel : INotifyPropertyChanged
{
    private Type _nextPage;

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public Type NextPage
    {
        get
        {
            return _nextPage;
        }
        set
        {
            _nextPage = value;
            OnPropertyChanged();
        }
    }

    public MainPageViewModel()
    {
         _nextPage = typeof(HomePage);
    }
}

MainPage.xaml

<Page.DataContext>
    <local:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Frame SourcePageType="{Binding NextPage,Mode=TwoWay}">
    </Frame>
</Grid>

这篇关于在Windows通用应用程序上从ViewModel调用框架控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆