C# 如何将ViewModel绑定到另一个XAML文件的ListView中的XAML文件?

C# 如何将ViewModel绑定到另一个XAML文件的ListView中的XAML文件?,c#,xaml,listview,xamarin,xamarin.forms,C#,Xaml,Listview,Xamarin,Xamarin.forms,我真的不知道如何寻找这个,所以我希望有人能理解我的问题,或者帮助或澄清我的方法是否错误 我有两个XAML文件(ItemsPage和ItemTemplatePage),我想将每个XAML文件绑定到一个ViewModel(ItemsPageViewModel和ItemTemplateViewModel)-现在这很简单,因为它们都是单独的页面,但在本例中,ItemTemplatePage加载在ItemsPage上的ListView中,用于Items中的每个项目 显然,当我与一个项目交互时,我会调用It

我真的不知道如何寻找这个,所以我希望有人能理解我的问题,或者帮助或澄清我的方法是否错误

我有两个XAML文件(ItemsPageItemTemplatePage),我想将每个XAML文件绑定到一个ViewModel(ItemsPageViewModelItemTemplateViewModel)-现在这很简单,因为它们都是单独的页面,但在本例中,ItemTemplatePage加载在ItemsPage上的ListView中,用于Items中的每个项目

显然,当我与一个项目交互时,我会调用ItemTemplatePage的代码,但我宁愿调用ItemTemplateViewModel的命令。但是,当Items填充并绑定到ItemsPage中的ListViews ItemsSource时,据我所知,会为该特定模板创建Item的BindingContext。所以我不能调用ItemTemplatePage的构造函数:

BindingContext = new ItemTemplateViewModel();
因为此时它会覆盖ItemsSource从ItemsPage执行的任何绑定;至少到目前为止,这是我所经历的

以下是我迄今为止从代码角度提供的代码,我试图实现的目标是可行的,还是让自己变得复杂,还是我误解了绑定,并且我的方法完全错误

非常感谢您的帮助

===

ItemsPage.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:template="clr-namespace:App.Views.Templates"
             x:Class="App.Views.ItemsPage"
             Title="All Items">
    <ContentPage.Content>
        <StackLayout>
            <ListView ItemsSource="{Binding Items}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <template:Item />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Contant>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App.Views.Templates.ItemTemplatePage"
             Spacing="0">
     <Label Text="{Binding Name}" />
     <Button Text="View" />
</StackLayout>
ItemsPageViewModel.cs

Public ItemsPage() {
    InitializeComponent();
    BindingContext = new ItemsPageViewModel();
}
public class ItemsPageViewModel : BaseViewModel {
    private ObservableCollection<Item> items;
    public ObservableCollection<Item> Items { get => items; set => SetValue(ref items, value); }

    public ItemsPageViewModel() => DownloadItems();

    private async void Download() => Items = await API.GetItems(); 
}
公共类ItemsPageViewModel:BaseViewModel{
私人可观测收集项目;
公共ObservableCollection项{get=>Items;set=>SetValue(参考项,值);}
public ItemsPageViewModel()=>DownloadItems();
私有异步void Download()=>Items=await API.GetItems();
}
ItemTemplagePage.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:template="clr-namespace:App.Views.Templates"
             x:Class="App.Views.ItemsPage"
             Title="All Items">
    <ContentPage.Content>
        <StackLayout>
            <ListView ItemsSource="{Binding Items}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <template:Item />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Contant>
</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App.Views.Templates.ItemTemplatePage"
             Spacing="0">
     <Label Text="{Binding Name}" />
     <Button Text="View" />
</StackLayout>


我没有为ItemTemplatePage.xaml提供代码,因为这里没有编写其他代码。

ListView
实现了
DataTemplate
,这意味着它的内容绑定到项目,而不是一般的页面视图模型,这就是它应该如何工作的,如果您尝试提供任何其他绑定上下文,它不会使应用程序崩溃,但可以肯定的是,应用程序不会像预期的那样工作,因为这样做基本上违背了XAML的逻辑

另外,您的
ItemTemplagePage
根本不是一个页面(也不应该是),但出于某种原因,您称它为页面,这可能会让您更加困惑


总的来说,您需要做更多的工作来理解XAML中的一些基本术语和原则。我想你想检查一下这是否正确,这很好。

谢谢你的理解,伊万,从你的解释来看,我的方法似乎不可行。所以这意味着我将被要求对任何业务逻辑使用ItemTemplatePage背后的代码?我相信当你提到ItemTemplatePage不是一个实际的页面时,我理解你来自哪里。这是因为它是根标签而不是根标签吗?我采用这种方法认为,在一个答案中装入第二个是没有意义的,我这样想对吗?@MattVon很难/不可能在一个答案中解释多个基本概念。但是yes ItemTemplatePage不能有自己的视图模型。如果您真的需要将数据模型放在那里,您可以在数据模型上放置一些逻辑,它感觉最接近您所需要的。