2017-03-06 157 views
0

我正在嘗試創建一個按鈕渲染器。我遵循自定義渲染器在此處找到的條目:Xaml解析異常按鈕Rederer

該解決方案使用PLC構建。

我使用錯誤的程序集嗎?

Xamarin.Forms.Xaml.XamlParseException被拋出

類型本地:在錯誤

更多信息則myButton的xmlns中未發現:地方= 「CLR的命名空間:MyApp的;裝配= MyApp的」

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/entry/

我得到一個xaml解析異常錯誤。

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:MyApp;assembly=MyApp" 
    x:Class="MyApp.LoginPage" 
    BackgroundColor="White"> 
    <StackLayout>   
      <local:MyButton 
       Text="Login" 
       TextColor="Blue" 
       BackgroundColor="Transparent" 
       HorizontalOptions="CenterAndExpand" /> 
    </StackLayout> 
</ContentPage> 

Android的渲染:

using Xamarin.Forms.Platform.Android; 
using Xamarin.Forms; 
using DeliveryTracker; 
using DeliveryTracker.Droid; 

[assembly: ExportRenderer(typeof(MyButton), typeof(MyButtonRenderer))] 

    namespace MyApp.Droid 
    { 
     class MyButtonRenderer : ButtonRenderer 
     { 
      protected override void OnDraw(Android.Graphics.Canvas canvas) 
      { 
       base.OnDraw(canvas); 
      } 
     } 
    } 

在形式:

using System; 
using Xamarin.Forms; 

namespace MyApp 
{ 
    public class MyButton : Button 
    { 
     public MyButton() 
     { 
     } 
    } 
} 
+0

您還沒有告訴我們關於您所看到的錯誤的任何信息。 「xaml解析異常」並不能提供足夠的信息。什麼是錯誤信息?它是否指定了XAML中的特定位置? –

+1

啓用XAML編譯以獲取有關根本原因的更多信息:https://developer.xamarin.com/guides/xamarin-forms/xaml/xamlc/ – Jason

+0

錯誤來自XAML預覽。在模擬器中運行應用程序後,我得不到任何錯誤,並顯示自定義按鈕渲染器。 – MartDavious

回答

2

你試過

xmlns:local="clr-namespace:MyApp"

而不是

xmlns:local="clr-namespace:MyApp;assembly=MyApp"

?如果它有效,你可能會重命名你的項目或改變你的名字空間。它已經發生在我身上。檢查控件的名稱空間並確保它與XAML中的相同。

+0

這對我有用!但是排除裝配零件意味着什麼?不是需要嗎? –

+0

如果您在多個程序集中擁有相同的命名空間,我認爲程序集部分是必需的。大多數情況下,情況並非如此,這就是爲什麼刪除它沒有任何作用(或者如果您重命名項目或更改了名稱空間,則可以解決您的問題)。 –

+0

但是,不應該有其他方法來解決這個問題,因爲這個問題的原因是項目被重命名了?像改變一些配置文件中的東西一樣? –