3 votes

ContentView réutilisable dans les formulaires Xamarin

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.

4voto

jgoldberger Points 2653

Je pense que le code de création de BindableProperty est erroné. Vous avez :

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));

Le troisième paramètre est donc typeof(Label) mais il doit s'agir du type de la classe qui aura la propriété, dans ce cas-ci AlertView . De plus, par convention (je ne sais pas si c'est obligatoire), le nom de la propriété liante sera le nom de la propriété + "Property". Vous avez le nom de la propriété + "TextProperty". Essayez donc :

public static readonly BindableProperty HeadingProperty = 
    BindableProperty.Create("Heading", typeof(string), typeof(AlertView));
public static readonly BindableProperty MessageProperty = 
    BindableProperty.Create("Message", typeof(string), typeof(AlertView));

Voir cette pour plus d'informations. Vous constaterez que le troisième paramètre de la fonction BindableProperty.Create est la méthode declaringType qui doit être le type définissant la propriété liante.

exemple de projet : https://www.dropbox.com/s/j7kpehpt8htrf8k/TestAlertView.zip?dl=0

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X