在 XAML 通用 Windows 平台中向 {x:Bind} 添加额外的字符串

Adding additional string to {x:Bind} in XAML Universal Windows Platform

我是 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}" />

我想得到"E-Mail to email@email.com"

但是没有成功。感谢您的帮助。

为此创建转换器:

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();
    }
}

将此添加到您的 page/control/app 资源中:

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

然后像这样使用它:

<TextBlock Text="{x:Bind e_mail}" 
    Converter="{StaticResource StringFormatConverter}" 
    ConverterParameter="E-Mail To {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