未找到Xaml标记扩展名

时间:2016-04-20 14:19:17

标签: xamarin.forms

在我的Xamarin Forms应用程序中,我尝试在我的一个xaml文件中设置嵌入式图像的来源,但在创建自定义命名空间时,我收到错误 Xamarin.Forms.Xaml.XamlParseException: Position 31:12. MarkupExtension not found for local:ImageResource。我基于https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images的官方文档,并检查了示例代码。我的程序集名称和默认名称空间都与xaml文件中的名称匹配。

我在Windows上使用Visual Studio 2015。我有其他人在Mac上试用Xamarin Studio上的代码,代码工作正常并显示图像。

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:App;assembly=App"
         x:Class="App.LoginPage">

 <RelativeLayout>

    <Label Text="Logo"
       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.3}" BackgroundColor="Aqua" />

    <StackLayout Spacing="20" Padding="20"
       VerticalOptions="Center"
       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.5}"
       RelativeLayout.XConstraint= "{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0}"
       RelativeLayout.YConstraint= "{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.3}">

      <Entry Placeholder="Username"
         Text="{Binding Username}"/>
      <Entry Placeholder="Password"
         Text="{Binding Password}"
         IsPassword="true"/>
      <Button Text="Login" TextColor="White"
          BackgroundColor="Blue"
          Command="{Binding LoginCommand}"/>

    </StackLayout>

    <Image Source="{local:ImageResource App.logo.png}"
       Aspect="AspectFill"
       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.4}"
       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.15}"
       RelativeLayout.XConstraint= "{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.6}"
       RelativeLayout.YConstraint= "{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.85}" />

 </RelativeLayout>

</ContentPage>

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:4)

由于没有从字符串到ResourceImageSource的内置类型转换器,因此Xaml无法本机加载这些类型的图像。

要解决此限制,可以使用Xaml中指定的资源ID编写一个简单的自定义Xaml标记扩展来加载图像。

[ContentProperty ("Source")]
public class ImageResourceExtension : IMarkupExtension
 { 
  public string Source { get; set; }

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

  // Do your translation lookup here, using whatever method you require
  var imageSource = ImageSource.FromResource(Source);

  return imageSource;
 }
}

要使用此扩展,请使用项目的正确命名空间和程序集值将自定义xmlns添加到Xaml。然后可以使用以下语法设置图像源:{local:ImageResource WorkingWithImages.beach.jpg}

Source

答案 1 :(得分:2)

Dakshal的回答应该是正确的,但是可能遗漏的一件事是标记扩展需要驻留在xmlns别名中指向的同一名称空间/程序集中。因此,请确保此代码存在于您的表单项目中:

namespace App
{
    [ContentProperty ("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }

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

            // Do your translation lookup here, using whatever method you require
            var imageSource = ImageSource.FromResource(Source);

            return imageSource;
        }
    }
}

命名空间(本例中为App)与XAML文件中指定的别名(本例中为xmlns:local="clr-namespace:App;assembly=App")相匹配非常重要。

文档没有指出这一点。它假定您知道将命名空间别名与放置扩展代码的CLR命名空间相关联。

答案 2 :(得分:0)

为什么不这样做:

element

这将在以下目录中查找图像:

Android:资源\ drawable

iOS:资源

UWP: basefolder

查看更多here (Working with images in Xamarin)

答案 3 :(得分:0)

只需在Markup类之前添加[ContentProperty("Source")]即可。像:

[ContentProperty("Source")] public class EmbeddedImage : IMarkupExtension { //................ }