简体   繁体   中英

Xamarin XAML: Type is not found from external assembly

I have a Visual Studio solution, which has 2 C# projects:

  1. a C# portable BasicLib ;

  2. a C# portable XAML project .

I defined resource files and a public LocalizedStrings class in the BasicLib . Its namespace is " PL.Common.BasicLib ", the assembly name is " PL.Common.BasicLib.dll ".

LocalizedStrings.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 . 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 ".

Now, I would like to put this resource into App.xaml as application resource in this way:

App.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.

There are couple of links that talk about this:

  1. Extending Control plugins > Getting Started
  2. Force assembly linking

There are a lot of options to resolve this:

  1. 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() { }
  2. Or, preserve code using preserve attributes on Android , and iOS

    public class Example { [Android.Runtime.Preserve] public Example () { } }
  3. Or, use Custom linking .

  4. Or, update project configuration to not link. Android , and iOS . Not a recommended option though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM