【问题标题】:Markup extension not found未找到标记扩展
【发布时间】:2019-01-28 19:13:08
【问题描述】:

我正在研究 microsoft 页面中的文档,因为我愿意学习 xamarin.forms。 每当引入一个新主题时,我都会尝试编写一个小应用程序来测试我刚刚学到的东西。 在尝试使用 xaml 标记扩展时,我发现 microsoft 文档中显示的示例之一对我不起作用,即使我从网站复制它并将其粘贴到我的 vs 项目中也是如此。

C#代码是这样的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace XamarinLab
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }

    public class HslColorExtension : IMarkupExtension<Color>
    {
        public double H { set; get; }

        public double S { set; get; }

        public double L { set; get; }

        public double A { set; get; } = 1.0;

        public Color ProvideValue(IServiceProvider serviceProvider)
        {
            return Color.FromHsla(H, S, L, A);
        }

        object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
        {
            return (this as IMarkupExtension<Color>).ProvideValue(serviceProvider);
        }
    }
}

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:XamarinLab"
             x:Class="XamarinLab.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="BoxView">
                <Setter Property="WidthRequest" Value="80" />
                <Setter Property="HeightRequest" Value="80" />
                <Setter Property="HorizontalOptions" Value="Center" />
                <Setter Property="VerticalOptions" Value="CenterAndExpand" />
            </Style>
        </ResourceDictionary>
    </ContentPage.Resources>

    <StackLayout>
        <BoxView>
            <BoxView.Color>
                <local:HslColorExtension H="0" S="1" L="0.5" A="1" />
            </BoxView.Color>
        </BoxView>

        <BoxView>
            <BoxView.Color>
                <local:HslColor H="0.33" S="1" L="0.5" />
            </BoxView.Color>
        </BoxView>

        <BoxView Color="{local:HslColorExtension H=0.67, S=1, L=0.5}" />

        <BoxView Color="{local:HslColor H=0, S=0, L=0.5}" />

        <BoxView Color="{local:HslColor A=0.5}" />
    </StackLayout>

 </ContentPage>

当我尝试运行应用程序时,会弹出以下异常

markup extension not found for local:HslColorExtension

我复制的示例位于:https://docs.microsoft.com/it-it/xamarin/xamarin-forms/xaml/markup-extensions/creating

这是第一个示例,在此先感谢您的帮助。

【问题讨论】:

    标签: c# xamarin.forms markup-extensions


    【解决方案1】:

    您需要在项目中创建一个名为 HslColorExtension 的新文件类,而不是在 MainPage 文件中创建一个类,因为它无法识别命名空间或创建的类。

    只需将该部分移动到一个新文件中,您就应该完成所有设置。

    您可以查看Offical Sample,查看结构、文件以及如何设置。

    【讨论】:

    • 感谢您的快速回答,但我发现问题实际上是我用来在智能手机上测试的 xamarin 实时播放器应用程序,当我切换到模拟器时它工作得很好,没有移动任何东西.我猜模拟器还没有完全准备好。
    猜你喜欢
    • 2016-08-13
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2018-07-11
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多