Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按钮可见,无法使用BindingContext-XAML_C#_Xamarin.forms - Fatal编程技术网

C# 按钮可见,无法使用BindingContext-XAML

C# 按钮可见,无法使用BindingContext-XAML,c#,xamarin.forms,C#,Xamarin.forms,我遇到了这个问题,在CollectionView中有一个按钮,该按钮具有与我的ViewModel绑定的IsVisible属性,在该按钮中还有BindingContext和CommandParameter属性。问题是,当我有BindingContext和CommandParameter时,IsVisible属性不起作用 XAML <CollectionView x:Name="collectionFri

我遇到了这个问题,在CollectionView中有一个按钮,该按钮具有与我的ViewModel绑定的IsVisible属性,在该按钮中还有BindingContext和CommandParameter属性。问题是,当我有BindingContext和CommandParameter时,IsVisible属性不起作用

XAML

                <CollectionView
                        x:Name="collectionFriend"
                        ItemsSource="{Binding ListPerson}"
                        VerticalScrollBarVisibility="Never">
                        <CollectionView.ItemsLayout>
                            <LinearItemsLayout ItemSpacing="5" Orientation="Vertical" />
                        </CollectionView.ItemsLayout>
                        <CollectionView.ItemTemplate>
                            <DataTemplate>
                                <Grid x:Name="GridPerson"
                                      Padding="5"
                                      BackgroundColor="White"
                                      ColumnDefinitions="Auto,Auto,*"
                                      RowDefinitions="*,Auto,Auto,Auto,Auto"
                                      RowSpacing="5">                                     
                                   
                                    <Label
                                        Grid.Column="1"
                                        FontAttributes="Bold"
                                        FontSize="15"
                                        FontFamily="MontSemiBold"
                                        Text="{Binding Name}"
                                        TextColor="Black"
                                        VerticalTextAlignment="Center" />
                                    <Button
                                        CornerRadius="4"
                                        Grid.Row="2"
                                        Grid.Column="1"
                                        BackgroundColor="#5bd9d9"
                                        FontSize="15"
                                        WidthRequest="140"
                                        HeightRequest="40"
                                        IsVisible="{Binding Map}"
                                        FontFamily="MontBold"
                                        Text="{helpers:Translate ViewMap}"
                                        BindingContext="{Binding Source={x:Reference collectionFriend}, Path=BindingContext}"
                                        Command="{Binding MapCommand}"
                                        CommandParameter="{Binding Source={x:Reference GridPerson}, Path=BindingContext}"
                                        TextColor="White"
                                        Padding="0">
                                       
                                    </Button>
                                    <Button
                                        x:Name="PhotosPerson"
                                        Grid.Row="3"
                                        Grid.Column="1"
                                        BackgroundColor="#3299d9"
                                        FontSize="15"
                                        WidthRequest="140"
                                        HeightRequest="40"
                                        CornerRadius="4"
                                        IsVisible="{Binding Photos}"
                                        FontFamily="MontBold"
                                        Text="{helpers:Translate Photos}"
                                        BindingContext="{Binding Source={x:Reference collectionFriend}, Path=BindingContext}"
                                        Command="{Binding PhotosCommand}"
                                        CommandParameter="{Binding Source={x:Reference GridPerson}, Path=BindingContext}"
                                        TextColor="White"
                                        Padding="0">
                                    </Button>                                     
                                    <Line Grid.Row="4"  Grid.ColumnSpan="3" BackgroundColor="#5a5a5a" HeightRequest="1" />
                                </Grid>
                            </DataTemplate>
                        </CollectionView.ItemTemplate>
                    </CollectionView>

视图模型

List<People> lista = new List<People>();
var result = await _tripRestService.GetPeople(UserTokenDbService.GetToken());
foreach (var item in result)
{ 
    
    var person = new People()
            {
                Name = item.Name,
                Photos = item.Photos,
                Map = item.Map,
                UriImage = image,
                Id = item.Id
            };
    lista.Add(person);
    ListPerson = new ObservableCollection<People>(lista.OrderBy(p => p.Name));
}
       
List lista=新列表();
var result=await_tripRestService.GetPeople(UserTokenDbService.GetToken());
foreach(结果中的var项目)
{ 
var person=新员工()
{
名称=项。名称,
照片=项目。照片,
Map=item.Map,
UriImage=图像,
Id=项目。Id
};
增加(人);
ListPerson=newobserveCollection(lista.OrderBy(p=>p.Name));
}

