微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 xamarin 制作观看标志以将视频添加到稍后观看列表 在您的自定义单元格中在模型中在 xaml 中

如何解决如何使用 xamarin 制作观看标志以将视频添加到稍后观看列表 在您的自定义单元格中在模型中在 xaml 中

我正在尝试制作视频流应用查看系列和电影,但遇到了问题。

问题是我无法让列表视图包含剧集编号和眼睛图标,当我按下它时,该剧集将被添加到稍后观看列表中,并且在此之后眼睛图标将更改为橙色眼睛图标。

现在的问题: 当我制作自定义视单元时,我无法为其中的标签指定值,因此图标是唯一会显示内容,因为我不知道如何为标签传递绑定值。 这是我的视单元 XAML 代码

 public EpisodeViewCell()
    {
        Label episodenum = new Label();
        Image eyebtn = new Image();
        eyebtn.source = ImageSource.Fromresource("Kitsune_Zone.imgs.emptyeye.png");
        StackLayout stackLayout = new StackLayout { Margin = new Thickness(0,10,0),FlowDirection = FlowDirection.RightToLeft,Orientation = StackOrientation.Horizontal,Children = { episodenum,eyebtn } };

        Grid grid = new Grid
        {
            RowDeFinitions = {
                                new RowDeFinition { Height = GridLength.Auto }
                             },ColumnDeFinitions = {
                                    new ColumnDeFinition { Width = GridLength.Auto }
                                }
        };
        grid.Children.Add(stackLayout,0);


        Frame frame = new Frame
        {
            BorderColor = Color.FromHex("6fbeff"),BackgroundColor = Color.FromHex("0C0C0C"),CornerRadius = 30,HeightRequest = 20,Margin = new Thickness(35,5,35,Content = grid
        };
        View = frame;
        //bindings
        eyebtn.HorizontalOptions = Layoutoptions.End;

    }

这是我如何实现它

<ListView ItemsSource="{Binding Episode}" ItemSelected="ListView_ItemSelected" x:Name="lstseries" HasUnevenRows="True" Margin="20" >
                        <ListView.Behaviors>
                            <behaviors:deselectItemBehavior />
                            <behaviors1:ListViewHeightBehavior ExtraSpace="180"/>
                        </ListView.Behaviors>
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <episodesection:EpisodeViewCell x:Name="par"/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

谁能告诉我如何为视单元中的标签传递值显示眼睛图标 或者任何人都可以为这样的事情提出另一种解决方案。

示例:

enter image description here

解决方法

如果你想在运行时改变按钮的图标,你可以定义一个可绑定的属性来改变值。

在您的自定义单元格中

public partial class EpisodeViewCell : ViewCell
{ 
    public EpisodeViewCell()
    {
        InitializeComponent();

        //...
        //binding 
       

    }



    public static readonly BindableProperty IsWatchLaterProperty = BindableProperty.Create("IsWatchLater",typeof(bool),typeof(EpisodeViewCell),false,propertyChanged: OnChanged);


    static void OnChanged(BindableObject bindable,object oldValue,object newValue)
    {
        var isNext = (bool)newValue;
        var cell = bindable as EpisodeViewCell;
        if(isNext)
        {
            cell.eyebtn.Source = ImageSource.FromResource("xxx.png");
        }
        else
        {
            cell.eyebtn.Source = ImageSource.FromResource("xxx.png");
        }

    }

    public bool IsWatchLater
    {
        get { return (bool)GetValue(IsWatchLaterProperty); }
        set { SetValue(IsWatchLaterProperty,value); }
    }



}

在模型中

添加新属性

public class YourModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyname)
    {
        PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyname));
    }

    public bool isWatchLater;
    public bool IsWatchLater
    {
        get { return isWatchLater; }
        set
        {
            isWatchLater = value;
            OnPropertyChanged("IsWatchLater");
        }
    }


    //...other properties

}

在 xaml 中

将属性绑定到模型,或者您可以在后面的代码中绑定它

<episodesection:EpisodeViewCell IsWatchLater = "{Binding IsWatchLater}"  x:Name="par"/>

现在您可以更改 ViewModel 中 IsWatchLater 的值来控制按钮的图标。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。