如何在自定义渲染器中更新滑动 left/right 手势内 Grid 的 BindingContext?
How to update BindingContext of Grid inside swipe left/right gesture in custom renderer?
我正在尝试使用 FrameRenderer
对我的 Frame
执行左右滑动手势。我在 XAML 中有以下代码(最小):
<Frame x:Name="swipeableFrame" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid x:Name="favoritesGrid"> <!--Update BindingContext -->
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0">
<Label x:Name="englishLabel" FontSize="40" />
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="0">
<Label x:Name="romajiLabel" FontSize="40" />
</StackLayout>
</Grid>
</Grid>
</Frame>
在我的自定义渲染器 C# 代码中,我有以下内容:
public class FrameCustomRenderer : FrameRenderer
{
UISwipeGestureRecognizer leftSwipeGestureRecognizer;
UISwipeGestureRecognizer rightSwipeGestureRecognizer;
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
Forms.Init();
leftSwipeGestureRecognizer = new UISwipeGestureRecognizer();
leftSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Left;
leftSwipeGestureRecognizer.AddTarget((obj) =>
{
App.index++;
Console.WriteLine("Swiped left");
App.favoriteWords = App.DB.GetFavoritePhrases();
App.favoriteWord = App.favoriteWords[App.index];
//I want to update the BindingContext of the favoritesGrid here
});
rightSwipeGestureRecognizer = new UISwipeGestureRecognizer();
rightSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Right;
rightSwipeGestureRecognizer.AddTarget((obj) =>
{
App.index--;
Console.WriteLine("Swiped right");
App.favoriteWords = App.DB.GetFavoritePhrases();
App.favoriteWord = favoriteWords[App.index];
//I want to update the BindingContext of the favoritesGrid here
});
if (e.NewElement == null)
{
if (leftSwipeGestureRecognizer != null || rightSwipeGestureRecognizer != null)
{
this.RemoveGestureRecognizer(leftSwipeGestureRecognizer);
this.RemoveGestureRecognizer(rightSwipeGestureRecognizer);
}
}
if (e.OldElement == null)
{
this.AddGestureRecognizer(leftSwipeGestureRecognizer);
this.AddGestureRecognizer(rightSwipeGestureRecognizer);
}
}
}
在我的目标 class C# 代码中,我有以下内容:
public partial class FavoritesFrameRendererClass : Frame
{
public FavoritesFrameRendererClass()
{
InitializeComponent();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
App.favoriteWords = App.DB.GetFavoritePhrases();
App.favoriteWord = App.favoriteWords[App.index];
favoritesGrid.BindingContext = App.favoriteWord;
englishLabel.SetBinding(Label.TextProperty, Lang.English.Text());
romajiLabel.SetBinding(Label.TextProperty, Lang.Romaji.Text());
}
}
这基本上做的是,如果我向左滑动,它会给我 List
中的下一个词,如果我向右滑动,它会给我上一个词。现在左右滑动手势给了我想要的正确单词,但我的网格没有更新以显示正确的单词。有什么想法可以实现吗?
PS:我是自定义渲染器的新手。
由于 BindingContext 位于 Frame 内部的 Grid 上,因此您应该能够从 FrameRenderer 中的 Frame 内容获取 Grid 并通过执行以下操作在代码中设置其 BindingContext:
var grid = Element?.Content as Layout<Xamarin.Forms.View>;
if (grid != null)
{
grid.BindingContext = newBindingContext;
}
我正在尝试使用 FrameRenderer
对我的 Frame
执行左右滑动手势。我在 XAML 中有以下代码(最小):
<Frame x:Name="swipeableFrame" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid x:Name="favoritesGrid"> <!--Update BindingContext -->
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackLayout Grid.Row="0" Grid.Column="0">
<Label x:Name="englishLabel" FontSize="40" />
</StackLayout>
<StackLayout Grid.Row="1" Grid.Column="0">
<Label x:Name="romajiLabel" FontSize="40" />
</StackLayout>
</Grid>
</Grid>
</Frame>
在我的自定义渲染器 C# 代码中,我有以下内容:
public class FrameCustomRenderer : FrameRenderer
{
UISwipeGestureRecognizer leftSwipeGestureRecognizer;
UISwipeGestureRecognizer rightSwipeGestureRecognizer;
protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
{
base.OnElementChanged(e);
Forms.Init();
leftSwipeGestureRecognizer = new UISwipeGestureRecognizer();
leftSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Left;
leftSwipeGestureRecognizer.AddTarget((obj) =>
{
App.index++;
Console.WriteLine("Swiped left");
App.favoriteWords = App.DB.GetFavoritePhrases();
App.favoriteWord = App.favoriteWords[App.index];
//I want to update the BindingContext of the favoritesGrid here
});
rightSwipeGestureRecognizer = new UISwipeGestureRecognizer();
rightSwipeGestureRecognizer.Direction = UISwipeGestureRecognizerDirection.Right;
rightSwipeGestureRecognizer.AddTarget((obj) =>
{
App.index--;
Console.WriteLine("Swiped right");
App.favoriteWords = App.DB.GetFavoritePhrases();
App.favoriteWord = favoriteWords[App.index];
//I want to update the BindingContext of the favoritesGrid here
});
if (e.NewElement == null)
{
if (leftSwipeGestureRecognizer != null || rightSwipeGestureRecognizer != null)
{
this.RemoveGestureRecognizer(leftSwipeGestureRecognizer);
this.RemoveGestureRecognizer(rightSwipeGestureRecognizer);
}
}
if (e.OldElement == null)
{
this.AddGestureRecognizer(leftSwipeGestureRecognizer);
this.AddGestureRecognizer(rightSwipeGestureRecognizer);
}
}
}
在我的目标 class C# 代码中,我有以下内容:
public partial class FavoritesFrameRendererClass : Frame
{
public FavoritesFrameRendererClass()
{
InitializeComponent();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
App.favoriteWords = App.DB.GetFavoritePhrases();
App.favoriteWord = App.favoriteWords[App.index];
favoritesGrid.BindingContext = App.favoriteWord;
englishLabel.SetBinding(Label.TextProperty, Lang.English.Text());
romajiLabel.SetBinding(Label.TextProperty, Lang.Romaji.Text());
}
}
这基本上做的是,如果我向左滑动,它会给我 List
中的下一个词,如果我向右滑动,它会给我上一个词。现在左右滑动手势给了我想要的正确单词,但我的网格没有更新以显示正确的单词。有什么想法可以实现吗?
PS:我是自定义渲染器的新手。
由于 BindingContext 位于 Frame 内部的 Grid 上,因此您应该能够从 FrameRenderer 中的 Frame 内容获取 Grid 并通过执行以下操作在代码中设置其 BindingContext:
var grid = Element?.Content as Layout<Xamarin.Forms.View>;
if (grid != null)
{
grid.BindingContext = newBindingContext;
}