Visual C++ 和 XAML 页面继承

Visual C++ and XAML Page Inheritance

我正在使用带有 Visual Studio 2015 的 Visual C++ 为 Windows 10 开发通用 Windows 应用程序。由于我们要使用旧的 C++ 库,该语言是一个限制条件进入应用程序。我的目标是为我的应用程序的所有页面使用基础 class。

基础class.h

namespace App1 {
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class MobyMainPage : public Windows::UI::Xaml::Controls::Page
    {

    internal:
         MobyMainPage();

    };
}

Subclass 有 XAML

#include "MobyMainPage.h"

namespace App1
{
    public ref class MainPage sealed
    {
    public:
        MainPage();

    };
}

子XAMLclass

<local:MobyMainPage
    x:Class="App1.MainPage"
    xmlns:local="using:App1">

    ...
</local:MobyMainPage>

错误发生在XAML文件的第一行。它说 MobyMainPage 在 App1 命名空间中不存在。 可以在通用 Windows 应用程序的 Visual C++ 中执行 XAML 继承?

这是我在 XAML 页面中重新使用 C++/CX class BasePage 的方法。

#1 创建一个 class 命名的 BasePage

BasePage.h

namespace FunctionPointerDemo
{
    public ref class BasePage : public ::Windows::UI::Xaml::Controls::Page
    {
    public:
        void InitializeComponent();
    };
}

BasePage.cpp

#include "pch.h"
#include "PageBase.h"

using namespace FunctionPointerDemo;

void BasePage::InitializeComponent()
{

}

#2 创建XAML 视图

在Visual Studio中,添加>新项目>XAML视图,命名为“HelloPage.xaml”。

添加后端class:HelloPage.xaml.h 和HelloPage.xaml.cpp

HelloPage.xaml

<Page
    x:Class="FunctionPointerDemo.HelloPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:FunctionPointerDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Button Click="Button_Click">Click Me</Button>
    </Grid>
</Page>

HelloPage.xaml.h

#pragma once

#include "PageBase.h"

namespace FunctionPointerDemo
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    [::Windows::Foundation::Metadata::WebHostHidden]
    public ref class HelloPage sealed : public BasePage,
        public ::Windows::UI::Xaml::Markup::IComponentConnector,
        public ::Windows::UI::Xaml::Markup::IComponentConnector2
    {
    public:
        HelloPage();
        void InitializeComponent();
        virtual void Connect(int connectionId, ::Platform::Object^ target);
        virtual ::Windows::UI::Xaml::Markup::IComponentConnector^ GetBindingConnector(int connectionId, ::Platform::Object^ target);

    private:
        bool _contentLoaded;
        void Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
    };
}

HelloPage.xaml.cpp

#include "pch.h"
#include "HelloPage.xaml.h"

using namespace FunctionPointerDemo;

HelloPage::HelloPage()
{
    InitializeComponent();
}


void FunctionPointerDemo::HelloPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{

}