根据您的描述,您希望将列表人员数据绑定到CollectionView,并将ICommand绑定到Button,将当前CollectionView行传递给Button命令参数I您不需要为Button分配BindingContext,您需要使用


这是ViewModel,我使用一些简单的数据进行测试

 public partial class Page14 : ContentPage
{
    public ObservableCollection<People> ListPerson { get; set; }
    public ICommand MapCommand { get; set; }
    public ICommand PhotosCommand { get; set; }
    public Page14()
    {
        InitializeComponent();
        ListPerson = new ObservableCollection<People>()
        {
            new People(){Id=1,Name="image 1",Photos=true,Map=false },
            new People(){Id=2,Name="image 2",Photos=false,Map=true },
            new People(){Id=3,Name="image 3",Photos=true,Map=false },
            new People(){Id=4,Name="image 4",Photos=false,Map=true },
            new People(){Id=5,Name="image 5",Photos=true,Map=false },
            new People(){Id=6,Name="image 6",Photos=false,Map=true },
        };

        MapCommand = new Command(p=> {
            People person = (People)p;
            Application.Current.MainPage.DisplayAlert("Alert", person.Name, "OK");

        });
        PhotosCommand = new Command(p=> {
            People person = (People)p;
            Application.Current.MainPage.DisplayAlert("Alert", person.Name, "OK");
        });
        this.BindingContext = this;
    }
}
public class People
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Photos { get; set; }
    public bool Map { get; set; }
    public string UriImage { get; set; }

}
公共部分类第14页:内容页
{
公共ObservableCollection ListPerson{get;set;}
公共ICommand映射命令{get;set;}
公共ICommand命令{get;set;}
公共页14()
{
初始化组件();
ListPerson=新的ObservableCollection()
{
新人(){Id=1,Name=“image 1”,Photos=true,Map=false},
新人(){Id=2,Name=“image 2”,Photos=false,Map=true},
新人(){Id=3,Name=“image 3”,Photos=true,Map=false},
新人(){Id=4,Name=“image 4”,Photos=false,Map=true},
新人(){Id=5,Name=“image 5”,Photos=true,Map=false},
新人(){Id=6,Name=“image 6”,Photos=false,Map=true},
};
MapCommand=新命令(p=>{
人=(人)p;
Application.Current.MainPage.DisplayAlert(“警报”,person.Name,“确定”);
});
PhotoCommand=新命令(p=>{
人=(人)p;
Application.Current.MainPage.DisplayAlert(“警报”,person.Name,“确定”);
});
this.BindingContext=this;
}
}
公营阶层人士
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共bool照片{get;set;}
公共布尔映射{get;set;}
公共字符串UriImage{get;set;}
}

照片
似乎是
人员
上的属性,而不是整个虚拟机上的属性。这是正确的。只有当
人物
照片
时,按钮才会出现。如果我删除
BindingContext
CommandParameter
属性,它可以正常工作。但是您将该按钮的BindingContext分配给基本VM,而不是People类。如果使用相对绑定而不是在每个控件上重新分配BindingContext,则不会出现此问题,您应该发布完整的viewModel
 public partial class Page14 : ContentPage
{
    public ObservableCollection<People> ListPerson { get; set; }
    public ICommand MapCommand { get; set; }
    public ICommand PhotosCommand { get; set; }
    public Page14()
    {
        InitializeComponent();
        ListPerson = new ObservableCollection<People>()
        {
            new People(){Id=1,Name="image 1",Photos=true,Map=false },
            new People(){Id=2,Name="image 2",Photos=false,Map=true },
            new People(){Id=3,Name="image 3",Photos=true,Map=false },
            new People(){Id=4,Name="image 4",Photos=false,Map=true },
            new People(){Id=5,Name="image 5",Photos=true,Map=false },
            new People(){Id=6,Name="image 6",Photos=false,Map=true },
        };

        MapCommand = new Command(p=> {
            People person = (People)p;
            Application.Current.MainPage.DisplayAlert("Alert", person.Name, "OK");

        });
        PhotosCommand = new Command(p=> {
            People person = (People)p;
            Application.Current.MainPage.DisplayAlert("Alert", person.Name, "OK");
        });
        this.BindingContext = this;
    }
}
public class People
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Photos { get; set; }
    public bool Map { get; set; }
    public string UriImage { get; set; }

}