xamarin - 如何将 CollectionView.EmptyView 绑定到页面的 ViewModel?
问题描述
我正在使用 CollectionView 构建一个应用程序,我希望用户在 CollectionView 为空时向 ObservableCollection 添加一个项目。问题是:我不知道如何绑定到页面的 ViewModel。这是一个 Xamarin.Forms/MvvmCross 项目
我尝试设置 BindingContext,但没有成功。
<CollectionView.EmptyView>
<Grid Padding="24,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="154" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="154" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<pan:PancakeView
CornerRadius="8"
WidthRequest="154"
HeightRequest="154"
IsClippedToBounds="True"
HorizontalOptions="FillAndExpand"
BackgroundColor="#FF252525"
VerticalOptions="FillAndExpand"
Elevation="2">
<pan:PancakeView.HasShadow>
<OnPlatform
x:TypeArguments="x:Boolean"
iOS="False"
Android="True" />
</pan:PancakeView.HasShadow>
<ImageButton
HorizontalOptions="FillAndExpand"
Padding="50"
VerticalOptions="FillAndExpand"
BackgroundColor="#FF252525"
BindingContext="{Binding .}"
Command="{Binding AddPlaylistCommand}"
Source="icon_plus" />
</pan:PancakeView>
</Grid>
</CollectionView.EmptyView>
命令绑定不起作用,我希望我的命令只绑定到页面的 Viewmodel。
解决方案
尝试这个:
<ImageButton
HorizontalOptions="FillAndExpand"
Padding="50"
VerticalOptions="FillAndExpand"
BackgroundColor="#FF252525"
Command="{Binding Source={x:Reference Name=myPage}, Path=BindingContext.DataContext.AddPlaylistCommand}"
Source="icon_plus" />
你的. myPage
_Name
ContentPage
推荐阅读
- excel - 使用循环填充列表框,同时忽略空白
- git - 我可以远程执行存储在 Azure DevOps 存储库中的代码吗
- google-chrome - 我的谷歌浏览器没有显示 OPTIONS 请求
- reactjs - React 中 div 元素的 onBlur
- xml - 使用 XSLT 检索转义的 XML 和取消转义
- react-native - 有没有办法记录在某处创建 React Native 项目的输出以用作参考?
- laravel - 当数据库在外部服务器中时,laravel 尝试连接到 localhost 数据库
- unity3d - Unity 如何确定光线投射命中的法线?
- php - 如何从为每个字段集分隔的表单字段创建数组
- java - 在没有构建项目android studio的情况下预览xml布局文件