xaml - XAML 资源字典中的算术运算

标签 xaml xamarin.forms resourcedictionary ivalueconverter

我想做的事
我最近一直在探索 XAML 资源字典。它们非常强大,但为了减少(甚至进一步)为适应任何修改而需要进行的更改,我想使用一些基本的算术运算来更改 HeightRequest Entry 的属性(property).
我已经很好地利用了OnPlatformOnIdiom对于不同的方面,如FontSize .
对于 iOS 平台,我想制作 HeightRequest条目 20+(FontSize) . FontSize已使用 OnIdiom 设置(平板电脑略有增加)。
在一个完美的世界里,我正在尝试做的核心事情可能看起来像<Setter Property="HeightRequest" Value="{DynamicResource StandardFontSize}+10">什么“有效”
如果我使用 OnIdiom 的组合,我有一个可行的解决方案和 OnPlatform .

<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinDesigner.App"
             xmlns:local="clr-namespace:XamarinDesigner"
             >
    <Application.Resources>
        <ResourceDictionary>
            <OnIdiom x:Key="StandardFontSize" x:TypeArguments="x:Double"  Tablet="22" Phone="18"/>
            <Style x:Key="MyEntry" TargetType="Entry">
                <Setter Property="FontSize" Value="{DynamicResource StandardFontSize}"/>
                <Setter Property="HeightRequest">
                    <Setter.Value>
                        <OnIdiom x:TypeArguments="x:Double">
                            <OnIdiom.Phone>
                                <OnPlatform x:TypeArguments="x:Double" iOS="30"/>
                            </OnIdiom.Phone>
                            <OnIdiom.Tablet>
                                <OnPlatform x:TypeArguments="x:Double" iOS="40"/>
                            </OnIdiom.Tablet>
                        </OnIdiom>
                    </Setter.Value>
                </Setter>
                <Setter Property="VerticalOptions" Value="Center"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>
使用这个“解决方案” - 我需要明确设置值并自己进行计算。虽然这可行,但我希望能够执行基本的算术运算来找到 FontSize 的值, 并添加一些数字。
我试过的
在我的另一次尝试中,I've found a converter并试图使其适应我的用例。虽然没有智能感知或构建/编译错误,但应用程序在打开后会立即崩溃。 ArithmeticConverter 的 .cs 文件可以在上面的链接中找到。
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinDesigner.App"
             xmlns:local="clr-namespace:XamarinDesigner"
             >
    <Application.Resources>
        <local:ArithmeticConverter x:Key="AScript"/>

        <ResourceDictionary>
            <OnIdiom x:Key="StandardFontSize" x:TypeArguments="x:Double"  Tablet="22" Phone="18"/>

            <Style x:Key="MyEntry" TargetType="Entry">
                <Setter Property="FontSize" Value="{DynamicResource StandardFontSize}"/>
                <Setter Property="HeightRequest" Value="{Binding Converter={StaticResource AScript},ConverterParameter=Int32.Parse(20+{DynamicResource StandardFontSize}}"/>
                <Setter Property="VerticalOptions" Value="Center"/>
            </Style>

        </ResourceDictionary>
    </Application.Resources>
</Application>
我不完全了解转换器的使用,{Binding} App.xaml 中的值内部对我来说也是新事物。查看转换器提供的示例,我认为我接近正确,可能只需要朝着正确的方向插入?

是否可以在 App.xaml 中执行这种基本的算术函数?单独(或使用转换器)?我希望尽可能多地包含这个文件。
我在搜索中找到的其他解决方案提到了 View 模型的使用,但这是一个“全局”更改,我想将其应用于每个平台/惯用语的每个条目,所以我看不出这种适应是如何工作的。
谢谢你的时间!

最佳答案

您的应用程序崩溃的原因之一是因为 Converter 在 ResourceDictionary 之外。

解决方案一

只有在分配了 BindingContext 时才应使用绑定(bind),因此您需要在 cs 文件中分配它。

应用程序.cs:

public App()
{
    InitializeComponent();
    BindingContext = new { EntryHeightRequest = 10 };
    MainPage = ...
}

应用程序.xaml:
<ResourceDictionary>
    <local:ArithmeticConverter x:Key="AScript"/>
    <OnIdiom x:Key="StandardFontSize" x:TypeArguments="x:Double"  Tablet="22" Phone="18"/>
    <Style x:Key="MyEntry" TargetType="Entry">
        <Setter Property="FontSize" Value="{DynamicResource StandardFontSize}" /> 
        <Setter Property="HeightRequest" Value="{Binding EntryHeightRequest, Converter={StaticResource AScript},ConverterParameter="{StaticResource StandardFontSize}"/>
        <Setter Property="VerticalOptions" Value="Center"/>
    </Style>
</ResourceDictionary>

算术转换器.cs:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if(value is int constant && parameter is OnIdiom<double> dynamicSize)
        return constant + dynamicSize.GetValue();
    return -1;
}

OnIdiomExtension:
    public static T GetValue<T>(this OnIdiom<T> idiom)
    {
        switch(Device.Idiom)
        {
            case TargetIdiom.Phone:
                return idiom.Phone;

            case TargetIdiom.Desktop:
                return idiom.Desktop;

            case TargetIdiom.Tablet:
                return idiom.Tablet;

            case TargetIdiom.TV:
                return idiom.TV;

            case TargetIdiom.Watch:
                return idiom.Watch;

            default:
                throw new NotSupportedException();
        }
    }

当心 :当我尝试时,BindingContext 被传递给 ResourceDictionary( but this post contradicts it, may be they changed? )

解决方案二

类似于 解决方案一但您可以在 HeightRequest 上使用 OnIdiom 并使用默认值代替设置 BindingContext。
<Setter Property="HeightRequest" Value="{OnIdiom Default=10, Converter={StaticResource AScript}, ConverterParameter={StaticResource StandardFontSize}}" />

关于xaml - XAML 资源字典中的算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52618845/

相关文章:

xamarin - $(AndroidSupportedAbis) 中的值 'armeabi' 无效。不再支持此 ABI。 Xamarin.Forms - VS2019

c# - 在 Xamarin.Forms 的 WebView 中通过 Javascript onClick 事件调用 C# 函数

wpf - 无法从引用的项目中合并 ResourceDictionary

xaml - winrt xaml合并资源

c# - 如何链接从磁盘外部加载但未包含在项目或程序集中的资源字典?

.net - 嵌入式 WebBrowser 控件拦截水平滚动事件

c# - 将 SelectedItems 绑定(bind)到 ObservableCollection 属性

C# FileNotFoundException 未被用户代码处理

c# - 更改 PlaceholderText 的文字颜色(Windows 8.1 App)

xamarin - 如何在 Xamarin.Forms 标签中添加换行符?