Xamarin Page Renderer WinRT Desktop UserControl not rendered

本文关键字:UserControl not rendered Desktop WinRT Page Renderer Xamarin | 更新日期: 2023-09-27 18:36:47

我尝试在Xamarin中添加的WIndows Desktop项目中添加Xaml UserControl和/或UserPage。页面呈现器不显示任何 Windows UI 项(页面或用户控件和按钮)。你能帮忙我在哪里犯错误吗?

相机页面渲染器

[assembly: ExportRenderer(typeof(Shared.CameraPage), typeof(CameraPageRenderer))]
namespace MyApp.Windows
{
public class CameraPageRenderer : PageRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null || Element == null)
        {
            return;
        }
        try
        {
             //1st test
            this.Children.Add(new PhotoCaptureControl());
             //[OR] 2nd Test
            this.Children.Add(new PhotoCapturePage());
        }
        catch (Exception ex)
        {
            //Logger class
        }
    }
}

相机页面从"可移植库"页面调用

public class CameraPage : ContentPage
   {
    public CameraPage(TaskCompletionSource<MediaFile> SelectionTask)
    { 
        Content = new StackLayout
        {
        };
    }
}

PhotoCaptureControl.xaml.cs文件具有初始化方法。

<UserControl x:Class="MyApp.Windows.PhotoCaptureControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="using:MyApp.Windows"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         d:DesignHeight="300"
         d:DesignWidth="400"
         mc:Ignorable="d">
<StackPanel>
    <TextBlock Text="Capture Photo text" />
    <Button x:Name="BtnCapturePhoto"
            HorizontalAlignment="Center"
            Content="Capture Photo" />
</StackPanel>    
</UserControl>

PhotoCapturePage.xaml .cs包含 InitializeComponents() 方法

<Page x:Class="eWorker.Windows.PhotoCapturePage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:local="using:eWorker.Windows"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="4*" />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Button x:Name="BtnCapturePhoto"
            Grid.Row="1"
            HorizontalAlignment="Center"
            Content="Capture Photo" />
</Grid>
</Page>

Xamarin Page Renderer WinRT Desktop UserControl not rendered

我找到了解决问题的部分解决方案。它将在PhotoCapturePage的情况下工作,但在用户控制的情况下可能不起作用 PhotoCaptureControl .

我替换了以下代码行

this.Children.Add(new PhotoCapturePage());

跟-

 this.SetNativeControl(new PhotoCapturePage());

如果需要添加一个或多个本机用户控件,则此解决方案将不起作用。