Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有不同ContentView绑定ViewModel的CarouseView_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# 具有不同ContentView绑定ViewModel的CarouseView

C# 具有不同ContentView绑定ViewModel的CarouseView,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我想把我的旋转木马换成旋转木马视图。 在我的CarouselPage中,我有一些ContentPages,我将其转换为contentview。 现在,我将contentview添加到CarouselView中 <forms:CarouselView ItemsSource="{Binding ContentViews}"> <forms:CarouselView.ItemTemplate> <DataTemplate> <ContentVie

我想把我的旋转木马换成旋转木马视图。 在我的CarouselPage中,我有一些ContentPages,我将其转换为contentview。 现在,我将contentview添加到CarouselView中

<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() };}
private List\u contentview;
公共列表内容视图
{
获取{return\u contentViews;}
set{SetProperty(ref_contentViews,value);}
}
public TestVM(){{ContentViews=new List{new Selector(),new TestView()};}
它工作正常,但我的数据绑定失败了。
现在我想知道如何将ViewModel绑定到旋转木马视图中的视图,或者是否可以将它们绑定到旋转木马的“父”ViewModel。

您做得很好,只是通常情况下,视图模型不应该保存内容视图列表(根据定义,它应该在视图中进行管理)

我要做的是在我的内容页面中创建我的内容视图,就像在您的xaml.cs中那样:

CarouselView.ItemSource = new List<View>
{
    new ContentPage{ BindingContext = myCurrentViewModel},
    new ContentPage{ BindingContext = myCurrentViewModel},
...
}
CarouselView.ItemSource=新列表
{
新内容页{BindingContext=myCurrentViewModel},
新内容页{BindingContext=myCurrentViewModel},
...
}
我找到了解决方案: 我将内容设置为视图的内容,但绑定设置在视图内容之外。 现在,我将绑定放在视图的内容中,它最终会起作用

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

在我的
视图模型中
我创建了视图模型的an
项目源
,这样我可以将不同的视图模型绑定到旋转木马中的每个视图。然后我创建一个DataTemplateSelector,用于处理视图和视图模型的匹配

视图模型代码:

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

    public ObservableCollection<ViewModelBase> Views { get; set; }
}
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>

我制作了一个类似的解决方案,但为组合ContentView/ViewModel制作了一个额外的容器:

以下是全球旋转木马视图:

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

你的Xaml是什么样子的?因为当我尝试你的方法时,我仍然没有任何绑定。我的Xaml和你的Xaml一样,我快疯了。请帮助。在网络上和这个页面上,人们都使用这样的例子:``但每当我这样做时,我都会收到错误消息,无法分配属性“ItemTemplate”..!!!…发生了什么事???发生了什么事?你受黑猩猩的摆布。你的“CarouselView”对象需要来自CarouselView.Forms.Plugin而不是Xamarin.Forms。前者具有你需要的属性,而后者只是一个“chimp占位符元素”
        <CarouselView ItemsSource="{Binding ContentViews}">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <ContentView Content="{Binding Content}" BindingContext="{Binding ViewModel}" />
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
        public ContainerViewModel()
    {
        _contentViews = new ObservableCollection<ContentViewWithVM>();
        ContentViews.Add(new ContentViewWithVM
        {
            Content = new Page1View(),
            ViewModel = this //Or any other viewmodel
        });
        ContentViews.Add(new ContentViewWithVM
        {
            Content = new Page2View(),
            ViewModel = this
        });
        ContentViews.Add(new ContentViewWithVM
        {
            Content = new Page3View(),
            ViewModel = this
        });
    }
public class ContentViewWithVM : BindableObject
{
    public ContentView _content;
    public object _viewModel; //Choose a base viewmodel, object just for reference

    public object ViewModel
    {
        get
        {
            return _viewModel;
        }

        set
        {
            _viewModel = value;
            OnPropertyChanged();
        }
    }

    public ContentView Content
    {
        get
        {
            return _content;
        }

        set
        {
            _content = value;
            OnPropertyChanged();
        }
    }
}