【发布时间】:2018-08-20 13:09:03
【问题描述】:
我正在尝试将我的应用程序转换为 MVVM 架构,但我遇到了 itemsource 的问题。没有可用的命令属性。
我应该如何将 itemtapped 和 Menuitem clicked 转换为 MVVM 命令? 当前使用 FRESHMVVM。我在谷歌搜索时找到了一些解决方案,但它们似乎非常复杂。
<ContentPage.Resources>
<ResourceDictionary>
<converters:StatusTextConverter x:Key="IntStatusToTextConverter" />
<converters:DateTextConverter x:Key="DateToTextConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ListView ItemsSource="{Binding Issues}" ItemTapped="OnEventSelected" SeparatorColor="#444444" RowHeight="90" IsPullToRefreshEnabled="True" IsRefreshing="{Binding IsBusy}" RefreshCommand="{Binding PullRefreshCommand}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<ViewCell.ContextActions>
<MenuItem Clicked="OnDelete" Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Source="{Binding SeverityImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="70"/>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="2" Source="{Binding StatusImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="60"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Title}" LineBreakMode="TailTruncation" YAlign="Center" VerticalOptions="Start" Font="Bold, Medium"/>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Created, Converter={StaticResource DateToTextConverter}}" YAlign="Center" VerticalOptions="Start" Font="Medium"/>
<Label Grid.Row="2" Grid.Column="1" Text="{Binding Description}" LineBreakMode="WordWrap" YAlign="Start" VerticalOptions="Start" Font="Small"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
PageModel 方法
public async void OnEventSelected(object o, ItemTappedEventArgs e)
{
if (IsBusy)
return;
var item = e.Item as IssueModel;
if (item != null)
{
MessagingCenter.Subscribe<IssuePageModel>(this, "refresh", async (sender) =>
{
MessagingCenter.Unsubscribe<IssueListPageModel>(this, "refresh");
await OnRefreshContent();
});
IssuePageModel page = null;
await Task.Run(() =>
{
UserDialogs.Instance.ShowLoading("Loading event...", maskType: MaskType.Clear);
// Does lost of stuff, So show loading message
UserDialogs.Instance.HideLoading();
});
if (item != null)
await CoreMethods.PushPageModel<IssuePageModel>(item);
}
}
public async void OnDelete(object sender, EventArgs e)
{
var mi = ((MenuItem)sender);
IssueModel issue = mi.BindingContext as IssueModel;
if (issue != null)
{
IsBusy = true;
bool bDeleted = await Task.Run(() =>
{
try
{
App.Client.DeleteIssue(issue.Id);
return Issues.Remove(issue);
}
catch (Exception /*ex*/)
{
return false;
}
});
if (bDeleted == false)
await CoreMethods.DisplayAlert("Failed", "Failed to remove item", "Continue");
IsBusy = false;
}
}
【问题讨论】:
-
我建议使用事件来命令行为。这里有一篇很好的文章:developer.xamarin.com/guides/xamarin-forms/…
-
我对它有点了解,但不知道如何应用它
标签: c# xamarin mvvm xamarin.forms command