【问题标题】:StaticResource into MarkupExtension静态资源到 MarkupExtension
【发布时间】:2017-08-17 23:15:11
【问题描述】:

我目前正在尝试为我的条目的FontSize 属性使用带有扩展名的静态资源。我有这段代码:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="PROJECT.Sources.Pages.Extras.EditProfilePage"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:control="clr-namespace:PROJECT.Sources.Controls;assembly=PROJECT"
    xmlns:extension="clr-namespace:PROJECT.Sources.Extensions;assembly=PROJECT"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:Key="NL_BlueNight">#0E1728</Color>
            <Color x:Key="NL_OrangeBeer">#E87E07</Color>
            <Color x:Key="NL_OrangeSky">#BD4327</Color>
            <Color x:Key="NL_White">#ECECEC</Color>
            <sys:Double x:Key="EntryFontSize">20</sys:Double>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>
        <AbsoluteLayout BackgroundColor="{x:StaticResource NL_BlueNight}">
            <AbsoluteLayout
                Margin="{Binding LayoutThicknessAdapter}"
                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                AbsoluteLayout.LayoutFlags="All"
                BackgroundColor="Black">
                <control:Gif
                    AbsoluteLayout.LayoutBounds="0.5, 0, 1, 0.9"
                    AbsoluteLayout.LayoutFlags="All"
                    GifSource="Gifs/LoginBackground.gif" />
                <BoxView
                    AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                    AbsoluteLayout.LayoutFlags="All"
                    BackgroundColor="Transparent" />
            </AbsoluteLayout>

            <AbsoluteLayout
                Margin="{Binding LayoutThicknessAdapter}"
                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                AbsoluteLayout.LayoutFlags="All"
                BackgroundColor="{StaticResource NL_BlueNight}"
                Opacity="0.8">
                <ScrollView AbsoluteLayout.LayoutBounds="0.5, 0, 1, 0.9" AbsoluteLayout.LayoutFlags="All">
                    <StackLayout
                        HorizontalOptions="Fill"
                        Orientation="Vertical"
                        Spacing="15"
                        VerticalOptions="CenterAndExpand">
                        <BoxView BackgroundColor="Transparent" HeightRequest="{Binding SeparatorHeight}" />
                        <control:CustomImageCircle
                            x:Name="UserProfileImageButton"
                            HeightRequest="{Binding PictureHeightWidth}"
                            HorizontalOptions="Center"
                            Source="{Binding CurrentUser.ImageProfile}"
                            VerticalOptions="Center"
                            WidthRequest="{Binding PictureHeightWidth}" />
                        <Image
                            HeightRequest="{Binding SeparatorHeight}"
                            HorizontalOptions="Center"
                            Source="{extension:ImageSource LogoPROJECT.png}" />
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                Placeholder="pseudo"
                                PlaceholderColor="Gray"
                                Text="{Binding CurrentUser.Pseudo}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                Placeholder="email"
                                PlaceholderColor="Gray"
                                Text="{Binding CurrentUser.Email}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                Placeholder="firstname"
                                PlaceholderColor="Gray"
                                Text="{Binding CurrentUser.Firstname}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                Placeholder="lastname"
                                PlaceholderColor="Gray"
                                Text="{Binding CurrentUser.Lastname}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                Keyboard="Telephone"
                                Placeholder="phone number"
                                PlaceholderColor="Gray"
                                Text="{Binding CurrentUser.Number}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                IsPassword="True"
                                Placeholder="password"
                                PlaceholderColor="Gray"
                                Text="{Binding PasswordOne}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                        <AbsoluteLayout HeightRequest="{Binding EntryHeight}" WidthRequest="{Binding EntryWidth}">
                            <control:CustomEntry
                                AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                                AbsoluteLayout.LayoutFlags="All"
                                BackgroundColor="Transparent"
                                FontFamily="{extension:FontFamily Roboto_Light}"
                                FontSize="{extention:FontSize StaticResourceKey=EntryFontSize}}"
                                HasBorder="false"
                                IsPassword="True"
                                Placeholder="password (retype)"
                                PlaceholderColor="Gray"
                                Text="{Binding PasswordTwo}"
                                TextColor="White"
                                XAlign="Center" />
                            <!--<BoxView
                                AbsoluteLayout.LayoutBounds="0.5, 0.7, 0.8, 1"
                                AbsoluteLayout.LayoutFlags="XProportional, YProportional, WidthProportional"
                                BackgroundColor="{StaticResource NL_OrangeBeer}" />-->
                        </AbsoluteLayout>
                    </StackLayout>
                </ScrollView>
                <AbsoluteLayout
                    AbsoluteLayout.LayoutBounds="0.5,1,1,0.1"
                    AbsoluteLayout.LayoutFlags="All"
                    BackgroundColor="{StaticResource NL_OrangeBeer}">
                    <control:CustomLabel
                        AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                        AbsoluteLayout.LayoutFlags="All"
                        FontFamily="{extension:FontFamily Roboto_Light}"
                        FontSize="35"
                        HorizontalTextAlignment="Center"
                        Text="Save and Return"
                        TextColor="White"
                        VerticalTextAlignment="Center" />
                    <control:CustomButton
                        AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                        AbsoluteLayout.LayoutFlags="All"
                        BackgroundColor="Transparent"
                        BorderColor="Transparent"
                        Clicked="OnSaveClicked" />
                </AbsoluteLayout>
                <AbsoluteLayout
                    AbsoluteLayout.LayoutBounds="0, 0, 0.1, 0.1"
                    AbsoluteLayout.LayoutFlags="All"
                    BackgroundColor="Transparent"
                    IsVisible="{Binding IsReturnVisible}">
                    <control:CustomImage
                        AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.8, 0.8"
                        AbsoluteLayout.LayoutFlags="All"
                        Aspect="AspectFit"
                        Source="{extension:ImageSource cross.png}" />
                    <control:CustomButton
                        AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
                        AbsoluteLayout.LayoutFlags="All"
                        BackgroundColor="Transparent"
                        BorderColor="Transparent"
                        Clicked="OnReturnClicked" />
                </AbsoluteLayout>
            </AbsoluteLayout>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

