我需要在 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/