gpt4 book ai didi

c# - XAML 转换器如何做更多事情?

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:43 25 4
gpt4 key购买 nike

在 angular 中可以对文本进行一些过滤,例如:

<div ng-bind="vm.value | filter1| filter2| filter3"

并且当序列被维护时。这是如何在 XAML 中完成的?特别是 xamarin。

最佳答案

这是通过使用 Xamarin Forms 中的转换器完成的:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamlSamples.SharedResourcesPage"
Title="Shared Resources Page">
<ContentPage.Resources>
<ResourceDictionary>
<local:DoubleToIntConverter x:Key="intConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Label Text="{Binding Color.R,
Converter={StaticResource intConverter},
ConverterParameter=255,
StringFormat='R={0:X2}'}" />
</StackLayout>
</ContentPage>

转换器:

class DoubleToIntConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
double multiplier;

if (!Double.TryParse(parameter as string, out multiplier))
multiplier = 1;

return (int)Math.Round(multiplier * (double)value);
}

public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
double divider;

if (!Double.TryParse(parameter as string, out divider))
divider = 1;

return ((double)(int)value) / divider;
}
}

更多信息在这里:https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_binding_basics/

关于c# - XAML 转换器如何做更多事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37189516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com