2014-10-30 21 views
5

我试图使用Xamarin形式的自定义标记扩展,以最终实现本地化。我正在努力去研究Xamarin表单的例子。Xamarin.Forms.Xaml.XamlParseException:找不到MarkupExtension

这里是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:CRI.MAP.Mobile.Views;assembly=CRI.MAP.Mobile" 
    x:Class="CRI.MAP.Mobile.Views.CustomerSearchPage"> 

    <StackLayout> 
     <Button 
      Text="{local:Translate Clear}" 
      Command="{Binding ClearCommand}" /> 
    </StackLayout> 

下面是翻译扩展标记代码:

using System; 
using Xamarin.Forms.Xaml; 
using Xamarin.Forms; 
using System.Diagnostics; 

namespace CRI.MAP.Mobile.Views 
{ 
    // You exclude the 'Extension' suffix when using in Xaml markup 
    [ContentProperty ("Text")] 
    public class TranslateExtension : IMarkupExtension 
    { 
     public string Text { get; set; } 

     public object ProvideValue (IServiceProvider serviceProvider) 
     { 
      //if (Text == null) 
      // return null; 
      //Debug.WriteLine ("Provide: " + Text); 
      // Do your translation lookup here, using whatever method you require 
      //var translated = L10n.Localize (Text); 

      //return translated; 
      return "hello"; 
     } 
    } 
} 

我注释掉一些代码,以防万一这是问题的原因。

每当我尝试运行这个,我得到的错误:Xamarin.Forms.Xaml.XamlParseException:MarkupExtension找不到本地:翻译。我很困惑,为什么它没有找到标记扩展,因为我所看到的所有示例似乎都以相同的方式完成。有人知道这是为什么吗?一般来说,我在寻找Xamarin表单的很好例子时遇到了很多麻烦。

回答

4

程序集名称错误。我为Xamarin表单使用共享项目,并将共享项目的名称作为程序集的名称。所以如果你有一个共享的项目,程序集的名字需要是使用共享项目的程序集。

+0

嗨米歇尔,谢谢分享!你是如何设法从XAML处理不同的命名空间(iOS,Droid,WinPhone)?我的意思是,如果你的程序集是'''assembly = MyApp''',那么当为Android构建时,你需要在为WP构建''''MyApp.Droid'''时更改为'''assembly = MyApp.WinPhone''' ,usw ... – Wizche 2014-12-22 09:42:13

+0

记住我正在做一个共享项目,所以我使它成为每个项目的相同程序集名称。程序集名称是在iOS,Android和Windows的每个项目的构建属性中定义的。命名空间指向名称空间,我在其中写入了扩展名。这是否更有意义? – Michelle 2015-01-02 16:04:33

+0

是的,我结束了同样的解决方案。 Xamarin的岩石;) – Wizche 2015-01-02 17:19:22

相关问题