Xamarin.forms 绑定到ControlTemplate中的属性无效

Xamarin.forms 绑定到ControlTemplate中的属性无效,xamarin.forms,freshmvvm,Xamarin.forms,Freshmvvm,我有一个带有Xamarin.Forms和FreshMvvm的移动应用程序。每个页面都使用App.xaml中定义的控件模板: <ControlTemplate x:Key="MainPageTemplate"> <Grid BindingContext="{Binding Source={RelativeSource TemplatedParent}}"> &

我有一个带有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>
由于某些原因,屏幕名没有显示在使用控件模板的页面上。如果我用硬编码文本替换绑定,则会:

<Label Text="Something" ...

绑定上下文设置为
{RelativeSource TemplatedParent}
时,绑定上下文就是页面

要访问ViewModel,必须更改xaml以通过页面的“BindingContext”访问属性



除非在视图模型的构造函数中设置了该属性,否则您需要实现INotifyPropertyChanged,并让UI知道该属性已更新。@TaylorD感谢您的评论。我确实在构造函数中初始化了属性,但它没有帮助…下一个想法是您将绑定上下文设置到页面而不是ViewModel,所以请尝试绑定到
BindingContext.ScreenName
。@TaylorD Yes!就这样!请你把它作为你的答案,我会这样做的。非常感谢你!请注意,我们更喜欢这里的技术风格。我们轻轻地劝阻问候,希望你能帮助,谢谢,提前感谢,感谢信,问候,亲切的问候,签名,请你能帮助,闲聊的材料和缩写的txtspk,恳求,你被困多久了,投票建议,元评论等。只需解释你的问题,并展示你已经尝试了什么,你期望什么,实际上发生了什么,非常感谢!很高兴我能帮忙!
<Label Text="Something" ...