extension:FontSize 来自哪里:

[ContentProperty("FontSize")]
public class FontSizeExtension : IMarkupExtension
{
    public double FontSize { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return Services.Sizing.FontSizeAdapter(FontSize);
    }
}

Sizing.cs

public class Sizing
{
    public static double FontSizeAdapter(double fontSize)
    {
        switch (Device.RuntimePlatform)
        {
            case "Android":
                return (fontSize / 2);
            case "iOS":
                return fontSize;
            case "Windows":
            case "WinPhone":
                return fontSize;
            default:
                return fontSize;
        }
    }
}

但是,当我执行FontSize="{extention:FontSize {x:StaticResource EntryFontSize}}" 时,它会抛出一个异常,指出该值不能为空。我如何同时使用两者?我的意思是 x:StaticResourceExtension

谢谢!

【问题讨论】:

    标签: xaml xamarin.forms staticresource markup-extensions


    【解决方案1】:

    编辑 - 08/18

    1.确保正确定义和使用前缀

    确保检查为前缀提供的命名空间对于您的标记扩展是否正确,并且在指定前缀时没有拼写错误。这应该可以解决“值不能为空”错误。

    2。使用嵌套标记扩展时指定属性名称

    使用嵌套标记扩展时需要指定扩展的属性名称。否则,我的测试显示,它被视为字符串值并分配给默认内容属性。这应该可以解决错误“输入字符串的格式不正确”。


    Solution-1:指定属性名

    <control:CustomEntry
           AbsoluteLayout.LayoutBounds="0.5, 0.5, 1, 1"
           AbsoluteLayout.Flags="All"
       ...
           FontSize="{extension:FontSize FontSize={x:StaticResource EntryFontSize}}"
       .../>
    

    Solution-2:您可以通过在标记扩展中添加StaticResourceKey 属性来进一步简化:

    [ContentProperty("FontSize")]
    public class FontSizeExtension : IMarkupExtension
    {
        public double FontSize { get; set; }
        public string StaticResourceKey { get; set; }
    
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
                throw new ArgumentNullException(nameof(serviceProvider));
    
            if (StaticResourceKey != null)
            {
                var staticResourceExtension = new StaticResourceExtension { Key = StaticResourceKey };
                FontSize = (double)staticResourceExtension.ProvideValue(serviceProvider);
            }
    
            return Services.Sizing.FontSizeAdapter(FontSize);
        }
    }
    

    用法可以是:

       ...
           FontSize="{extension:FontSize StaticResourceKey=EntryFontSize}}"
       ...
    

    【讨论】:

    • 谢谢。 FontSize="{extention:FontSize FontSize={x:StaticResource EntryFontSize}}" 仍在抛出“值不能为空”。我会尝试第二种解决方案!
    • 您在运行时或编译时看到该错误吗?因为我也测试了第一个解决方案 - 它按预期工作!
    • 太蠢了...看看我的问题和你的回答...我的意思是extension..的拼写。
    • 开枪!解决了这个问题。
    • 是的!现在可以了,谢谢,但是您应该使用正确的解决方案编辑主要答案,以便社区成员更容易阅读:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2013-08-01
    • 2014-08-29
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多