c# - Xamarin.Forms.Xaml.XamlParseException : MarkupExtension not found

标签 c# xaml xamarin.forms markup-extensions

我正在尝试将自定义标记扩展与 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>

这是翻译扩展标记的代码:

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 for local:Translate。我很困惑为什么它找不到标记扩展,因为我看到的所有示例似乎都是以相同的方式完成的。有谁知道为什么会这样?我在寻找 Xamarin 表单的好例子时遇到了很多麻烦。

最佳答案

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

关于c# - Xamarin.Forms.Xaml.XamlParseException : MarkupExtension not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657751/

相关文章:

ios - 将 ScrollView 推送到内容页面异步不起作用

c# - 使导航栏在 Xamarin.Forms 中消失

c# - 将 c# 代码放在代码隐藏中并将其编译为 DLL 是否比内联 &lt;script&gt; block 更安全?

c# - 尽管指定了 .Returns,最小起订量对象仍返回 null

c# - Windows Phone 中的语义缩放?

c# - 在什么情况下我会将操作指定为未选中?

c# - 更改 key 扩展 WPF 工具包 Zoombox

c# - 如何在UWP中的GridView中捕获Scroll事件

c# - 获取 TextBox 的 FontFamily 作为字符串 XAML C#

visual-studio - 使用 Xamarin 表单在 Visual Studio 中的 XAML 和代码之间切换