【问题标题】:Trying to control a backgroundcolor from a label when I add it to a list当我将标签添加到列表时尝试控制标签的背景颜色
【发布时间】:2016-02-15 17:54:40
【问题描述】:

我试图通过下面的代码控制背景颜色,但颜色并没有像我想要的那样变黑。

我在我的列表中将我的公共颜色加载为透明,当它发现我想让它变黑但它似乎不起作用时。有什么线索我哪里出错了吗?

XAML:

<Label BackgroundColor="{Binding thebackgroundColor}" />

代码:

public class pubClass 
{

public Color thebackgroundColor { get; set; }

}

async void loadOurItem () 
{
ourList.Add (new pubClass () 
                { 
                    thebackgroundColor = Color.Transparent

                });
}


protected override void OnAppearing()
    {
            loadOurItem ();
            var theClass = new pubClass ();
            if (theClass.thebackgroundColor != null) {

                theClass.thebackgroundColor = Color.Black;
            }

        }
    }

更新:

private void NotifyPropertyChanged(theClass.thebackgroundColor)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(theClass.thebackgroundColor = Color.Black));
    }
}

【问题讨论】:

  • pubClass 需要实现 INotifyPropertyChanged
  • 检查我更新的代码,类似的东西?并将其从 OnAppearing 中删除?
  • 还有更多。见developer.xamarin.com/guides/xamarin-forms/user-interface/…。此外,您发布的代码未显示您为页面设置 BindingContext 的位置。最后,您似乎从多个文件中发布了 sn-ps,这使您的代码难以阅读。请清楚说明每段代码的来源。

标签: c# xamarin xamarin.forms


【解决方案1】:

听起来您想通过调整模型来更新您的 ListView 项目,尽管您在此过程中遇到了困难并且有点困惑?

试试下面的例子,这是一个简单的例子,它创建了你的MyPubClassObservableCollection

屏幕底部的按钮将显示如何通过修改每个项目直接更新模型,并将这些更改反映在ListView

在此示例中,每个项目仅循环显示颜色 RedGreenBlue

MyViewCell 类中,您很可能会创建类似于XAML 的内容,通过模型的TheBackgroundColor 属性绑定到LabelsBackgroundColor 属性,该属性实现了一个BindableProperty.

示例:-

StackLayout objStackLayout = new StackLayout()
{
    Orientation = StackOrientation.Vertical,
};

ListView objListView = new ListView();
objStackLayout.Children.Add(objListView);

objListView.ItemTemplate = new DataTemplate(typeof(MyViewCell));

ObservableCollection<MyPubClass> objItems = new ObservableCollection<MyPubClass>();
objListView.ItemsSource = objItems;

objItems.Add(new MyPubClass(Color.Red));
objItems.Add(new MyPubClass(Color.Green));
objItems.Add(new MyPubClass(Color.Blue));

Button objButton1 = new Button()
{
    Text = "Change Colors"
};
objButton1.Clicked+=((o2,e2)=>
{
    foreach (MyPubClass objItem in objItems)
    {
        if (objItem.TheBackgroundColor == Color.Red)
        {
            objItem.TheBackgroundColor = Color.Green;
        }
        else if (objItem.TheBackgroundColor == Color.Green)
        {
            objItem.TheBackgroundColor = Color.Blue;
        }
        else if (objItem.TheBackgroundColor == Color.Blue)
        {
            objItem.TheBackgroundColor = Color.Red;
        }
    }
});
objStackLayout.Children.Add(objButton1);

自定义 ViewCell:-

public class MyViewCell
    : ViewCell
{
    public MyViewCell()
    {
        Label objLabel = new Label();
        objLabel.Text = "Hello";

        objLabel.SetBinding(Label.BackgroundColorProperty, "TheBackgroundColor");

        this.View  = objLabel;
    }
}

支持类:-

public class MyPubClass
    : Xamarin.Forms.View
{


    public static readonly BindableProperty TheBackgroundColorProperty = BindableProperty.Create<MyPubClass, Color>(p => p.TheBackgroundColor, default(Color));

    public Color TheBackgroundColor
    {
        get { return (Color)GetValue(TheBackgroundColorProperty); }
        set { SetValue(TheBackgroundColorProperty, value); }
    }


    public MyPubClass(Color pobjColor)
    {
        this.TheBackgroundColor = pobjColor;
    }

}

【讨论】:

  • 不完全是我想要的,但这肯定会帮助我。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2013-08-21
  • 2019-10-14
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多