c# - 通用 Windows 应用上的 ViewModel (MVVM) 绑定(bind)页面

标签 c# wpf mvvm uwp

我需要在 Xaml wpf 页面中的 Frame 控件上绑定(bind)一个页面。
我的 xml 页面:

<Page
x:Class="MyPro.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyPro"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodel="using:MyPro.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x1="using:System"
mc:Ignorable="d">
<Page.DataContext>
    <viewmodel:PagerViewModel x:Name="PagerViewModel"></viewmodel:PagerViewModel>
</Page.DataContext>
<Frame 
       VerticalContentAlignment="Stretch"
       HorizontalContentAlignment="Stretch"
       Name="frameMainPage"
       DataContext="{Binding Source=Pager.Page, Mode=TwoWay}">        
</Frame>

我试过用这个(但我不知道它是否正确):

DataContext="{Binding Source=Pager.Page, Mode=TwoWay}"



但不起作用。

我的 View 模型,我调用 Pager 来设置新页面:
class PagerViewModel
{
    public PagerViewModel()
    {
        m_pager = new Pager();
    }

    public static Pager m_pager;  
    public Pager Pager
    {
        get
        {
            return m_pager;
        }
        set
        {
            m_pager = value;
        }
    }
}

和我的模型,我设置页面模式是这样的:
public class Pager : INotifyPropertyChanged
{
    private Page m_page;
    public Page Page
    {
        get
        {
            return m_page;
        }
        set
        {
            m_page = value;
            OnPropertyChanged("Page");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

我需要从代码中的每个部分更改这样的页面:
PagerViewModel.m_pager.Page = new MyPage();

如何在通用 Windows 应用 UWP 上执行此操作?

最佳答案

我已经这样解决了:

DataContext="{Binding Path=Pager.Page, Mode=TwoWay}"

您必须在 UWP 上的通用应用程序中使用路径而不是源

关于c# - 通用 Windows 应用上的 ViewModel (MVVM) 绑定(bind)页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49688122/

相关文章:

wpf - Canvas ItemsControl 的问题

c# - 可编辑的 ComboBox 绑定(bind)和更新源触发器

c# - 在 DataTrigger WPF 上开始 Storybard

c# - WPF 基础 : Shared global styles for MVVM

model-view-controller - MVVM:查找其他ViewModel

c# - 将 gridview 的多行置于编辑模式

c# - 提取部分字符串

c# - 在调用 Add() 之后但在调用 SaveChanges() 之前从上下文中检索实体

c# - 根据一组规则验证日期

c# - 在 WPF 中的用户控件上创建可绑定(bind)属性的正确方法是什么?