【问题标题】:Xamarin Form's Resource dictionary type not found in namespace xmlns when debugging using iPhone使用 iPhone 进行调试时,在命名空间 xmlns 中找不到 Xamarin Forms Resourcedictionary 类型
【发布时间】:2017-01-30 18:49:52
【问题描述】:

我的解决方案包括 3 个项目:

  1. 我的后端项目与程序集 My_Test_App(便携式)

  2. My_Test_App.Android

  3. My_Test_App.iOS

在后端项目中,我有这个 XAML 页面代码(请原谅名称)

<ContentPage
    x:Class="My_Test_App.Pages.LoginPage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:converters="clr-namespace:My_Test_App.Converters;assembly=My_Test_App"
    xmlns:effects="clr-namespace:My_Test_App.Effects;assembly=My_Test_App"
    xmlns:viewModels="clr-namespace:My_Test_App.ViewModels;assembly=My_Test_App"
    xmlns:views="clr-namespace:My_Test_App.Views;assembly=My_Test_App">
    <ContentPage.Resources>
        <ResourceDictionary>
            <converters:Converter1 x:Key="conv1" />
            <converters:Converter2 x:Key="conv2" />
            <converters:Converter3 x:Key="conv3" />
        </ResourceDictionary>
    </ContentPage.Resources>
</ContentPage>

它适用于 android 和 iPhone 模拟器,但是当我在真正的 iPhone 上测试它时,我得到了这个错误: Xamarin.Forms.Xaml.XamlParseException: Position 13:14. Type converters:Converter1 not found in xmlns clr-namespace:My_Test_App.Converters;assembly=My_Test_App

我在后端项目中的 Converter1 代码:

namespace My_Test_App.Converters
{
    public class Converter1: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool original = (bool)value;
            return !original;
        }       
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public My_Test_App()
        {
        }
    }
}

你能帮忙吗?我这里有几个嫌疑人:

  1. 程序集名称下划线,但我需要保留当前程序集名称..

  2. 在 IOS 项目属性中,我将 iOS 构建部分中的链接器选项从“仅链接 SDK”更改为“链接所有程序集”。但是,如果我不更改它,我会收到错误“Could not AOT the assembly.......”

  3. 当前 xamarin 版本中可能存在的错误(我的是 4.2.2.11)

谢谢你帮助我!

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    类型转换器:找不到Converter1

    Xamarin 链接器使用静态分析来确定可以从程序集中删除哪些IL 代码以减小大小,并且由于来自Xamarin.Form 的反射调用使用,基于IValueConverter 的类似乎不被使用。

    在您的 Xamarin.Forms (PCL) 项目中,添加 PreserveAttribute 类:

    public sealed class PreserveAttribute : System.Attribute {
        public bool AllMembers;
        public bool Conditional;
    }
    

    现在将带有 AllMembers 属性的 [Preserve] 添加到您的 IValueConverter 类中,以通知链接器跳过该类:

    [Preserve(AllMembers = true)]
    public class Converter1: IValueConverter
    {
      ~~~
    }
    

    回复:https://developer.xamarin.com/guides/ios/advanced_topics/linker/

    【讨论】:

    • 谢谢,保留属性有效。但是我不确定不使用 IValueConverter 意味着什么,因为我需要实现该接口(这意味着它正在被使用吗?)
    • @prinnny 谢谢,措辞不正确/糟糕。我将其更新为 the IValueConverter-based class appears not to be used. 在您的情况下,Converter1 没有 direct 代码引用,因为它是通过反射调用而不是直接方法调用(链接器会看到) .需要注意的一点是,使用 XAMLC 预编译大部分 XAML 有助于避免 一些 这些链接器问题,因为将进行直接代码引用:developer.xamarin.com/guides/xamarin-forms/xaml/xamlc
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多