与 Xamarin Forms 中的 BasePage 绑定

use*_*037 2 binding xamarin.forms binding-context freshmvvm

我正在使用 FreshMVVM 框架实现 Xamarin 应用程序,我想使用 BasePage 在页面之间共享一些代码。问题是,当我需要在 MainPage.xaml 中绑定某些属性时,我必须以这种方式指定源才能使其正常工作:Text="{Binding Title, Source={x:Reference mainPage}}"。否则没有绑定就不起作用。好的,我明白了,但是这是正确的方法吗?还有其他方法可以达到相同的结果吗?当页面中有大量绑定时怎么办?例如,是否可以在上层“设置”源,因为在我看来,为每个绑定设置相同的源是非常烦人的。

基本页.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestXamarin.BasePage"
             x:Name="basePage">
    <ContentView>
        <StackLayout Orientation="Vertical">
            <Label Text="HEADER" FontSize="Large"/>
            <Label Text="{Binding Text, Source={x:Reference basePage}}" FontSize="Large"/>
            <ContentPresenter BindingContext="{Binding Parent.BindingContext}" 
                              Content="{Binding PageContent, Source={x:Reference basePage}}" />
        </StackLayout>
    </ContentView>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

基本页.xaml.cs

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestXamarin
{
	[XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class BasePage : ContentPage
	{
		public static readonly BindableProperty TextProperty = BindableProperty.Create(
			   nameof(Text),
			   typeof(string),
			   typeof(BasePage));

		public string Text
		{
			get { return (string)GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}

		public static readonly BindableProperty PageContentProperty = BindableProperty.Create(
			   nameof(PageContent),
			   typeof(object),
			   typeof(BasePage));

		public object PageContent
		{
				get { return GetValue(PageContentProperty); }
				set { SetValue(PageContentProperty, value); }
		}

		public BasePage()
		{
			InitializeComponent();
		}
	}
}
Run Code Online (Sandbox Code Playgroud)

主页.xaml

<local:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:TestXamarin"
                x:Class="TestXamarin.MainPage"
                Text="FROM MAIN PAGE"
                x:Name="mainPage">
    <local:BasePage.PageContent>
        <StackLayout>
            <Label Text="Body" FontSize="Large"/>
            <Label Text="{Binding Title, Source={x:Reference mainPage}}" FontSize="Large"/>
        </StackLayout>
    </local:BasePage.PageContent>
</local:BasePage>
Run Code Online (Sandbox Code Playgroud)

MainPage.xaml.cs

public partial class MainPage : BasePage
	{
		public MainPage()
		{
			Title = "MAIN PAGE";

			InitializeComponent();
		}
	}
Run Code Online (Sandbox Code Playgroud)

Ste*_*rne 5

实现您想要做的事情的另一种方法是使用控件模板。

这里我在App.xaml中定义了一个模板

<ControlTemplate x:Key="ActivityIndicatorTemplate">
    <Grid>
        <ContentPresenter />
        <StackLayout Style="{StaticResource BlockingPanel}"
                     IsVisible="{TemplateBinding BindingContext.IsBusy}">
            <ActivityIndicator Style="{StaticResource ActivityIndicatorStyle}"
                               IsVisible="{TemplateBinding BindingContext.IsBusy}"
                               IsRunning="{TemplateBinding BindingContext.IsBusy}" />
        </StackLayout>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

请注意内容呈现器和 TemplateBinding。

我在这样的页面上使用它。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Test.MyTestPage"
             ControlTemplate="{StaticResource ActivityIndicatorTemplate}"
             Title="{Binding Title}">

    <Grid>
    ...
    </Grid>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

页面内容替换模板中的内容呈现器。看起来比基页简单。