C# Xamarin.Forms.Xaml.XamlParseException:未找到在发布模式下使用PCL进行trans:Translate的MarkupExtension

C# Xamarin.Forms.Xaml.XamlParseException:未找到在发布模式下使用PCL进行trans:Translate的MarkupExtension,c#,android,xamarin,xamarin.forms,C#,Android,Xamarin,Xamarin.forms,我非常迫切地想找到下面所述问题的解决方案 我有一个跨平台的解决方案,它有4个项目 3操作系统详细信息 MyApp.android, MyApp.Winphone, MyApp.IOS 和PCL项目 MyApp.BLL 在MyApp.BLLbll中,我使用的是MyApp名称空间,在根目录中有TranslateExtension.cs类 namespace MyApp { [ContentProperty("Text")] public class TranslateExtensio

我非常迫切地想找到下面所述问题的解决方案

我有一个跨平台的解决方案,它有4个项目

3操作系统详细信息
MyApp.android,
MyApp.Winphone,
MyApp.IOS
和PCL项目
MyApp.BLL

在MyApp.BLLbll中,我使用的是MyApp名称空间,在根目录中有TranslateExtension.cs类

namespace MyApp
{
    [ContentProperty("Text")]
    public class TranslateExtension: IMarkupExtension

{
    readonly CultureInfo ci;
    const string ResourceId = "MyApp.Resources.AppResource";

    public TranslateExtension()
    {
        ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
    }

    public string Text { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Text == null)
            return "";

        ResourceManager resmgr = new ResourceManager(ResourceId
                            , typeof(TranslateExtension).GetTypeInfo().Assembly);

        var translation = resmgr.GetString(Text, ci);
        try
        {
        if (translation == null)
            {
#if DEBUG
                throw new ArgumentException(
                    String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name),
                    "Text");
#else
                translation = Text; // HACK: returns the key, which GETS DISPLAYED TO THE USER
#endif
            }
        }
        catch (Exception exp) {
        }
        return translation;
    }
}
名称空间MyApp
{
[内容属性(“文本”)]
公共类TranslateExtension:IMarkupExtension
{
只读文化信息ci;
const string ResourceId=“MyApp.Resources.AppResource”;
公共TranslateExtension()
{
ci=DependencyService.Get().GetCurrentCultureInfo();
}
公共字符串文本{get;set;}
公共对象ProviderValue(IServiceProvider服务提供者)
{
if(Text==null)
返回“”;
ResourceManager resmgr=新的ResourceManager(ResourceId
,typeof(TranslateExtension).GetTypeInfo().Assembly);
var translation=resmgr.GetString(文本,ci);
尝试
{
if(translation==null)
{
#如果调试
抛出新的ArgumentException(
String.Format(“在区域性“{2}”的资源“{1}”中找不到键“{0}”)、Text、ResourceId、ci.Name),
“文本”);
#否则
translation=Text;//HACK:返回键,该键将显示给用户
#恩迪夫
}
}
捕获(异常扩展){
}
返回翻译;
}
}
在Xamarin表单中,我使用以下内容来启用标签的翻译,等等

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Views.RegisterWebServiceXaml"
             xmlns:trans="clr-namespace:MyApp;assembly=MyApp.BLL"
             IsBusy="{Binding IsWorking}"
             >

  <ContentPage.Content>
    <StackLayout Orientation="Vertical" VerticalOptions="FillAndExpand" Padding="0,75,0,0">
          <Entry Text="{Binding Path=OrganizationId, Mode=TwoWay}" Placeholder="{trans:Translate OrganizationId}" Style="{StaticResource LoginStyle}" />
          ...
          <Label Text="{trans:TranslateUpper Next}" x:Name="LoginLabel" HorizontalOptions="Center" Style="{StaticResource BasicLabel}" />
    </StackLayout>
  </ContentPage.Content>
</ContentPage>

...
使用debug modus,这就像一个魔咒,但当我试图在发布模式下编译Android解决方案时,我的应用程序在加载splashscreen声明“MyApp已停止”后立即停止

查看应用程序输出,我可以找到以下错误消息:

