在我的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>
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
App命名空间(在本例中)与 XAML 文件(在本例中)中指定的别名相匹配非常重要xmlns:local="clr-namespace:App;assembly=App"。
文档没有指出这一点。它假定您知道将命名空间别名与放置扩展代码的 CLR 命名空间相关联。
由于没有从字符串到ResourceImageSource的内置类型转换器,因此Xaml无法本地加载这些类型的图像。
为了解决此限制,可以编写一个简单的自定义Xaml标记扩展名,以使用Xaml中指定的资源ID加载图像。
[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;
}
}
Run Code Online (Sandbox Code Playgroud)
要使用此扩展,请使用项目的正确名称空间和程序集值将自定义xmlns添加到Xaml。然后可以使用以下语法设置图像源:{local:ImageResource WorkingWithImages.beach.jpg}
| 归档时间: |
|
| 查看次数: |
6579 次 |
| 最近记录: |