我使用PageRenderer時遇到了一些麻煩。Xamarin Forms UWP PageRenderer
MainPage.xml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="abc.CustomView">
<ContentPage.Content>
<StackLayout>
<Button Text="scann" Clicked="BtnScannClicked"></Button>
</StackLayout>
</ContentPage.Content>
MainPage.cs
async void BtnScannClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new CustomView());
}
CustomView.Xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="abc.CustomView">
<ContentPage.Content>
</ContentPage.Content>
</ContentPage>
CustomView.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomView : ContentPage
{
public CustomView()
{
InitializeComponent();
}
}
個
DemoPage.cs(這是我CustomRenderer)
[assembly: ExportRenderer(typeof(CustomView), typeof(DemoPage))]
namespace abc.UWP
{
class DemoPage: PageRenderer
{
Page page;
Application app;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Page> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
try
{
app = Application.Current;
SetupUserInterface();
this.Children.Add(page);
}
catch (Exception ex)
{
Debug.WriteLine(@" ERROR: ", ex.Message);
}
}
void SetupUserInterface()
{
var stackPanel = new StackPanel();
page = new Page();
page.Content = stackPanel;
}
}
}
總是有 拋出異常: 'System.InvalidOperationException' 在Xamarin.Forms.Platform.UAP.dll 錯誤的構建過程中。
但我想這不是一個真正的問題與PageRenderer。看起來這是在ClickEvent期間出現的。
好的,問題是使用Navigation.PushAnsync,而不是PushModalAsync。現在導航部分似乎工作(沒有我的DemoPage.cs中的exportRenderer程序集)。在導航到DemoPage.cs後,我的應用程序總是崩潰(退出代碼爲-1)。實施應該沒問題,或不? – flix