2016-03-01 80 views
1

我试图在Xamarin中添加的WIndows Desktop项目中添加Xaml UserControl和/或UserPage。页面渲染器不显示任何Windows UI项目(页面或用户控件和按钮)。你能帮我在哪里犯错。Xamarin页面渲染器WinRT Desktop UserControl未渲染

CameraPageRenderer

[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 
     } 
    } 

} 

CameraPage 从便携式库页面调用

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> 

回答

0

我发现了部分解决我的问题。它将在PhotoCapturePage的情况下工作,但在UserControl PhotoCaptureControl的情况下可能不会。

我替换的代码

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

以下行 -

this.SetNativeControl(new PhotoCapturePage()); 

此解决方案,如果需要添加一个或多个本地用户控件将无法正常工作。