Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Xamarin渲染页面中的页面_Android_Xamarin_Xamarin.forms_Xamarin.android - Fatal编程技术网

Android Xamarin渲染页面中的页面

Android Xamarin渲染页面中的页面,android,xamarin,xamarin.forms,xamarin.android,Android,Xamarin,Xamarin.forms,Xamarin.android,我需要能够在ContentPage中显示ContentPage。所有当前的Xamarin内容控件只允许将视图作为其内容。有一个内容控制,有一个网页作为内容打开了相当多的可能性,所以我渴望得到这个工作 我已经创建了一个自定义控件,该控件将ContentPage作为内容,并且可以在Android上成功呈现子页面。但是,呈现的子页面没有呈现其子页面。请参阅下面的渲染器,但我只是为子页面获取正确的渲染器并进行设置。我必须做什么才能获得子页面的子级渲染。我本以为它的渲染器会处理这个 主机控件 using

我需要能够在ContentPage中显示ContentPage。所有当前的Xamarin内容控件只允许将视图作为其内容。有一个内容控制,有一个网页作为内容打开了相当多的可能性,所以我渴望得到这个工作

我已经创建了一个自定义控件,该控件将ContentPage作为内容,并且可以在Android上成功呈现子页面。但是,呈现的子页面没有呈现其子页面。请参阅下面的渲染器,但我只是为子页面获取正确的渲染器并进行设置。我必须做什么才能获得子页面的子级渲染。我本以为它的渲染器会处理这个

主机控件

using Xamarin.Forms;

namespace TestApp.Views.Controls
{
    [ContentProperty("Content")]
    public class PageViewContainer: View
    {
        public static readonly BindableProperty ContentProperty = BindableProperty.Create("Content",
                                                                                          typeof(ContentPage),
                                                                                          typeof(PageViewContainer));

        public ContentPage Content
        {
            get { return (ContentPage)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }
    }
}
成功呈现子页面但由于某些原因未呈现子页面内容的Android呈现器

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using TestApp.Views.Controls;
using Android.Content;
using System.ComponentModel;
using TestApp.Renderers;

[assembly: ExportRenderer(typeof(PageViewContainer), typeof(PageViewContainerRenderer))]
namespace TestApp.Renderers
{
    public class PageViewContainerRenderer : ViewRenderer<PageViewContainer, Android.Views.View>
    {
        public PageViewContainerRenderer(Context context): base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<PageViewContainer> e)
        {
            base.OnElementChanged(e);
            var pageViewContainer = e.NewElement as PageViewContainer;
            if (e.NewElement != null)
            {
                ChangePage(e.NewElement.Content);
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == "Content")
            {
                ChangePage(Element.Content);
            }
        }

        private void ChangePage(ContentPage page)
        {
            if (page != null)
            {
                var renderer = Platform.CreateRendererWithContext(page, this.Context);
                SetNativeControl(renderer.View);
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Easilog.Forms.Views.Pages.Account.ChildPage
             BackgroundColor="Red">
    <ContentPage.Content>
            <Label
                Text="A child label that is not rendering :-("
                TextColor="White"
                FontSize="Large">
            </Label>
    </ContentPage.Content>
</ContentPage>
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android;
使用TestApp.Views.Controls;
使用Android.Content;
使用系统组件模型;
使用TestApp.Renders;
[程序集:ExportRenderer(typeof(PageViewContainer)、typeof(PageViewContainerRenderer))]
命名空间TestApp.Renderers
{
公共类PageViewContainerRenderer:ViewRenderer
{
公共页面ViewContainerRenderer(上下文):基本(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
var pageViewContainer=e.NewElement作为pageViewContainer;
if(例如NewElement!=null)
{
变更页面(如新元素、内容);
}
}
受保护的覆盖无效OnElementPropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
如果(例如,PropertyName==“内容”)
{
变更页面(元素、内容);
}
}
私有无效更改页(内容页)
{
如果(第页!=null)
{
var renderer=Platform.createrenderwithcontext(第页,this.Context);
SetNativeControl(renderer.View);
}
}
}
}
样本页

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestApp.Pages.PageInPage"
             xmlns:custom_controls="clr-namespace:TestApp.Views.Controls">
    <ContentPage.Content>    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="8*"></RowDefinition>
                <RowDefinition Height="2*"></RowDefinition>
            </Grid.RowDefinitions>
            <custom_controls:PageViewContainer
                Grid.Row="0"
                Content="{Binding ChildPage}">
            </custom_controls:PageViewContainer>
            <Label
                Grid.Row="1"
                Text="A Label"
                FontSize="Large">
            </Label>
        </Grid>
    </ContentPage.Content>
</ContentPage>

具有页面呈现器(显示红色背景)但未呈现子标签的子页面

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using TestApp.Views.Controls;
using Android.Content;
using System.ComponentModel;
using TestApp.Renderers;

[assembly: ExportRenderer(typeof(PageViewContainer), typeof(PageViewContainerRenderer))]
namespace TestApp.Renderers
{
    public class PageViewContainerRenderer : ViewRenderer<PageViewContainer, Android.Views.View>
    {
        public PageViewContainerRenderer(Context context): base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<PageViewContainer> e)
        {
            base.OnElementChanged(e);
            var pageViewContainer = e.NewElement as PageViewContainer;
            if (e.NewElement != null)
            {
                ChangePage(e.NewElement.Content);
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            if (e.PropertyName == "Content")
            {
                ChangePage(Element.Content);
            }
        }

        private void ChangePage(ContentPage page)
        {
            if (page != null)
            {
                var renderer = Platform.CreateRendererWithContext(page, this.Context);
                SetNativeControl(renderer.View);
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Easilog.Forms.Views.Pages.Account.ChildPage
             BackgroundColor="Red">
    <ContentPage.Content>
            <Label
                Text="A child label that is not rendering :-("
                TextColor="White"
                FontSize="Large">
            </Label>
    </ContentPage.Content>
</ContentPage>


如果要呈现标签,我认为应该尝试使用
var renderer=Platform.createrenderwithcontext(page.Content,this.Context)ChangePage()方法中的code>