Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Reference a custom ContentView in XAML

$
0
0

So I'm trying to do something that seems like it should be really straight forward. I created a custom ContentView to output a list of items without needing the ListView.

namespace MyApp.Pages
{

    public partial class DashboardObjectAlerts : ContentView
    {
        //Bindable property for the progress color
        public static readonly BindableProperty AlertsProperty =
            BindableProperty.Create<DashboardObjectAlerts,IEnumerable<DashboardAlert>> (p => p.Alerts, new List<DashboardAlert>(), BindingMode.OneWay,null,
                (bindable, oldValue, newValue) => { ((DashboardObjectAlerts)bindable).UpdateDisplay (); });

        public IEnumerable<DashboardAlert> Alerts {
            get { return (IEnumerable<DashboardAlert>)GetValue (AlertsProperty); }
            set { SetValue (AlertsProperty, value); }

        }

        public DashboardObjectAlerts ()
        {
            InitializeComponent ();

        }

        public void UpdateDisplay()
        {
            AlertsLayout.Children.Clear ();
            foreach (var alert in Alerts) {
                AlertsLayout.Children.Add (new ContentView()
                    {
                        Content = new Frame()
                        {
                            HasShadow = true,
                            BackgroundColor = alert.AgeColor,
                            Content = new Label()
                            {
                                Text = alert.Text
                            }
                        }
                    });
            }

        }
    }
}

Associated 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" x:Class="MyApp.Pages.DashboardObjectAlerts">
    <ContentView.Content>
        <StackLayout x:Name="AlertsLayout" Orientation="Horizontal">

        </StackLayout>
    </ContentView.Content>
</ContentView>

Then in my Page I'm trying to reference it as such:

xmlns:myApp="clr-namespace:MyApp;assembly=MyApp"

And:

<ListView x:Name="DashboardList"
    BackgroundColor="Black"
    SeparatorVisibility="None"
    HasUnevenRows="true">


    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                    <ContentView
                        Padding="5,5,5,5"
                        HorizontalOptions="FillAndExpand"
                        VerticalOptions="StartAndExpand">
                        <Frame
                            HasShadow="true"
                            BackgroundColor="{x:Static myApp:InnovativeColorHelper.GreyLight}">
                            <StackLayout
                                VerticalOptions="StartAndExpand">
                                <Label Text="{Binding OrganizationName}" />
                                <Label Text="{Binding UnitName}" />
                                <Label Text="{Binding Name}" />
                                <Label Text="Item is critical" 
                                       IsVisible="{Binding IsCritical}"
                                       TextColor="Red" />

                                <myApp:Pages.DashboardObjectAlerts Alerts="{Binding Alerts}" />
                            </StackLayout>
                        </Frame>
                    </ContentView>
            </ViewCell>
        </DataTemplate> 
    </ListView.ItemTemplate>

</ListView>

When I try to reference DashboardObjectAlerts I get a error complaining about x:Static, but when I take the reference out the x:Static bind works just fine.


Viewing all articles
Browse latest Browse all 89864

Trending Articles