这几天做个WPF的项目,这东西真数据绑定做的真的挺反人类,还是JS好啊。
为了让主窗口与frame中的page相互调用以及传参。我们选择新建一个基类。
public class BasePage : Page
{
protectedMainWindow parentWin;
protected MainWindow ParentWin { get => parentWin; set => parentWin = value; }
}
修改页面:
将XAML的Page类型修改为BasePage
<local:BasePage x:Class="StoreManageApp.Pages.HistoryPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StoreManageApp.Pages"
mc:Ignorable="d"
Title="HistoryPage">
修改CS文件:
public partial class HistoryPage : BasePage
{
public HistoryPage(MainWindow win)
{
InitializeComponent();
this.parentWin = win;
}
主界面修改导航逻辑:
this.subPage.Navigate(new HistoryPage(this));
这样在子页面就可以直接调用主窗口逻辑。
以上
碰到问题:改变Page继承后,“Page”的分部声明一定不能指定不同的基类
可能原因: XAML中的页面类型和Page的实现类不匹配