首页 > 解决方案 > Xamarin Forms - 在运行时翻译抽屉菜单项

问题描述

我正在关注 Xamarin.Forms 中多语言的优秀教程(链接

一切都运行良好,但我有一个问题。

在我的应用程序中,我正在使用 Syncfusion 的导航抽屉,因为我在 ListView 中生成菜单项,如下所示。

DrawerPage.xaml

<ListView x:Name="listView"
                  SelectionMode="Single"
                  RowHeight="70"
                  ItemSelected="listView_ItemSelected"
                  SeparatorColor="Transparent">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="32,10,0,0" 
                                         VerticalOptions="Center" >

                                <Grid RowDefinitions="1*, Auto">

                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="50"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>

                                    <Label Grid.Row="0"
                                           Grid.Column="0" 
                                           HorizontalTextAlignment="Start"
                                           HorizontalOptions="Start"
                                           FontSize="25"
                                           FontFamily="Material-Outlined"
                                           Style="{StaticResource IconLabelStyle}"
                                           Text="{Binding Icon}" />

                                    <Label Grid.Row="0"
                                           Grid.Column="1"
                                           HorizontalTextAlignment="Start"
                                           HorizontalOptions="Start"
                                           Margin="10,0,0,0" 
                                           Text="{Binding Name}" 
                                           FontSize="16"/>

                                </Grid>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

抽屉页面.cs

private static string MAP = Lang.ResourceManager.GetString("Map");
private static string MAPICON = IconFont.ResourceManager.GetString("LocationOn");
private static string SETTINGS = Lang.ResourceManager.GetString("Settings");
private static string SETTINGSICON = IconFont.ResourceManager.GetString("Settings");

public partial class DrawerPage : ContentPage {
    drawerNavItems();
}

private void drawerNavItems()
{
    List<MenuItem> itemList = new List<MenuItem>();
    itemList.Add(new MenuItem { Icon = MAPICON, Name = MAP });
    itemList.Add(new MenuItem { Icon = SETTINGSICON, Name = SETTINGS });
    listView.ItemsSource = itemList;
 }

我遇到的问题是我不明白我应该如何使用上面教程链接中的帮助程序类来翻译抽屉菜单项。

在 Xaml 中,我们可以像这样翻译字符串

Text="{helpers:Translate Support}"

但是我如何在后面的代码中做同样的事情?

标签: c#androidiosxamarintranslate

解决方案


推荐阅读