0

我有以下 XAML,它在我的主要 XAML 中包含 ContentView。

我想知道如何访问LabelPushNotificationPrice来更改文本?

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:SyncfusionBusyIndicator="clr-namespace:Syncfusion.SfBusyIndicator.XForms;assembly=Syncfusion.SfBusyIndicator.XForms"
x:Class="ZayedAlKhair.InitiativeDetails"
xmlns:SyncfusionPopup="clr-namespace:Syncfusion.XForms.PopupLayout;assembly=Syncfusion.SfPopupLayout.XForms"
Title="زايد الخير">
<ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="PushNotificationsViewTemplate">
            <ContentView BackgroundColor="White" x:Name="PushNotificationsContentView">
                <StackLayout Padding="15">
                    <Label HorizontalTextAlignment="End" Text="هذه الخدمة تمكنكم من إرسال تنبيهات الهواتف الذكية لجميع مشتركي تطبيق زايد الخير وهي أفضل خدمة لتصل مبادرتكم لآلاف المشتركين" HeightRequest="90" WidthRequest="100" />
                    <Label x:Name="**LabelPushNotificationPrice**" HorizontalTextAlignment="End" Text="سعر الخدمة : 499 دولار" HeightRequest="30" WidthRequest="100" />                        
                    <Label HorizontalTextAlignment="End" Text="مدة الترويج : مرة واحدة لكل مبادرة" HeightRequest="30" WidthRequest="100" />
                </StackLayout>
            </ContentView>
        </DataTemplate>
        <DataTemplate x:Key="PromoteViewTemplate">
            <ContentView BackgroundColor="White" x:Name="PromoteContentView">
                <StackLayout Padding="15">
                    <Label HorizontalTextAlignment="End" Text="هذه الخدمة ستجعل مبادرتكم مميزة باللون الأحمر ودائما في أعلى القائمة ليتمكن كل مستخدمي التطبيق من التعرف عليها والتفاعل معها" HeightRequest="90" WidthRequest="100" />
                    <Label HorizontalTextAlignment="End" Text="سعر الخدمة : 99 دولار" HeightRequest="30" WidthRequest="100" />                        
                    <Label HorizontalTextAlignment="End" Text="مدة التمييز : 30 يوما" HeightRequest="30" WidthRequest="100" />
                </StackLayout>
            </ContentView>
        </DataTemplate>
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Grid Padding="10" x:Name="GridInitiativeDetails">
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>
4

1 回答 1

1

首选方法是使用DataBindings,例如:

<Label HorizontalTextAlignment="End" Text="{Binding LabelPushNotificationPrice}" />

这样,您可以无缝更新Label绑定到您的ViewModel. 关键是将 UI 层与 BL 层分开,通常我们使用MVVM而不是MVC在 Xamarin.Forms 中。官方文档很好地涵盖了这个主题,并且有免费的电子书,比如使用 Xamarin.Forms 的企业应用程序模式,我建议您另外阅读。

PS:请注意,在 UI 控件上设置固定的高度和宽度可能会破坏您在不同尺寸屏幕上的 UX 体验。

于 2018-05-04T06:52:04.897 回答