首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin形式TemplateBinding

Xamarin形式TemplateBinding
EN

Stack Overflow用户
提问于 2019-07-12 19:48:12
回答 2查看 2.2K关注 0票数 2

我知道XAML实际上没有从另一个页面扩展(继承)的工具,但是我看到其他人使用ControlTemplates来实现这种效果。

在这种情况下,我很难找到一个绑定。在我的示例中,我有一个由ContentPage扩展的基本MainPage。而且,我使用的是FreshMVVM,所以这就是为什么视图模型是最小的,因为FreshMVVM处理所有属性更改通知的管道。

当应用程序运行时,标签应该得到在MainPageModel中初始化的值"Xamarin Forms Header“。

我在github 这里上有一个完全可运行的源代码,但是下面是代码。有人能看到问题所在吗?

MainPage.xaml

代码语言:javascript
复制
<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

代码语言:javascript
复制
public class MainPageModel : FreshBasePageModel
{
    public MainPageModel()
    {
        LabelText = "Xamarin Forms Header";
    }

    public string LabelText { get; set; }
}

PageBase.xaml

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-15 09:02:12

你可以尝试像这样改变:

MainPage.xaml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2019-07-12 20:40:51

TemplateBinding不绑定到视图模型,所以整个代码都是错误的,请阅读主题https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/template-binding

同样值得注意的是,如果您使用Binding而不是TemplateBinding,这可能会奏效,但我不认为这样做是有意义的,因为我说了一些根本问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57013261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档