Xamarin.Forms.Xaml.XamlParseException:找不到MarkupExtension

Mic*_*lle 5 c# xaml markup-extensions xamarin.forms

我试图使用Xamarin表单的自定义标记扩展,以便最终实现本地化.我试图深入了解Xamarin形式的例子.

以下是使用扩展名的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>
Run Code Online (Sandbox Code Playgroud)

以下是Translation Extension Markup的代码:

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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我注释掉了一些代码,以防出现问题.

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

Mic*_*lle 4

程序集名称错误。我正在使用 Xamarin 表单的共享项目,并将共享项目的名称作为程序集的名称。因此,如果您有共享项目,则程序集的名称必须是使用共享项目的程序集。

  • 当我用小写 b 而不是大写拼写“绑定”时,出现类似的错误消息,例如(错误)。, &lt;Label Text="{binding Date, StringFormat='{0:d}'}" /&gt; (2认同)