在XAML通用Windows平台中向{x:Bind}添加其他字符串

时间:2016-01-31 17:26:22

标签: xaml windows-store-apps uwp

我是XAML的新手。我想在x:bind

中添加额外的字符串

我试过了

<AppBarToggleButton Icon="Phone" Label="E-Mail To {x:Bind  e_mail}" />
<AppBarToggleButton Icon="Phone" Label="{"E-Mail To" + x:Bind  e_mail}" />

我想收到“发送电子邮件至email@email.com”

但没有成功。谢谢你的帮助。

2 个答案:

答案 0 :(得分:4)

为此创建转换器:

public sealed class StringFormatConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (value == null)
            return null;

        if (parameter == null)
            return value;

        return string.Format(parameter.ToString(), value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
        string language)
    {
        throw new NotImplementedException();
    }
}

将此添加到您的页面/控制/应用资源:

<converters:StringFormatConverter x:Key="StringFormatConverter" />

然后像这样使用它:

<TextBlock Text="{x:Bind e_mail}" 
    Converter="{StaticResource StringFormatConverter}" 
    ConverterParameter="E-Mail To {0}!" />

答案 1 :(得分:0)

您可以使用 builtin converter from Microsoft 代替创建自己的 StringFormatConverter

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converter="using:Microsoft.Toolkit.Uwp.UI.Converters"
    mc:Ignorable="d">
    <converter:StringFormatConverter x:Key="StringFormatConverter"/>
</ResourceDictionary>

还有一些其他有用的内置转换器,请查看: https://docs.microsoft.com/en-us/dotnet/api/microsoft.toolkit.uwp.ui.converters?view=win-comm-toolkit-dotnet-7.0