C# 在通用Windows应用程序上使用ViewModel(MVVM)绑定页面
我需要在Xaml wpf页面的框架控件上绑定一个页面。 我的xaml页面:C# 在通用Windows应用程序上使用ViewModel(MVVM)绑定页面,c#,wpf,mvvm,uwp,C#,Wpf,Mvvm,Uwp,我需要在Xaml wpf页面的框架控件上绑定一个页面。 我的xaml页面: <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.microso
<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>
在我的模型中,我设置页面模式如下:
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
}
DataContext="{Binding Path=Pager.Page, Mode=TwoWay}"
我需要从代码中的每个部分更改如下页面:
PagerViewModel.m_pager.Page = new MyPage();
如何在通用windows应用程序UWP上执行此操作 绑定到
Frame
的DataContext
不起任何作用DataContext
基本上只是告诉控件其绑定的相对路径引用了什么,但不会导致任何行为(至少内置控件是这样)
在本例中,您需要绑定到框架
控件的内容
属性:
<Frame Content="{Binding Pager.Page}" />
SecondPage
是一个空页面,我将其设置为蓝色背景,以便能够清楚地看到它显示在框架
中,绑定到框架
的数据上下文
不起任何作用DataContext
基本上只是告诉控件其绑定的相对路径引用了什么,但不会导致任何行为(至少内置控件是这样)
在本例中,您需要绑定到框架
控件的内容
属性:
<Frame Content="{Binding Pager.Page}" />
SecondPage
是一个空白页面,我将其设置为蓝色背景,以便能够清楚地看到它显示在框架中
我已解决了以下问题:
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
}
DataContext="{Binding Path=Pager.Page, Mode=TwoWay}"
您必须在UWP上的Universal App中使用路径而不是源,我已经这样解决了:
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
}
DataContext="{Binding Path=Pager.Page, Mode=TwoWay}"
您必须在UWP上的Universal App中使用Path而不是Source您是否不需要
页面上的OnPropertyChanged
?我还没有测试过它,但是绑定框架的内容属性而不是datacontext
属性不是更好吗?我不知道是绑定内容还是绑定datacontext更好,我需要一个解决方案,我愿意接受所有的想法。但我知道,如果我绑定内容…在我的视图中,我会找到字符串格式的Pager.page,白色背景。你不需要页面上的OnPropertyChanged
吗?我还没有测试过它,但是绑定框架的内容属性而不是数据上下文
属性不是更好吗?我不知道是绑定内容还是绑定数据上下文更好,我需要一个解决方案,我愿意接受所有想法。但我知道,如果我绑定内容…在我的视图中,我会找到字符串格式的Pager.page,背景为白色。