Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当XAML中的静态资源采用StringFormat格式时,如何向StringFormat添加文本_Xaml_Xamarin_String Formatting_Staticresource - Fatal编程技术网

当XAML中的静态资源采用StringFormat格式时,如何向StringFormat添加文本

当XAML中的静态资源采用StringFormat格式时,如何向StringFormat添加文本,xaml,xamarin,string-formatting,staticresource,Xaml,Xamarin,String Formatting,Staticresource,我有一个像这样的xaml ddMMMyy HH:mm:ss.fff tt {0:h:mm tt} {0:M/d/yy h:mm tt} {0:0} {0:0.0} {0:0.00} {0:0.000} {0:0.0000} {0:(###) ###-####} {0:$#,0.00;($#,0.00);零} {0:0%} 后面的代码是这样的 使用Xamarin.Forms; 使用Xamarin.Forms.Xaml; 命名空间eFatura.Tools { [XamlCompilation(

我有一个像这样的xaml


ddMMMyy HH:mm:ss.fff tt
{0:h:mm tt}
{0:M/d/yy h:mm tt}
{0:0}
{0:0.0}
{0:0.00}
{0:0.000}
{0:0.0000}
{0:(###) ###-####}
{0:$#,0.00;($#,0.00);零}
{0:0%}
后面的代码是这样的

使用Xamarin.Forms;
使用Xamarin.Forms.Xaml;
命名空间eFatura.Tools
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共类格式化文本
{
公共十进制?Entry1{get;set;}
公共十进制?Entry2{get;set;}
公共十进制?Entry3{get;set;}
公共十进制?Entry4{get;set;}
公共十进制?Entry5{get;set;}
}
公共部分类StringFormats:ContentPage
{
私有FormattedText testFormat=新FormattedText()
{
Entry1=新的十进制数(544616.545546),
Entry2=新的十进制数(544616.545546),
Entry3=新的十进制数(544616.545546),
Entry4=新的十进制数(544616.545546),
Entry5=新的十进制数(544616.545546)
};
公共字符串格式()
{
初始化组件();
this.BindingContext=testFormat;
}
}
}
在XAML中,我想制作如下内容:

Text=“{Binding Entry5,StringFormat='some Text{StaticResource formatdecimalthreplaces}”

如何实现这一点?

您可以创建自定义行为:

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;
    }
}
公共类MyBehavior:Behavior
{
公共静态只读BindableProperty CustomTextProperty=
Create(“CustomText”、typeof(string)、typeof(MyBehavior)、null);
公共字符串自定义文本
{
get{return(string)GetValue(CustomTextProperty);}
set{SetValue(CustomTextProperty,value);}
}
受保护的覆盖无效数据到(条目可绑定)
{
碱。可粘合的DTO(可粘合);
bindable.TextChanged+=HandleTextChanged;
}
私有void HandleTextChanged(对象发送方,textchangedventargs e)
{
SetCustomText(发送者作为条目);
}
受保护的重写无效OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
base.OnPropertyChanged(propertyName);
}
私有void SetCustomText(条目)
{
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>

谢谢你解决了我的问题:)