我有:
<Grid.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding Source={RelativeSource AncestorType={x:Type locals:OneViewModel}},
路徑=OneTappedView}" CommandParameter="{Binding .}" />
</Grid.GestureRecognizers>
<Button x:Name="bt_one" Clicked="bt_one_Clicked" />
當我做Grid Tap時,Command和bt_one_Clicked同時執行?謝謝你
uj5u.com熱心網友回復:
當我做Grid Tap時,Command和bt_one_Clicked會同時執行嗎?
是的,你可以在點選你的網格時,在你的網格點選事件中添加按鈕的點擊代碼。
你可以參考以下代碼:
OnePage.xml
OnePage.xml
<?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:formapp1="clr-namespace:FormApp1"
x:Class="FormApp1.OnePage">
<ContentPage.BindingContext>
<formapp1:OneViewModel></formapp1:OneViewModel>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<Grid WidthRequest="300" HeightRequest="600" BackgroundColor="Yellow" >
<Grid.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding OneTappedViewCommand}" />
</Grid.GestureRecognizers>
</Grid>
<Button x:Name="bt_one" Text="一個按鈕" Command="{Binding BtnOneClickedCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
OneViewModel.cs
public class OneViewModel: INotifyPropertyChanged
{
public ICommand OneTappedViewCommand { private set; get; }
公共ICommand BtnOneClickedCommand { private set; get; }
公共OneViewModel() {
OneTappedViewCommand = new Command(GridTapped);
BtnOneClickedCommand=新命令(btnOneClickedMthod)。
}
private void GridTapped()
{
System.Diagnostics.Debug.WriteLine("----111------> GridTapped is triggered......")。
//在這里添加一個點擊的方法
btnOneClickedMthod()。
}
private void btnOneClickedMthod()
{
System.Diagnostics.Debug.WriteLine("----222------> GridTapped is triggered......")。
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
如果(Object.Equals(storage, value))
回傳false。
storage = value。
OnPropertyChanged(propertyName);
回傳true。
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName))。
}
public event PropertyChangedEventHandler PropertyChanged;
}
注意:
在模型OneViewModel中,你可以在Grid Tap函式GridTapped中添加bt_one事件(btnOneClickedMthod)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317286.html
標籤:
下一篇:Div拆分窗格,最小寬度
