Xamarin表单模板绑定

时间:2019-07-12 19:48:12

标签: xamarin xamarin.forms

我了解XAML确实不具备从另一个页面扩展(继承)页面的功能,但是我已经看到其他人使用ControlTemplate来实现这种效果。

在这种情况下,我无法使绑定工作。在我的示例中,我有一个由MainPage扩展的基本ContentPage。另外,我正在使用FreshMVVM,所以这就是为什么视图模型最小化的原因,因为FreshMVVM处理所有属性更改通知。

应用运行时,标签应获得在MainPageModel中初始化的值“ Xamarin Forms Header”。

我在github here上有完全可运行的源代码,但这是代码。有人可以看到问题所在吗?

MainPage.xaml

<local:PageBase xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SampleApp"
             x:Class="SampleApp.MainPage">

    <StackLayout>
    </StackLayout>

</local:PageBase>

MainPageModel.xaml

public class MainPageModel : FreshBasePageModel
{
    public MainPageModel()
    {
        LabelText = "Xamarin Forms Header";
    }

    public string LabelText { get; set; }
}

PageBase.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.PageBase">
    <ContentPage.ControlTemplate>
        <ControlTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Label Grid.Row="0" Text="{Binding LabelText}" />
                <ContentPresenter Grid.Row="1" />
            </Grid>
        </ControlTemplate>
    </ContentPage.ControlTemplate>
</ContentPage>

2 个答案:

答案 0 :(得分:1)

您可以尝试这样更改:

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<local:PageBase xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:SampleApp"
             x:Class="SampleApp.MainPage"
             LabelText ="{Binding LabelText} " 
                >
    <StackLayout>
    </StackLayout>

</local:PageBase>

PageBase.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SampleApp.PageBase">
    <ContentPage.ControlTemplate>
        <ControlTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
            <Label Grid.Row="0" Text="{TemplateBinding LabelText}" />
                <ContentPresenter Grid.Row="1" />
            </Grid>
        </ControlTemplate>
    </ContentPage.ControlTemplate>
</ContentPage>

在您的 PageBase.xaml.cs 中:

public partial class PageBase : ContentPage
{
    public static readonly BindableProperty LabelTextProperty = BindableProperty.Create("LabelText", typeof(string), typeof(PageBase), "Control Template Demo App");
    public string LabelText
    {
        get { return (string)GetValue(LabelTextProperty); }
    }
    public PageBase ()
    {
        InitializeComponent ();
    }
}

您可以参考TemplateBinding

答案 1 :(得分:0)

TemplateBinding不会绑定到视图模型,因此整个代码从根本上是错误的,请阅读主题https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/template-binding

还要注意,如果您使用Binding而不是TemplateBinding,这可能会起作用,但是我认为这样做根本没有道理。