eri*_*rin 2 c# prism xamarin xamarin.forms
将 Prism 用于 Xamarin Forms,我有一个像这样的 ListView:
<ListView ItemsSource="{Binding Conditions}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="4" RowSpacing="0">
<Button Command="{Binding DoSomething}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
其中 Condition 是 ConditionViewModel 的 ObservableCollection,DoSomething 命令位于页面的 ViewModel 中。
所以,当然,绑定在这里不起作用,因为它的 bindingcontext 将是一个单独的 ConditionViewModel 项目。
我知道相对绑定在 Xamarin 中不可用,并且规定的解决方案似乎是直接应用 {x:Reference SomeViewModel}。但是使用 Prism,View 不能直接访问它的 ViewModel,所以这是不可能的。
我也看过这个,试图获得一个 RelativeContext 标记扩展:https : //github.com/corradocavalli/Xamarin.Forms.Behaviors/blob/master/Xamarin.Behaviors/Extensions/RelativeContextExtension.cs 但是,得到RootObject 为空的异常。
还有其他解决办法吗?
如果我正确理解你的问题,你想要的是这样的:
视图模型:
public class MyPageViewModel : BindableBase
{
public MyPageViewModel()
{
MyCommand = new DelegateCommand<MyModel>( ExecuteMyCommand );
}
public ObservableCollection<MyModel> Collection { get; set; }
public DelegateCommand<MyModel> MyCommand { get; }
private void ExecuteMyCommand( MyModel model )
{
// Do Foo
}
}
Run Code Online (Sandbox Code Playgroud)
看法;
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyProject.Views.MyPage"
x:Name="page">
<ListView ItemsSource="{Binding Collection}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Button Command="{Binding BindingContext.MyCommand,Source={x:Reference page}}"
CommandParameter="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1277 次 |
| 最近记录: |