【问题标题】:Call frame control from viewmodel on Windows universal app从 Windows 通用应用程序上的视图模型调用帧控制
【发布时间】:2018-03-26 08:21:20
【问题描述】:

我的 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 代码

...
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 页面? 可以从视图模型中做到这一点吗?

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

【问题讨论】:

  • 如何获取我的框架并在 Dashboard.xaml 页面中加载? 您要加载哪个框架? MainPage ?
  • @NicoZhu-MSFT 我想从 viewmodel 调用 frameMainPage 并在它的框架内加载新页面。

标签: c# xaml uwp win-universal-app frame


【解决方案1】:

我想从 viewmodel 调用 frameMainPage 并在它的框架内加载新页面。

对于这种场景,可以使用FrameSourcePageType属性来实现。如果你想Frame加载一个新页面,你可以像下面这样修改SourcePageType的绑定项。

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>

【讨论】:

    猜你喜欢
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多