Xamarin在Xaml中继承ContentPage

时间:2017-06-26 21:30:41

标签: xaml inheritance xamarin xamarin.forms

如何使用xamarin表单创建基本内容页面类继承?

我按照forum提出建议。

一切都很好,直到我必须解决Xaml部分。

using System;
using Xamarin.Forms;

namespace MyProject
{
    public class BaseContentPage : ContentPage
    {
        public BaseContentPage()
        {
        }
    }
}


using System;

using Xamarin.Forms;

namespace MyProject
{
    public partial class DashboardPage : BaseContentPage
    {
        public DashboardPage()
        {
            InitializeComponent();
        }

        void GoToJobDetailsPage(object sender, EventArgs args)
        {
            var masterPage = Application.Current.MainPage as MasterDetailPage;
            masterPage.Detail = new H2HNavigationPage(new JobDetailPage());
        }
    }
}


<?xml version="1.0" encoding="UTF-8"?>
<d.BaseContentPage
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="clr-namespace:MyProject;assembly=MyProject"
    x:Class="MyProject.DashboardPage"
    ControlTemplate="{StaticResource Background}">
    <ContentPage.Content>

    </ContentPage.Content>
</d.BaseContentPage>

当我这样做时,我得到编译器错误

...DashboardPage.xaml.g.cs(64,64): Error CS0234: The type or namespace name 'd' does not exist in the namespace 'Xamarin.Forms' (are you missing an assembly reference?) (CS0234)

当然,我可以认为d别名不在范围内,但我该怎么做才能创建自定义基类内容页面?

1 个答案:

答案 0 :(得分:6)

在XAML中,使用&#34;:&#34;作为命名空间和类名之间的分隔符

<d:BaseContentPage>
  ...
</d:BaseContentPage>