我尝试在Xamarin中添加的WIndows Desktop项目中添加Xaml UserControl和/或UserPage。页面渲染器不显示任何Windows UI项(页面或用户控件和按钮)。你能帮助我在哪里犯错误吗?
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文件有Initialize方法。
<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 :(得分:0)
我找到了解决问题的部分方法。它适用于PhotoCapturePage
但在UserControl PhotoCaptureControl
的情况下可能不适用。
我替换了以下代码行
this.Children.Add(new PhotoCapturePage());
with -
this.SetNativeControl(new PhotoCapturePage());
如果需要添加一个或多个本机UserControl,此解决方案将无效。