Xaml 访问放置在ResourceDictionary内ContentView内的标签

Xaml 访问放置在ResourceDictionary内ContentView内的标签,xaml,xamarin.forms,resourcedictionary,content-pages,Xaml,Xamarin.forms,Resourcedictionary,Content Pages,我有下面的XAML,它在我的主XAML中包含ContentView 我想知道如何访问标签PushNotificationPrice来更改文本 <?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:SyncfusionB

我有下面的XAML,它在我的主XAML中包含ContentView

我想知道如何访问标签PushNotificationPrice来更改文本

<?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>

首选方法是使用,例如:

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

这样,您就可以无缝地更新绑定到
ViewModel
标签的值。重点是将UI层与BL层分开,我们通常在Xamarin.Forms中使用
MVVM
而不是
MVC
。这本书很好地涵盖了这一主题,而且还有像这样的免费电子书,我建议另外阅读


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

这是否意味着不绑定就无法访问标签及其属性?例如.IsVisible或.TextColr等…不确定“不可能”,但如果没有数据绑定,它会变得非常棘手。请问如何绑定它?我试过了,但它没有显示任何值,也没有出现错误:公共类BannerAds{public double ad_price{get;set;}}}httpresponsemessageresponse=wait client.GetAsync(“url_here”);如果(response.issusccessstatuscode){var data=wait response.Content.ReadAsStringAsync();var result=JsonConvert.DeserializeObject(数据);observeCollection trends=new observeableCollection(result);this.BindingContext=trends;}并且在XAML中: