我有一个CarouselView,里面有3个ContentViews:
<CarouselView x:Name="CarouselObject"
Grid.RowSpan="2"
ItemsSource="{Binding Views}"
ItemTemplate="{StaticResource mySelector}"
CurrentItemChanged="CarouselView_CurrentItemChanged"
Loop="False"/>
字符串
我的视图模型:
internal class CVVM
{
public ObservableCollection<View> Views { get; set; }
public CVVM()
{
Views = new ObservableCollection<View>()
{
new testpage2(),
new testpage1(),
new ReleaseRadarView()
};
}
}
型
我希望我的应用在ContentView之间滑动时做一些事情,所以我尝试使用CurrentItemChanged事件处理程序。问题是我的一个ContentView在运行时运行它的bindingcontext,我认为,正因为如此,事件处理程序根本不会只为这个ContentView触发。ContentView XAML代码:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:Planetarium_V2.Classes"
x:Class="Planetarium_V2.testpage2">
<ContentView.BindingContext>
<vm:GameCardPack />
</ContentView.BindingContext>
<ContentView.Content>
<Grid RowSpacing="0"
BackgroundColor="#2d2d2d">
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CollectionView Grid.Row="1"
ItemsSource="{Binding cards}"
SelectionMode="Single"
SelectionChanged="CollectionView_SelectionChanged"
x:Name="CollectionViewInstance">
型
ContentView:
public partial class testpage2 : ContentView
{
public testpage2 ()
{
InitializeComponent ();
CreateBindingContextAsync();
}
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var ItemSelected = e.CurrentSelection[0] as GameCard;
var newPage = new GameDetailPage();
newPage.BindingContext = ItemSelected;
Navigation.PushAsync(newPage);
}
private async void CreateBindingContextAsync()
{
try
{
GameCardPack pack = await new JSONUnpacker().Unpack();
this.BindingContext = pack;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
型
}
我尝试使用实现OnPropertyChanged(),但它并没有真正做任何事情。
ContentView的VisualModel:
public class GameCardPack : BaseViewModel
{
ObservableCollection<GameCard> _cards { get; set; }
public ObservableCollection<GameCard> cards
{
get
{
return _cards;
}
set
{
_cards = value;
OnPropertyChanged(nameof(cards));
}
}
public GameCardPack()
{
cards = new ObservableCollection<GameCard>();
}
public void AddCard(GameCard card)
{
card.ConvertToUri();
cards.Add(card);
}
}
型
编辑:这是GameCard类:
public class GameCard
{
public string Title { get; set; }
public string[] tagsList { get; set; }
public string[] imageURLS { get; set; }
public Uri[] imageURIS { get; set; }
public string originalPrice { get; set; }
public string newPrice { get; set; }
public string description { get; set; }
public GameCard()
{
Title = "PlaceholderTitle";
tagsList = new string[]
{
"tag1",
"tag2",
"tag3"
};
imageURLS = new string[]
{
"Placeholder_view_vector.png",
"Placeholder_view_vector.png",
"Placeholder_view_vector.png",
"Placeholder_view_vector.png",
"Placeholder_view_vector.png"
};
originalPrice = "13.57";
newPrice = "4.20";
description= string.Empty;
}
public void ConvertToUri()
{
Uri[] auximageURIs = new Uri[]
{
new Uri(imageURLS[0]),
new Uri(imageURLS[1]),
new Uri(imageURLS[2]),
new Uri(imageURLS[3]),
new Uri(imageURLS[4])
};
imageURIS= auximageURIs;
}
}
型
testpage1和ReleaseRadarView contentViews目前只是一些空的ContentViews。我也计划构建它们,但我现在想解决这个问题。
1条答案
按热度按时间kpbpu0081#
我解决这个问题的方法是,不为ContentView设置绑定上下文,而是为CollectionView设置绑定上下文
字符串