当 XAML 中的静态资源采用格式时,如何将文本添加到 StringFormat
How to add text to StringFormat ,when the format is taken by a static resource in XAML
我有一个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:d="http://xamarin.com/schemas/2014/forms/design" x:Class="eFatura.Tools.StringFormats">
<ContentPage.Resources>
<x:String x:Key="formatdatetimelong">ddMMMyy HH:mm:ss.fff tt</x:String>
<x:String x:Key="formattimewithoutseconds">{0:h:mm tt}</x:String>
<x:String x:Key="formatdatetimewithoutseconds">{0:M/d/yy h:mm tt}</x:String>
<x:String x:Key="formatdecimalzeroplaces">{0:0}</x:String>
<x:String x:Key="formatdecimaloneplaces">{0:0.0}</x:String>
<x:String x:Key="formatdecimaltwoplaces">{0:0.00}</x:String>
<x:String x:Key="formatdecimalthreeplaces">{0:0.000}</x:String>
<x:String x:Key="formatdecimalfourplaces">{0:0.0000}</x:String>
<x:String x:Key="formatphonenumberusa">{0:(###) ###-####}</x:String>
<x:String x:Key="formatcurrencyusa">{0:$#,##0.00;($#,##0.00);Zero}</x:String>
<x:String x:Key="formatpercent">{0:0%}</x:String>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Entry x:Name="_entry2" Text="{Binding Entry2 ,StringFormat={ StaticResource formatdecimalthreeplaces} }" Keyboard="Numeric" TextColor="Black" Placeholder="Db Config" PlaceholderColor="Black" />
<Entry x:Name="_entry4" Text="{Binding Entry4 ,StringFormat={StaticResource formatdecimalthreeplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Default" PlaceholderColor="Black" />
<Entry x:Name="_entry5" Text="{Binding Entry5 ,StringFormat={StaticResource formatdecimaltwoplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Default" PlaceholderColor="Black" />
<Entry x:Name="_entry6" Text="{Binding Entry5 ,StringFormat={StaticResource formatdecimaloneplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Default" PlaceholderColor="Black" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
后面的代码是这样的
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace eFatura.Tools
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class formatedText
{
public decimal? Entry1 { get; set; }
public decimal? Entry2 { get; set; }
public decimal? Entry3 { get; set; }
public decimal? Entry4 { get; set; }
public decimal? Entry5 { get; set; }
}
public partial class StringFormats : ContentPage
{
private formatedText testFormat = new formatedText()
{
Entry1 = new decimal(544616.545546),
Entry2 = new decimal(544616.545546),
Entry3 = new decimal(544616.545546),
Entry4 = new decimal(544616.545546),
Entry5 = new decimal(544616.545546)
};
public StringFormats()
{
InitializeComponent();
this.BindingContext = testFormat;
}
}
}
在 XAML 我想制作类似的东西:
Text="{Binding Entry5 ,StringFormat='some text { StaticResource formatdecimalthreeplaces}'}"
如何实现?
您可以创建自定义行为:
public class MyBehavior: Behavior<Entry>
{
public static readonly BindableProperty CustomTextProperty =
BindableProperty.Create("CustomText", typeof(string), typeof(MyBehavior), null);
public string CustomText
{
get { return (string)GetValue(CustomTextProperty); }
set { SetValue(CustomTextProperty, value); }
}
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.TextChanged += HandleTextChanged;
}
private void HandleTextChanged(object sender, TextChangedEventArgs e)
{
SetCustomText(sender as Entry);
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
}
private void SetCustomText(Entry entry)
{
entry.Text = $"{CustomText} {entry.Text}";
entry.TextChanged -= HandleTextChanged;
}
}
并按如下方式使用它:
<Entry x:Name="_entry2" Text="{Binding Entry2 ,StringFormat={StaticResource formatdecimaltwoplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Config" PlaceholderColor="Black" >
<Entry.Behaviors>
<local:MyBehavior CustomText="Example text"/>
</Entry.Behaviors>
</Entry>
我有一个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:d="http://xamarin.com/schemas/2014/forms/design" x:Class="eFatura.Tools.StringFormats">
<ContentPage.Resources>
<x:String x:Key="formatdatetimelong">ddMMMyy HH:mm:ss.fff tt</x:String>
<x:String x:Key="formattimewithoutseconds">{0:h:mm tt}</x:String>
<x:String x:Key="formatdatetimewithoutseconds">{0:M/d/yy h:mm tt}</x:String>
<x:String x:Key="formatdecimalzeroplaces">{0:0}</x:String>
<x:String x:Key="formatdecimaloneplaces">{0:0.0}</x:String>
<x:String x:Key="formatdecimaltwoplaces">{0:0.00}</x:String>
<x:String x:Key="formatdecimalthreeplaces">{0:0.000}</x:String>
<x:String x:Key="formatdecimalfourplaces">{0:0.0000}</x:String>
<x:String x:Key="formatphonenumberusa">{0:(###) ###-####}</x:String>
<x:String x:Key="formatcurrencyusa">{0:$#,##0.00;($#,##0.00);Zero}</x:String>
<x:String x:Key="formatpercent">{0:0%}</x:String>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Entry x:Name="_entry2" Text="{Binding Entry2 ,StringFormat={ StaticResource formatdecimalthreeplaces} }" Keyboard="Numeric" TextColor="Black" Placeholder="Db Config" PlaceholderColor="Black" />
<Entry x:Name="_entry4" Text="{Binding Entry4 ,StringFormat={StaticResource formatdecimalthreeplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Default" PlaceholderColor="Black" />
<Entry x:Name="_entry5" Text="{Binding Entry5 ,StringFormat={StaticResource formatdecimaltwoplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Default" PlaceholderColor="Black" />
<Entry x:Name="_entry6" Text="{Binding Entry5 ,StringFormat={StaticResource formatdecimaloneplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Default" PlaceholderColor="Black" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
后面的代码是这样的
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace eFatura.Tools
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public class formatedText
{
public decimal? Entry1 { get; set; }
public decimal? Entry2 { get; set; }
public decimal? Entry3 { get; set; }
public decimal? Entry4 { get; set; }
public decimal? Entry5 { get; set; }
}
public partial class StringFormats : ContentPage
{
private formatedText testFormat = new formatedText()
{
Entry1 = new decimal(544616.545546),
Entry2 = new decimal(544616.545546),
Entry3 = new decimal(544616.545546),
Entry4 = new decimal(544616.545546),
Entry5 = new decimal(544616.545546)
};
public StringFormats()
{
InitializeComponent();
this.BindingContext = testFormat;
}
}
}
在 XAML 我想制作类似的东西:
Text="{Binding Entry5 ,StringFormat='some text { StaticResource formatdecimalthreeplaces}'}"
如何实现?
您可以创建自定义行为:
public class MyBehavior: Behavior<Entry>
{
public static readonly BindableProperty CustomTextProperty =
BindableProperty.Create("CustomText", typeof(string), typeof(MyBehavior), null);
public string CustomText
{
get { return (string)GetValue(CustomTextProperty); }
set { SetValue(CustomTextProperty, value); }
}
protected override void OnAttachedTo(Entry bindable)
{
base.OnAttachedTo(bindable);
bindable.TextChanged += HandleTextChanged;
}
private void HandleTextChanged(object sender, TextChangedEventArgs e)
{
SetCustomText(sender as Entry);
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);
}
private void SetCustomText(Entry entry)
{
entry.Text = $"{CustomText} {entry.Text}";
entry.TextChanged -= HandleTextChanged;
}
}
并按如下方式使用它:
<Entry x:Name="_entry2" Text="{Binding Entry2 ,StringFormat={StaticResource formatdecimaltwoplaces}}" Keyboard="Numeric" TextColor="Black" Placeholder="Db Config" PlaceholderColor="Black" >
<Entry.Behaviors>
<local:MyBehavior CustomText="Example text"/>
</Entry.Behaviors>
</Entry>