简体   繁体   English

如何获取Xamarin Forms中ObservableCollection第一个元素的数据?

[英]How to get the data of the first element of ObservableCollection in Xamarin Forms?

I'm making a floating button with a CollectionView and adding the data in an ObservableCollection.我正在制作一个带有 CollectionView 的浮动按钮,并将数据添加到 ObservableCollection 中。 In the first index of the ObservableCollection, I have links.在 ObservableCollection 的第一个索引中,我有链接。 My idea is get the links of the first index, and depends of the button you touch, go to the link they have in the list.我的想法是获取第一个索引的链接,并取决于您触摸的按钮,go 到列表中的链接。

ViewModel.cs:视图模型.cs:

 public class ViewModel :INotifyPropertyChanged
{

   

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }



    public ObservableCollection<Items> ItemList { get; set; }

    string imageprimarybutton;
    public string ImagePrimaryButton{ get => imageprimarybutton; set {
            imageprimarybutton = value;
            OnPropertyChanged();
        } }
    public string ColorPrimaryButton { get; set; }

    public Command OpenFloating { get; }

    public Command LaunchWeb { get; }

    private bool isVisible;

    public bool IsVisible
    {
        get => isVisible;
        set
        {
            isVisible = value;
            OnPropertyChanged();
        }
    }


    public ViewModel() {

        OpenFloating = new Command(openFloatingButton);
        LaunchWeb = new Command(openApp);
        ItemList = new ObservableCollection<Items>();

        IsVisible = false;

        ImagePrimaryButton = "dots.png";

        ColorPrimaryButton = "#B52D50";

       // All images must be the same resolution(256x256 recommended, trim the image is recommended)         
        ItemList.Add(new Items { Website="https://facebook.com",Image="facebook.png",ColorButton= "#B52D50" });
        ItemList.Add(new Items { Website = "https://google.com", Image = "twitter.png", ColorButton = "#B52D50" });
        ItemList.Add(new Items { Website = "https://twitter.com", Image = "insta.png", ColorButton = "#B52D50" });
        ItemList.Add(new Items { Website = "https://instagram.com", Image = "linkedin.png", ColorButton = "#B52D50" });
       


    }

    Boolean firstStart = true;
    Boolean nextClick = true;

    public void openFloatingButton()
    {


        if (firstStart)
        {
            ImagePrimaryButton = "cross.png";
            IsVisible = true;
            firstStart = false;

        }
        else
        {


            if (nextClick)
            {
                ImagePrimaryButton = "dots.png";
                IsVisible = false;
                nextClick = false;

            }
            else
            {
                ImagePrimaryButton = "cross.png";
                IsVisible = true;
                nextClick = true;

            }



        }
    }

    public void openApp()
    {
     //Method to get the link and go to the url

    }


}

MainPage.xaml:主页.xaml:

MainPage.xaml主页.xaml

depends of the button you touch, go to the link they have in the list.取决于您触摸的按钮,go 到他们在列表中的链接。

If you want to open the url that define in the property Website when user click the ImageButton, you could pass the Website as CommandParameter如果要在用户单击 ImageButton 时打开属性Website中定义的 url,则可以将 Website 作为CommandParameter

in xaml在 xaml

<?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:converters="clr-namespace:xxx"
             x:Class="xxx.MainPage"
             
             x:Name="page"  // set name of ContentPage
             
             >
 <ImageButton
                
       Command="{Binding Source={x:Reference page},Path=BindindContext.LaunchWeb}"
       CommandParameter="{Binding Website}"
       //...
                
    />

in ViewModel在视图模型中

using Xamarin.Essentials;
public ICommand LaunchWeb { get; private set; }
LaunchWeb = new Command(async(url)=> {
            
   //url here is the website of the item that you click 
   // now you can do something you want

  string AppLink = (string)url;

  await Launcher.TryOpenAsync(AppLink);
            
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM