2017-03-06 113 views
1

我试图将我的CarouselPage转换为CarouselView。 在我的CarouselPage中,我有一些ContentPages,我将其转换为ContentViews。 现在我将ContentView添加到CarouselView。CarouselView与不同的ContentViews绑定ViewModel

<forms:CarouselView ItemsSource="{Binding ContentViews}"> 
<forms:CarouselView.ItemTemplate> 
    <DataTemplate> 
    <ContentView Content="{Binding Content}" />/ 
    </DataTemplate> 
</forms:CarouselView.ItemTemplate> 
</forms:CarouselView> 

//视图模型

private List<ContentView> _contentViews; 
public List<ContentView> ContentViews 
    { 
     get { return _contentViews; } 
     set { SetProperty(ref _contentViews, value); } 
    } 
public TestVM(){{ContentViews = new List<ContentView> { new Selector(), new TestView() };} 

它工作正常,但我真的失去了我的数据绑定。 现在我想知道如何将ViewModel绑定到Carousel视图中的视图,或者如果我可以将它们绑定到Carousel的“Parent”Viewmodel。

回答

0

我找到了解决办法: 我设置了内容的意见的内容,但绑定被设置在视图的内容之外。 现在我把绑定内的最终作品的视图的内容。

<ContentView.Content> 
    <StackLayout> 
     <StackLayout.BindingContext> 
      <viewModels:TestViewModel /> 
     </StackLayout.BindingContext> 
     <BoxView BackgroundColor="{Binding Colorback}"></BoxView> 
     <Label Text="Test" ></Label> 
    </StackLayout> 
</ContentView.Content> 
0

除了通常情况下,您的视图模型不应包含内容视图列表(根据定义,它应该在视图中进行管理),您做得很好。

我要做的就是在我的内容页面创建我的内容的看法,就像在你的xaml.cs:

CarouselView.ItemSource = new List<View> 
{ 
    new ContentPage{ BindingContext = myCurrentViewModel}, 
    new ContentPage{ BindingContext = myCurrentViewModel}, 
... 
} 
+0

你的Xaml是什么样的?因为当我尝试你的方法时,我仍然没有任何绑定。 – Olias

+0

我的xaml和你的一样 – Daniel

0

在我ViewModel我创建了我的ViewModels的ItemSource这样我可以在不同的ViewModels绑定到转盘每个视图。然后我创建一个DataTemplateSelector来处理Views和ViewModels的匹配。

视图模型代码:

public class HomeViewModel 
{ 
    public HomeViewModel() 
    { 
     Views = new ObservableCollection<ViewModelBase> 
     { 
      new SomeViewModel1(), 
      new SomeViewModel2() 
      new SomeViewModel3(), 
      new SomeViewModel4() 
     }; 
    } 

    public ObservableCollection<ViewModelBase> Views { get; set; } 
} 

ViewModelBase:

public class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void RaisePropertyChanged(string name) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); 
    } 
} 

HomeViewSelector:

public class HomeViewSelector : DataTemplateSelector 
{ 
    public DataTemplate ViewTemplate1 { get; set; } 
    public DataTemplate ViewTemplate2 { get; set; } 
    public DataTemplate ViewTemplate3 { get; set; } 
    public DataTemplate ViewTemplate4 { get; set; } 

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container) 
    { 
     var tab = (ViewTab)item; 
     DataTemplate template = null; 

     switch (tab.SelectedTab) 
     { 
      case CarouselViewTabs.View1: 
       template = ViewTemplate1; 
       break; 
      case CarouselViewTabs.View2: 
       template = ViewTemplate2; 
       break; 
      case CarouselViewTabs.View3: 
       template = ViewTemplate3; 
       break; 
      case CarouselViewTabs.View4: 
       template = ViewTemplate4; 
       break; 
      default: 
       throw new NotSupportedException(); 
     } 

     return template; 
    } 
} 

Home.xaml:

<?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:cv="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions" 
      xmlns:v="clr-namespace:Views" 
      xmlns:s="clr-namespace:Selectors" 
      x:Class="WeddingPhotos.Mobile.Views.HomePage"> 
    <ContentPage.Resources> 
     <ResourceDictionary> 
      <DataTemplate x:Key="ViewTemplate1"> 
       <v:SomeView1 BindingContext="{Binding BindingContext.ViewModel}" /> 
      </DataTemplate> 
      <DataTemplate x:Key="SomeView2"> 
       <v:SomeView2 BindingContext="{Binding BindingContext.ViewModel}" /> 
      </DataTemplate> 
      <DataTemplate x:Key="SomeView3">    
       <v:SomeView3 BindingContext="{Binding BindingContext.ViewModel}" /> 
      </DataTemplate> 
      <DataTemplate x:Key="SomeView4"> 
       <v:SomeView4 BindingContext="{Binding BindingContext.ViewModel}" /> 
      </DataTemplate> 
      <s:HomeViewSelector x:Key="HomeViewSelector" 
           SomeView1="{StaticResource ViewTemplate1}" 
           SomeView2="{StaticResource ViewTemplate2}" 
           SomeView3="{StaticResource ViewTemplate3}" 
           SomeView4="{StaticResource ViewTemplate4}"/> 
     </ResourceDictionary> 
    </ContentPage.Resources> 
    <ContentPage.Content> 
     <StackLayout>   
      <cv:CarouselViewControl ItemsSource="{Binding Tabs}" 
            ItemTemplate="{StaticResource HomeViewSelector}" /> 
     </StackLayout> 
    </ContentPage.Content> 
</ContentPage>