我有一个带有ListView的页面,其中显示了一些带有音频数据的单元格。
视图:
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:audioViews="clr-namespace:Lama.Forms.Source.UI.CustomControls.AudioViews;assembly=Lama.Forms"
x:Class="Lama.Forms.Source.UI.Pages.MainPages.MacOSAudiofileCellView"
Padding="0" HasShadow="False" >
<StackLayout BackgroundColor="{Binding BackgroundColor}" Orientation="Vertical" Margin="0,2,0,2" Spacing="0">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding SelectCellCommand}" />
</StackLayout.GestureRecognizers>
<Grid RowSpacing="0" ColumnSpacing="0" VerticalOptions="FillAndExpand" Margin="0,0,0,5">
<Grid.RowDefinitions>
<RowDefinition Height="75" />
<RowDefinition Height="75" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="75" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
...
<!-- The Playback Buttons -->
<Grid Grid.Row="1" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- Play Button -->
<Image Source="{Binding PlayButtonIcon}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding PlayCommand}" />
</Image.GestureRecognizers>
</Image>
<!-- Stop Button -->
<Image Source="{Binding StopButtonIcon}" Grid.Column="1">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding StopCommand}" />
</Image.GestureRecognizers>
</Image>
</Grid>
<!-- The Grid for Filename, Artist and Title -->
<Grid Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- The Filename -->
<StackLayout Grid.ColumnSpan="2" Margin="10,0,0,0"
Orientation="Horizontal" VerticalOptions="Fill">
<Label Text="Filename:"
VerticalTextAlignment="Center"
WidthRequest="100"
VerticalOptions="Fill"
Style="{StaticResource CellLabel}" />
<Label x:Name="LblFilename" Text="{Binding Filename}"
VerticalTextAlignment="Center"
VerticalOptions="Fill"
Style="{StaticResource CellLabel}"/>
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ChooseNewFilenameCommand}"/>
</StackLayout.GestureRecognizers>
</StackLayout>
<!-- The Artist -->
<StackLayout Grid.Row="1" Grid.Column="0" Margin="10,0,0,0" Orientation="Horizontal">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ChooseNewArtistCommand}"/>
</StackLayout.GestureRecognizers>
<Label Text="Artist:"
WidthRequest="100"
VerticalTextAlignment="Center"
Style="{StaticResource CellLabel}"/>
<Label Text="{Binding Artist}"
VerticalTextAlignment="Center"
Style="{StaticResource CellLabel}"/>
</StackLayout>
...
</ViewCell>
ViewModel:
public class AudiofileCellViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
#region Commands
public Command SelectCellCommand { get; }
public Command ChooseNewFilenameCommand { get; }
public Command ChooseNewArtistCommand { get; }
public Command PlayCommand { get; }
public Command StopCommand { get; }
#endregion
#region CTOr
protected BaseAudiofileCellViewModel()
{
SelectCellCommand = new Command(SelectCell);
ChooseNewFilenameCommand = new Command(async () => await ChooseNewFilename());
ChooseNewArtistCommand = new Command(ChooseNewArtist);
ChooseNewTitleCommand = new Command(ChooseNewTitle);
ChooseNewAlbumCommand = new Command(ChooseNewAlbum);
ChooseNewCommentCommand = new Command(ChooseNewComment);
ChooseNewKeyCommand = new Command(ChooseNewKey);
ChooseNewBpmCommand = new Command(ChooseNewBpm);
PlayCommand = new Command(async () => await PlayAudiofile());
StopCommand = new Command(StopAllAudiofiles);
}
#endregion
为标签定义了一些命令,这些命令将在Tap上调用。这适用于Xamarin.Forms.UWP,但是如果我切换到Xamarin.Forms.Mac,它们不会启动。我可以看到,通过主页上的动画视图发生了某些事情,该事件在单击时出现断断续续的情况,但从未遇到断点。 我正在运行最新的稳定器:Xamarin.Forms 3.1.0.697729
答案 0 :(得分:0)
macOS当前没有TapGestureHandler
实现,实际上根本没有Gesture处理程序,因为当前的macOS平台不支持它们。有一个写为NSClickGestureRecognizer
的Xamarin,但是在不同的Forms控件上以及在macOS上如何实现它们上存在问题。
在UWP上这很有意义,因为Microsoft不仅在UWP中具有触摸界面实现,而且在操作系统本身也具有触摸界面实现;如果您没有触摸界面,则操作系统会将鼠标/触控板的点击转换为“基于触摸的点击”,并且UWP touch API“正好”工作。
现在,将来macOS Mojave将通过苹果将基于iOS的仅框架移植到macOS来支持iOS应用,因此 UIKit框架已移植,您将能够使用UIKit而不是仅仅AppKit。因此,这将支持将鼠标/触控板的点击转换为本地UITapGestureRecognizer
事件。我还没有深入研究Mojave,以了解实际效果如何以及AppKit小部件是否会继承UIKit功能,但是由于您使用的是Xamarin.Forms
,因此您只需要等到Xamarin支持新的OS功能(当然至少在触摸支持方面,您的应用只能是Mojave。