[英]Xamarin XAML: Type is not found from external assembly
I have a Visual Studio
solution, which has 2 C# projects:我有一个
Visual Studio
解决方案,它有 2 个 C# 项目:
a C# portable BasicLib
;一个 C# 可移植的
BasicLib
;
a C# portable XAML project
.一个 C# 可移植
XAML project
。
I defined resource files and a public LocalizedStrings
class in the BasicLib
.我在
BasicLib
定义了资源文件和一个公共LocalizedStrings
类。 Its namespace is " PL.Common.BasicLib
", the assembly name is " PL.Common.BasicLib.dll
".它的命名空间是“
PL.Common.BasicLib
”,程序集名称是“ PL.Common.BasicLib.dll
”。
LocalizedStrings.cs本地化字符串.cs
namespace PL.Common.BasicLib
{
/// <summary>
/// Provides access to string resources.
/// </summary>
public class LocalizedStrings
{
private static AppResources _localizedResources = new AppResources();
public AppResources LocalizedResources { get { return _localizedResources; } }
}
}
Then I add reference from the XAML project to this PL.Common.BasicLib
.然后我将来自 XAML 项目的引用添加到这个
PL.Common.BasicLib
。 So far so good.到现在为止还挺好。 I double click the assembly name from the XAML project's "
References
" node, I can see the Type " LocalizedStrings
" under the " PL.Common.BasicLib
" in the Visual Studio's " Object Browser
".我从 XAML 项目的“
References
”节点双击程序集名称,我可以在 Visual Studio 的“ Object Browser
”中的“ PL.Common.BasicLib
”下看到类型“ LocalizedStrings
”。
Now, I would like to put this resource into App.xaml
as application resource in this way:现在,我想以这种方式将此资源作为应用程序资源放入
App.xaml
中:
App.xaml :应用程序.xaml :
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:basiclib="clr-namespace:PL.Common.BasicLib;assembly=PL.Common.BasicLib"
x:Class="PL.Common.Xaml.App">
<Application.Resources>
<basiclib:LocalizedStrings x:Key="LocalizedStrings"/>
</Application.Resources>
</Application>
Build the solution, no problem.构建解决方案,没问题。 When running it, there will throw an error:
运行时会报错:
Xamarin.Forms.Xaml.XamlParseException: Position 8:6.
Type basiclib:LocalizedStrings not found in xmlns clr-namespace:PL.Common.BasicLib;assembly=PL.Common.BasicLib
Can someone help to take a look where is the bug?有人可以帮忙看看错误在哪里吗?
thanks!谢谢!
This could be related to a known linking issue - where Xamarin compiler ends up linking out classes (from external assemblies) that have references only in XAML.这可能与已知的链接问题有关 - Xamarin 编译器最终链接出仅在 XAML 中具有引用的类(来自外部程序集)。
There are couple of links that talk about this:有几个链接讨论这个:
There are a lot of options to resolve this:有很多选项可以解决这个问题:
Add a static Init method in each class as mentioned here in "Getting started" section here添加静态初始化方法在这里提到的每个类“入门”一节在这里
// this ensures the class does not get // linked out in the application we add this assembly to. public static void Init() { }
Or, preserve code using preserve attributes on Android , and iOS或者,在Android和iOS上使用保留属性保留代码
public class Example { [Android.Runtime.Preserve] public Example () { } }
Or, use Custom linking .或者,使用自定义链接。
Or, update project configuration to not link.或者,将项目配置更新为不链接。 Android , and iOS .
安卓和iOS 。 Not a recommended option though.
虽然不是推荐的选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.