我正在尝试使用Frame
向我的FrameRenderer
实施向左和向右滑动手势。我在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);
}
}
}
在我的目标类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:我是自定义渲染器的新手。
答案 0 :(得分:0)
由于BindingContext位于Frame内部的Grid上,因此您应该能够从FrameRenderer中的Frame内容中获取Grid,并通过执行以下操作在代码中设置其BindingContext:
var grid = Element?.Content as Layout<Xamarin.Forms.View>;
if (grid != null)
{
grid.BindingContext = newBindingContext;
}