我試圖在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);
}
}
}
現在的問題是不明確的。你試圖做什麼,這是行不通的?例如,這裏是我的頁面,這裏是我想根據該模板添加的控件,但我沒有看到它。還請提供所有必要的xml和代碼以複製 –
謝謝Yuri,我在後面添加了xaml和代碼。 – David
要重現缺失的定義:SettingsViewModel,AlertInfo,AlertSummaryViewModel,Summary.ALERTS.After你提供那些請告訴我什麼不工作?從圖像中我看到所有數據都被填充或者它是想要的圖像? –