2016-10-28 35 views
0

我試圖在Xamarin Forms中創建一個XAML模板,我可以從多個屏幕中引用它。該模板具有標題,摘要,並且可以接收觸摸輸入。在附加的線框中,將會有一個支持三個數據塊的模型。我已閱讀https://developer.xamarin.com/guides/xamarin-forms/templates/control-templates/template-binding/的文檔,但在這一點上,我覺得我錯過了一個重要概念。我以爲我只需要創建一個ContentView模板,並通過.BindingContext將內部標籤內部綁定到一個對象上。我錯過了什麼?試圖在Xamarin.Forms中創建自定義可綁定模板視圖

內容頁

<?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="Namespace.SettingsPage" 
      Title ="Settings"> 
    <ContentPage.Resources> 
    <ResourceDictionary> 
     <ControlTemplate x:Key="SettingTemplate"> 
      <StackLayout> 
       <Label 
        x:Name="Header" 
        Text="{TemplateBinding Parent.Header}" /> 
       <Label 
        x:Name="Summary" 
        Text="{TemplateBinding Parent.Summary}"/> 
       </StackLayout>      
     </ControlTemplate> 
    </ResourceDictionary> 
    </ContentPage.Resources> 
    <ContentPage.Content>  
    <StackLayout>  
     <StackLayout> 
      <ContentView x:Name="alerts" ControlTemplate="{StaticResource SettingTemplate}"></ContentView> 
      <ContentView x:Name="recipients" ControlTemplate="{StaticResource SettingTemplate}"></ContentView> 
      <ContentView x:Name="log" ControlTemplate="{StaticResource SettingTemplate}"></ContentView> 
     </StackLayout> 
    </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

代碼隱藏

using Xamarin.Forms; 

namespace Namespace.Settings 
{ 
    public partial class SettingsPage : ContentPage 
    { 
     protected SettingsViewModel viewModel; 

     public SettingsPage(AlertInfo info) 
     { 
      InitializeComponent(); 
      BindingContext = viewModel = new SettingsViewModel(info, this); 

      // Bind individual models here. AlertSummary VM has logic to pull out 
      // header and summary fiels. 
      this.alerts.BindingContext = new AlertSummaryViewModel(info, Summary.ALERTS, this); 
      this.recipients.BindingContext = new AlertSummaryViewModel(info, Summary.RECIPIENTS, this); 
     }  
    } 
} 

Wireframe

+0

現在的問題是不明確的。你試圖做什麼,這是行不通的?例如,這裏是我的頁面,這裏是我想根據該模板添加的控件,但我沒有看到它。還請提供所有必要的xml和代碼以複製 –

+0

謝謝Yuri,我在後面添加了xaml和代碼。 – David

+0

要重現缺失的定義:SettingsViewModel,AlertInfo,AlertSummaryViewModel,Summary.ALERTS.After你提供那些請告訴我什麼不工作?從圖像中我看到所有數據都被填充或者它是想要的圖像? –

回答

1

這裏是起點。

public partial class ControlTemplateTest : ContentPage 
    { 
     public static readonly BindableProperty HeaderTextProperty = 
      BindableProperty.Create("HeaderText", typeof(string), typeof(ControlTemplateTest), "Alerts"); 
     public static readonly BindableProperty SummaryTextProperty = 
      BindableProperty.Create("SummaryText", typeof(string), typeof(ControlTemplateTest), "Three alerts set"); 

     public string HeaderText 
     { 
      get { return (string)GetValue(HeaderTextProperty); } 
     } 

     public string SummaryText 
     { 
      get { return (string)GetValue(SummaryTextProperty); } 
     } 

     public ControlTemplateTest() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

的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="ButtonRendererDemo.ControlTemplateTest"> 
    <ContentPage.Resources> 
    <ResourceDictionary> 
     <ControlTemplate x:Key="SettingTemplate"> 
     <StackLayout BackgroundColor="#DCF394" > 
      <Label FontSize="48" TextColor="Black" Text="{TemplateBinding Parent.Parent.HeaderText}" /> 
      <Label Text="{TemplateBinding Parent.Parent.SummaryText}"/> 
     </StackLayout> 
     </ControlTemplate> 
    </ResourceDictionary> 
    </ContentPage.Resources> 

    <ContentPage.Content> 
    <StackLayout BackgroundColor="White" VerticalOptions="FillAndExpand" Spacing="0,40" Padding="0,40"> 
     <ContentView x:Name="alerts" VerticalOptions="FillAndExpand" ControlTemplate="{StaticResource SettingTemplate}"></ContentView> 
     <ContentView x:Name="recipients" VerticalOptions="FillAndExpand" ControlTemplate="{StaticResource SettingTemplate}"></ContentView> 
     <ContentView x:Name="log" VerticalOptions="FillAndExpand" ControlTemplate="{StaticResource SettingTemplate}"></ContentView> 
    </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

enter image description here

+1

還有一個建議。在你的情況下,我不會使用這種方法。我要麼實現單個視圖或ListView。我沒有看到在同一頁面上有多個模板控件的要點。 –

+0

或者使用DataTemplate https://developer.xamarin.com/guides/xamarin-forms/templates/data-templates/introduction/ –

+0

我最終用我的新「SettingView」對ContentView進行了子分類,並創建了專門的子類,如「 AlertSettingView「和」RecipientsSettingView「,都包含Header和Summary字段。每個子類都定義了它如何顯示自己的Header和Summary字段。 – David