我有一个带有 Xamarin.Forms 和 FreshMvvm 的移动应用程序。每个页面都使用 App.xaml 中定义的控件模板:
<ControlTemplate x:Key="MainPageTemplate">
<Grid BindingContext="{Binding Source={RelativeSource TemplatedParent}}">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="9*" />
</Grid.RowDefinitions>
<Image Source="{local:ImageResource MyApp.Images.My_logo.jpg}" Margin="0, 0, 0, 5" Grid.Row="0" />
<Label Text="{Binding ScreenName}" FontSize="Subtitle" TextColor="Black" FontAttributes="Bold" HorizontalOptions="Center" Grid.Row="1" />
<Label Text="{Binding ErrorMessage}" TextColor="Red" Grid.Row="2" />
<ContentPresenter Margin="10, 0, 10, 10" Grid.Row="3" />
</Grid>
</ControlTemplate>
ScreenName 属性在 PageModel 中定义如下:
protected string _screenName;
public string ScreenName => _screenName;
由于某种原因,屏幕名称未显示在使用控件模板的页面上。如果我用硬编码文本替换绑定,它会:
<Label Text="Something" ...