C# 如何获得Xamarin形式的第一个可观测集合元素的数据?

C# 如何获得Xamarin形式的第一个可观测集合元素的数据?,c#,xamarin.forms,C#,Xamarin.forms,我正在制作一个带有CollectionView的浮动按钮,并将数据添加到ObservableCollection中。在ObservableCollection的第一个索引中,我有链接。我的想法是获取第一个索引的链接,并根据您触摸的按钮,转到列表中的链接 ViewModel.cs: public class ViewModel :INotifyPropertyChanged { public event PropertyChangedEventHandler Property

我正在制作一个带有CollectionView的浮动按钮,并将数据添加到ObservableCollection中。在ObservableCollection的第一个索引中,我有链接。我的想法是获取第一个索引的链接,并根据您触摸的按钮,转到列表中的链接

ViewModel.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

    }


}
公共类视图模型:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
公共ObservableCollection项列表{get;set;}
字符串imageprimarybutton;
公共字符串ImagePrimaryButton{get=>ImagePrimaryButton;设置{
imageprimarybutton=值;
OnPropertyChanged();
} }
公共字符串ColorPrimaryButton{get;set;}
公共命令OpenFloating{get;}
公共命令LaunchWeb{get;}
私有布尔是可见的;
公共图书馆是可见的
{
get=>isVisible;
设置
{
isVisible=值;
OnPropertyChanged();
}
}
公共视图模型(){
OpenFloating=新命令(openFloatingButton);
LaunchWeb=新命令(openApp);
ItemList=新的ObservableCollection();
IsVisible=false;
ImagePrimaryButton=“dots.png”;
ColorPrimaryButton=“#B52D50”;
//所有图像必须具有相同的分辨率(建议使用256x256,建议修剪图像)
ItemList.Add(新项目{网站=”https://facebook.com,Image=“facebook.png”,ColorButton=“#B52D50”});
ItemList.Add(新项目{网站=”https://google.com,Image=“twitter.png”,ColorButton=“#B52D50”});
ItemList.Add(新项目{网站=”https://twitter.com,Image=“insta.png”,ColorButton=“#B52D50”});
ItemList.Add(新项目{网站=”https://instagram.com,Image=“linkedin.png”,ColorButton=“#B52D50”};
}
布尔值firstStart=true;
布尔值nextClick=true;
public void openFloatingButton()
{
如果(首次启动)
{
ImagePrimaryButton=“cross.png”;
IsVisible=true;
firstStart=false;
}
其他的
{
如果(单击下一步)
{
ImagePrimaryButton=“dots.png”;
IsVisible=false;
nextClick=false;
}
其他的
{
ImagePrimaryButton=“cross.png”;
IsVisible=true;
nextClick=true;
}
}
}
public void openApp()
{
//方法获取链接并转到url
}
}
MainPage.xaml:

根据您触摸的按钮,转到列表中的链接

如果要在用户单击ImageButton时打开属性网站中定义的url,可以将网站作为命令参数传递

在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}"
       //...
                
    />
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);
            
 });