【发布时间】:2020-06-05 04:46:13
【问题描述】:
以 xamarin 形式工作。如何从内容页面引用内容视图,但还包括传递信息,如下所示?
await Navigation.PushAsync(new SecondContentPage(new NextViewModel(FirstViewModel.id)));
我尝试过 xaml 绑定上下文,但不知道从那里去哪里,它不断给我关于无参数构造函数的错误。
xmlns:viewmodel="clr-namespace:Project.ViewModels"
<StackLayout>
<local:SecondContentView>
<local:SecondContentView.BindingContext>
<viewmodel:NextViewModel></viewmodel:NextViewModel>
</local:SecondContentView.BindingContext>
</local:SecondContentView>
</StackLayout>
我基本上需要传递的 id,以便列表可以在内容视图上运行。谢谢大家
更新 - 我创建了一些新的示例代码。我创建了一个页面以使用 Listview 嵌套第二个页面。在我尝试使用 x:Arguments 或在 ViewModel 构造函数中从第一页到第二页传递参数之前效果很好。
首页
**<StackLayout>
<StackLayout>
<Label Text="First Page Content"></Label>
</StackLayout>
<StackLayout>
<local:SecondContentView>
<local:SecondContentView.BindingContext>
<viewmodel:SecondViewModel>
<x:Arguments>102</x:Arguments>
</viewmodel:SecondViewModel>
</local:SecondContentView.BindingContext>
</local:SecondContentView>
</StackLayout>
</StackLayout>**
第二页
<ContentView.Content>
<StackLayout>
<Label Text="Second Page"></Label>
<ListView x:Name="FirstListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10">
<Label Text="{Binding pType}"></Label>
<Label Text="{Binding fDepartment}"></Label>
<Label Text="{Binding Description}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentView.Content>
后面的第二页代码
SecondViewModel ViewModel;
public SecondContentView(SecondViewModel viewmodel)
{
InitializeComponent();
BindingContext = ViewModel = viewmodel;
FirstListView.ItemsSource = ViewModel.TypeList;
}
第二页的视图模型
**public List<TypeModel> TypeList;
public SecondViewModel(int parameter)
{
var p = parameter;
TypeList = new List<TypeModel>()
{
new TypeModel { pType = 1, Title = "First Type", Description = "First Description", Version = "9.9.9", fDepartment = 101 , Comments = "None"},
new TypeModel { pType = 2, Title = "Second Type", Description = "Second Description", Version = "9.9.9", fDepartment = 101 , Comments = "None"},
new TypeModel { pType = 3, Title = "Third Type", Description = "Third Description", Version = "9.9.9", fDepartment = 102 , Comments = "None"},
new TypeModel { pType = 4, Title = "Fourth Type", Description = "Fourth Description", Version = "9.9.9", fDepartment = 102 , Comments = "None"}
};
}**
【问题讨论】:
标签: xaml xamarin mvvm data-binding