我正在用Radzen组件在blazorframework中编程,这是一个带有注册页面的Web应用程序. 我需要从用户的电话号码与他的国家代码.为了 Select 国家代码,我有一个带有国家缩写("DE|US|AC")的下拉菜单,我希望在它的左侧有适当的标志. 有没有办法做到这一点.

Look at my Dropdown Menu

This is the HTML Part of the Dropdown

         <RadzenDropDown Data=@CountryCodes style="width:80px !important; top:9px;"
            AllowFiltering="true"
            FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
            FilterOperator="StringFilterOperator.StartsWith"
            TValue="string"
            Class="w-100"
            @bind-Value=@_value>
         <RadzenDropDown/>

我在下拉列表中填满了Nuget"libPhoneumbers"

    public HashSet<string> CountryCodes => PhoneNumberUtil.GetInstance().GetSupportedRegions();

推荐答案

这是一个使用flag emojis的解决方案.您需要一种将国家代码转换为国旗表情符号的方法.您还需要使用支持旗帜表情符号的字体,例如Noto Color Emoji来制作旗帜appear on windows.在下面的示例中,我使用的是一个简单的select元素,但您可以很容易地将其修改为RadzenDropDown.

<select @bind="_selectedCountryCode">
    <option disabled>Select country code</option>
    @foreach(var country in Countries)
    {
        <option value="@country.CountryCode">@country.FlagEmoji</option>
    }
</select>

<p>Selected country code: @_selectedCountryCode</p>

@code {
    private string _selectedCountryCode;
    private HashSet<string> CountryCodes = new HashSet<string> { "DE", "US", "AC" };

    private List<Country> Countries => CountryCodes
        .Select(x => new Country
        {
            CountryCode = x,
            FlagEmoji = IsoCountryCodeToFlagEmoji(x)
        })
        .ToList();

    private string IsoCountryCodeToFlagEmoji(string countryCode) =>
        string.Concat(countryCode.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));

    public class Country
    {
        public string CountryCode { get; set; }
        public string FlagEmoji { get; set; }      
    }
}

BlazorFiddle

How to convert country name to flag emoji article

100 example:

<RadzenDropDown Data="@Countries"
                style="width: 80px !important; top: 9px;"
                AllowFiltering="true"
                FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                FilterOperator="StringFilterOperator.StartsWith"
                TValue="string"
                TextProperty="@nameof(Country.FlagEmoji)"
                ValueProperty="@nameof(Country.CountryCode)"
                Class="w-100"
                @bind-Value=@_selectedCountryCode>
</RadzenDropDown>

Csharp相关问答推荐

下拉图片点击MudBlazor

为什么我不能更改尚未设置的模拟对象属性的值?

ASP.NET MVC中创建视图的过滤器

安装附加的. exe与Visual Studio

如何删除文件的基础上嵌入的时间戳嵌入文件名

REST API端点中异步后台代码执行的类型

在命名管道上使用GRPC ASP.NET核心时如何配置命名管道权限

如何从ASP.NET核心MVC视图和Blazor传递数据

JSON空引用异常仅在调试器中忽略try-Catch块,但在其他上下文中捕获得很好

在此系统上已禁用获取正在运行的脚本.&在ASP.NET Core Web API中

如何注册类使用多级继承与接口

在EF Core中,有没有什么方法可以防止在查询中写入相同的条件,而代之以表达式?

EF核心新验证属性`DeniedValues`和`StringCompison`不起作用

GODOT 4向C#中的字符串参数发送信号以等待

是否可以将Collectionview中的数组与ObservableCollection绑定?

将FileStream的特定部分作为字节数组读取

ReadOnlyMemory访问基础索引的替代方案

为什么我的自定义Json.net转换器不工作?

Unity 3D-意外轴捕捉和未知力对脉冲react 行为的影响

在Swagger中显示自定义属性的属性名称