我的 Shell 应用程序中的 FlyoutFooterTemplate DataTemplate 中有一个标签,我想将其设置为当前的应用程序版本 (AppInfo.Current.VersionString),但我很难完成这个简单的任务。 我不知道如何以编程方式访问模板内的 lblVersion 标签,并且找不到将标签的 Text 属性绑定到我在 AppShell.xaml.cs 类中定义的静态字符串的方法:
public static string AppVersion { get; } = AppInfo.Current.VersionString;
这是我的 AppShell.xaml 文件中代码的一部分
<Shell.FlyoutFooterTemplate>
<DataTemplate>
<Grid ColumnDefinitions="0.8*,0.2*" Padding="10">
<Label x:Name="lblVersion" TextColor="Red"
FontAutoScalingEnabled="True"
FontSize="Body"
VerticalOptions="EndAndExpand"
HorizontalOptions="Center"
Margin="10"></Label>
</Grid>
</DataTemplate>
</Shell.FlyoutFooterTemplate>
有什么想法吗?
你正在做的事情看起来非常接近。只需设置
Label.Text
的绑定即可:
<Label
x:Name="lblVersion"
Text="{Binding AppVersion}"
TextColor="Red"
FontAutoScalingEnabled="True"
FontSize="Body"
VerticalOptions="EndAndExpand"
HorizontalOptions="Center"
Margin="10"/>
然后确保在调用
InitializeComponent();
之前设置值和绑定上下文
public partial class AppShell : Shell
{
public AppShell()
{
BindingContext = this;
AppVersion = $"Version {AppInfo.Current.VersionString}";
InitializeComponent();
}
public string AppVersion { get; }
}
应该有效: