Xamarin.Froms框架背景颜色变化未显示

问题描述 投票:0回答:1

具有这种情况:一个ListView和每个单元格都是一个框架。当显示列表时,当我单击框架时,所有框架backgroudcolor是白色的,我希望颜色改变blie。我更改颜色但不刷新。这是我的代码:在xaml页面中


   <pages:PopupPage.Resources>
        <local1:ChangeFrameBackgroudColor x:Key="ChangeFrameBackgroudColor" />
    </ResourceDictionary>-->
    </pages:PopupPage.Resources>
  <ListView x:Name="IzberiFirmaListView" HasUnevenRows="True"  ItemsSource="{Binding KorisnikFirmi}" SelectedItem="{Binding IzbranaFirmaId } " Header="{Binding}" ItemTapped="IzberiFirmaListView_ItemTapped">
                <ListView.ItemTemplate >
                    <DataTemplate>
                        <local:ExtendedViewCell SelectedBackgroundColor="#2188ff"  >

                            <StackLayout  Padding="20, 10"  >
                                <Frame x:Name="frameLabel" BorderColor="#2188ff" BackgroundColor="{Binding IsActive, Converter={StaticResource ChangeFrameBackgroudColor}}">
                                    <Label  FontAttributes="Bold" FontSize="18" TextColor="Black" Text="{Binding Naziv}" ></Label>
                                </Frame>
                            </StackLayout>

                        </local:ExtendedViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.HeaderTemplate>
                    <DataTemplate>
                        <ContentView BackgroundColor="#006BE6" >
                            <Label Margin="10" HorizontalOptions="CenterAndExpand" Text="ОДБЕРЕТЕ ФИРМА"  TextColor="White" FontSize="20" FontAttributes="Bold"/>
                        </ContentView>
                    </DataTemplate>
                </ListView.HeaderTemplate>
            </ListView>

在xaml.cs页中:

        private void IzberiFirmaListView_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var vm = BindingContext as OdberiFirmaPopupViewModel;
            var firm = e.Item as SysDashFirma;
            vm.ChangeColorOnItemSelected(firm);


        }

在viewModel中

    public List<SysDashFirma> KorisnikFirmi
        {
            get { return korisnikFirmi; }
            set
            {
                if (korisnikFirmi != value)
                {
                    korisnikFirmi = value;

                    SetProperty(ref korisnikFirmi, value);
                    OnPropertyChanged("KorisnikFirmi");

                }
            }
        }
  public void ChangeColorOnItemSelected(SysDashFirma firm)
        {
            if (_oldFirmSelected == firm)
            {
                //firm.BackColor = "#2188ff";
                firm.IsActive = true;
                UpdateSelectedFirmItemColor(firm);
            }
            else
            {
                if(_oldFirmSelected != null)
                {
                    //_oldFirmSelected.BackColor = "#f5f5f5";
                    _oldFirmSelected.IsActive = false;
                    UpdateSelectedFirmItemColor(_oldFirmSelected);
                }
                // firm.BackColor = "#2188ff";
                firm.IsActive = true;
                UpdateSelectedFirmItemColor(firm);
            }


            _oldFirmSelected = firm;
        }

        private void UpdateSelectedFirmItemColor(SysDashFirma firm)
        {
            var index = KorisnikFirmi.IndexOf(firm);
            KorisnikFirmi.Remove(firm);
            KorisnikFirmi.Insert(index,firm);
        }

在列表中进行了更改,标志已更改,可能无法刷新出价上下文以使转换器从头开始读取所有内容。

xaml xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票

使用IsProActive属性的OnPropertyChanged通知UI有关其更改。

public class Firm : INotifyPropertyChanged
{
    private string name = "Unknown";

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            OnPropertyChanged();
        }
    }

    private bool isActive;

    public bool IsActive
    {
        get
        {
            return isActive;
        }
        set
        {
            isActive = value;
            OnPropertyChanged();
        }
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我建议单独更改IsActive属性,因为没有必要删除和添加该项。

<ListView
    ItemTapped="ListView_ItemTapped"
    ItemsSource="{Binding FirmCollection}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Frame
                    Padding="2"
                    BackgroundColor="{Binding IsActive, Converter={StaticResource converterTest}}">
                    <Label Text="{Binding Name}"/>
                </Frame>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Xaml.cs

void ListView_ItemTapped(System.Object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
    var tappedFirm = (e.Item as Firm);
    tappedFirm.IsActive = !tappedFirm.IsActive;
}

UI结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.