J'ai créé une vue de contenu séparément, afin de pouvoir la réutiliser dans différentes pages de contenu.
Voici mon ContentView.XAML
<?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:custom="clr-namespace:MyMXLibrary.Helpers"
x:Class="MyMXLibrary.Views.AlertView"
x:Name="this">
<ContentView.Content>
<Frame VerticalOptions="Center" HorizontalOptions="Center">
<StackLayout>
<Label Text="{Binding Source={x:Reference this}, Path=Heading}"/>
<Label Text="{Binding Source={x:Reference this}, Path=Message}"/><Label Text="Cancel">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding CancelCommand}" />
</Label.GestureRecognizers>
</Label>
<Label Text="Ok">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ProceedCommand}" />
</Label.GestureRecognizers>
</Label>
</StackLayout>
</Frame>
</ContentView.Content></ContentView>
Voici mon ContentView.Xaml.cs
public partial class AlertView : ContentView
{
public static readonly BindableProperty HeadingTextProperty = BindableProperty.Create(nameof(Heading), typeof(string), typeof(Label));
public static readonly BindableProperty MessageTextProperty = BindableProperty.Create(nameof(Message), typeof(string), typeof(Label));
public string Heading { get { return (string)GetValue(HeadingTextProperty); } set { SetValue(HeadingTextProperty, value); } }
public string Message { get { return (string)GetValue(MessageTextProperty); } set { SetValue(MessageTextProperty, value); } }
public AlertView()
{
InitializeComponent();
}
}
Voici mon exemple ContentPage.Xaml dans lequel je prévois de réutiliser la vue de contenu créée ci-dessus.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:alert="clr-namespace:MyMXLibrary.Views"
x:Class="MyMXLibrary.Views.MyPage"
Title="MyPage"><ContentPage.Content><ContentView >
<alert:AlertView Heading="Test" Message="Sample" ></alert:AlertView></ContentView></ContentPage.Content></ContentPage>
cela fonctionne bien si j'utilise une valeur statique. Mais au lieu de cela, si je lie la valeur
<alert:AlertView Heading="Test" Message="{Binding sample}" ></alert:AlertView>
J'obtiens l'erreur suivante
No property, bindable property, or event found for 'Message', or mismatching type between value and property
Comment puis-je faire la reliure ici. Puisque ma valeur est inconnue, je ne peux pas assigner une valeur statique dans XAML, je dois me contenter d'un Binding. Que dois-je faire ici pour lier la valeur, s'il vous plaît aidez-moi.