[Xamarin.Insights] Warning: Unhandled exception: Xamarin.Forms.Xaml.XamlParseException: MarkupExtension not found for trans:Translate
[Xamarin.Insights] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>
[Xamarin.Insights] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <0x0003f>
[Xamarin.Insights] at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () <0x00027>
[Xamarin.Insights] at Java.Lang.Thread/RunnableImplementor.Run () <0x0003f>
[Xamarin.Insights] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <0x0003b>
[Xamarin.Insights] at (wrapper dynamic-method) object.44dc0140-8f03-4bf1-a261-186aa2a7ef42 (intptr,intptr) <0x0003b>
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: MyApp.MyApp, PID: 32091
[AndroidRuntime] java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
[AndroidRuntime]    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
[AndroidRuntime] Caused by: java.lang.reflect.InvocationTargetException
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime]    at java.lang.reflect.Method.invoke(Method.java:372)
[AndroidRuntime]    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
[AndroidRuntime]    ... 1 more
[AndroidRuntime] Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: Xamarin.Forms.Xaml.XamlParseException: MarkupExtension not found for trans:Translate
[AndroidRuntime] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>
[AndroidRuntime] at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) <0x0003f>
[AndroidRuntime] at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () <0x00027>
[AndroidRuntime] at Java.Lang.Thread/RunnableImplementor.Run () <0x0003f>
[AndroidRuntime] at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <0x0003b>
[AndroidRuntime] at (wrapper dynamic-method) object.44dc0140-8f03-4bf1-a261-186aa2a7ef42 (intptr,intptr) <0x0003b>
[AndroidRuntime] 
[AndroidRuntime]    at mono.java.lang.RunnableImplementor.n_run(Native Method)
[AndroidRuntime]    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
[AndroidRuntime]    at android.os.Handler.handleCallback(Handler.java:739)
[AndroidRuntime]    at android.os.Handler.dispatchMessage(Handler.java:95)
[AndroidRuntime]    at android.os.Looper.loop(Looper.java:145)
[AndroidRuntime]    at android.app.ActivityThread.main(ActivityThread.java:5944)
[AndroidRuntime]    ... 4 more
[Xamarin.Insights]警告:未处理的异常:Xamarin.Forms.Xaml.XamlParseException:找不到trans:Translate的MarkupExtension
[Xamarin.Insights]位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
[Xamarin.Insights]位于System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0(对象)
[Xamarin.Insights]位于Android.App.SyncContext/c_uanostorey0.m_u0()
[Xamarin.Insights]位于Java.Lang.Thread/RunnableImplementor.Run()
[Xamarin.Insights]在Java.Lang.IRunnableInvoker.n_运行(intptr,intptr)
[Xamarin.Insights]at(包装器动态方法)object.44dc0140-8f03-4bf1-a261-186aa2a7ef42(intptr,intptr)
[art]JNI RegisterNativeMethods:尝试为md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable注册0个本机方法
[AndroidRuntime]正在关闭VM
[AndroidRuntime]致命异常:主
[AndroidRuntime]进程:MyApp.MyApp,PID:32091
[AndroidRuntime]java.lang.RuntimeException:java.lang.reflect.InvocationTargetException
[AndroidRuntime]位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
[AndroidRuntime]由以下原因引起:java.lang.reflect.InvocationTargetException
[AndroidRuntime]位于java.lang.reflect.Method.invoke(本机方法)
[AndroidRuntime]位于java.lang.reflect.Method.invoke(Method.java:372)
[AndroidRuntime]位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
[AndroidRuntime]…还有1个
[AndroidRuntime]由以下原因引起:md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable:Xamarin.Forms.Xaml.XamlParseException:找不到trans:Translate的MarkupExtension
[AndroidRuntime]位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
[AndroidRuntime]位于System.Runtime.CompilerServices.AsyncMethodBuilderCore.m_u0(对象)
[AndroidRuntime]位于Android.App.SyncContext/c_uanonnstorey0.m_u0()
[AndroidRuntime]位于Java.Lang.Thread/RunnableImplementor.Run()
[AndroidRuntime]在Java.Lang.irunableInvoker.n_运行(intptr,intptr)
[AndroidRuntime]at(包装器动态方法)object.44dc0140-8f03-4bf1-a261-186aa2a7ef42(intptr,intptr)
[AndroidRuntime]
[AndroidRuntime]位于mono.java.lang.RunnableImplementor.n_run(本机方法)
[AndroidRuntime]位于mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
[AndroidRuntime]位于android.os.Handler.handleCallback(Handler.java:739)
[AndroidRuntime]位于android.os.Handler.dispatchMessage(Handler.java:95)
[AndroidRuntime]位于android.os.Looper.loop(Looper.java:145)
[AndroidRuntime]位于android.app.ActivityThread.main(ActivityThread.java:5944)
[AndroidRuntime]…还有4个
有人能解决这个问题吗?我找不到。我试过使用几个名称空间(MyApp.android、MyApp、MyApp.BLL、MyApp.MyApp),但直到现在我还没有找到正确的名称空间


为什么它在调试模式下工作,而不是在发布/应用商店模式下工作?

检查项目设置,确保关闭链接或SDK ony

如果它是“链接所有程序集”,如果没有它,您的代码将变得太大,您可能需要在类上使用
[Preserve]
属性


有关Xamarin链接器的更多信息,请参阅

谢谢,这是我问题的解决方案。我切换到“不链接”,所有内容都在发布模式下工作。我尝试推广你的答案,但我没有足够的积分来做这件事。