鑒于以下內容ListView,我想要一個命令將單擊的物件(在本例中為地址)發送回視圖模型中的命令 -SelectNewAddress或DeleteAddress.
<StackLayout VerticalOptions="FillAndExpand" Padding="10,15,10,15">
<Label Text="Addresses" FontSize="22" HorizontalTextAlignment="Center" FontAttributes="Bold" Padding="0,0,0,7" TextColor="#404040" />
<StackLayout VerticalOptions="FillAndExpand">
<flv:FlowListView FlowColumnCount="1"
HeightRequest="200"
SeparatorVisibility="None"
HasUnevenRows="True"
FlowItemsSource="{Binding AllAddresses}">
<flv:FlowListView.FlowColumnTemplate>
<DataTemplate x:DataType="popups:AddressItem">
<Grid ColumnDefinitions="*,35" Padding="0,0,0,15" x:Name="Item">
<Grid Grid.Column="0">
<Grid.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding SelectNewAddress}" />
</Grid.GestureRecognizers>
<Label Text="{Binding MainAddress}"
LineBreakMode="TailTruncation"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
FontSize="18"
TextColor="{StaticResource CommonBlack}"/>
</Grid>
<Grid Grid.Column="1" IsVisible="{Binding IsSelected}" >
<Grid.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding SelectNewAddress}"/>
</Grid.GestureRecognizers>
<StackLayout Padding="10,0,0,0">
<flex:FlexButton Icon="check.png"
WidthRequest="25"
HeightRequest="25"
CornerRadius="18"
BackgroundColor="{StaticResource Primary}"
ForegroundColor="{StaticResource CommonWhite}"
HighlightBackgroundColor="{StaticResource PrimaryDark}"
HighlightForegroundColor="{StaticResource CommonWhite}"/>
</StackLayout>
</Grid>
<Grid Grid.Column="1" IsVisible="{Binding IsSelected, Converter={StaticResource invertBoolConverter}}">
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding DeleteAddress} />
</Grid.GestureRecognizers>
<StackLayout Padding="10,0,0,0">
<flex:FlexButton Icon="deleteCard.png"
WidthRequest="25"
HeightRequest="25"
CornerRadius="18"
BackgroundColor="{StaticResource WooopDarkGray}"
ForegroundColor="{StaticResource CommonWhite}"
HighlightBackgroundColor="{StaticResource PrimaryDark}"
HighlightForegroundColor="{StaticResource CommonWhite}"/>
</StackLayout>
</Grid>
</Grid>
</DataTemplate>
</flv:FlowListView.FlowColumnTemplate>
</flv:FlowListView>
</StackLayout>
視圖模型中的命令如下:
...
public ICommand SelectNewAddress { get; set; }
public ICommand DeleteAddress { get; set; }
...
public AddressSelectionViewModel()
{
DeleteAddress = new Command(DeleteAddressCommand);
SelectNewAddress = new Command(SelectNewAddressCommand);
}
...
private void SelectNewAddressCommand(object obj)
{
try
{
var item = (AddressItem)obj;
AddressHelper.UpdateAddress(item.DeliveryAddressLocation);
UpdateAddresses();
}
catch (Exception ex)
{
// TODO
}
}
private void DeleteAddressCommand(object obj)
{
try
{
var item = (AddressItem)obj;
AddressHelper.RemoveAddress(item.DeliveryAddressLocation);
UpdateAddresses();
}
catch (Exception ex)
{
// TODO
}
}
我希望object obj傳遞給SelectNewAddressCommand并 DeleteAddressCommand成為點擊的地址ListView
uj5u.com熱心網友回復:
首先確保您已將您的視圖模型包含DataType在Class以下內容中ContentPage:
xmlns:pages="clr-namespace:your.namespace.ViewModels"
x:DataType="pages:AddressSelectionViewModel"
x:Class="your.namespace.Views.AddressSelectionPage"
<ContentPage xmlns="..."
xmlns:x="..."
xmlns:flv="..."
xmlns:popups="..."
xmlns:flex="..."
xmlns:views="..."
xmlns:xct="..."
xmlns:pages="clr-namespace:your.namespace.ViewModels"
x:DataType="pages:AddressSelectionViewModel"
x:Class="your.namespace.Views.AddressSelectionPage"
Shell.FlyoutItemIsVisible="..."
Shell.NavBarIsVisible="..."
Shell.TabBarIsVisible="...">
在頂部Grid元素內部添加屬性x:Name="Item"(“Item”僅用作示例,您可以將其命名為任何名稱):
<flv:FlowListView FlowColumnCount="1"
HeightRequest="200"
SeparatorVisibility="None"
HasUnevenRows="True"
FlowItemsSource="{Binding AllAddresses}">
<flv:FlowListView.FlowColumnTemplate>
<DataTemplate x:DataType="popups:AddressItem">
<Grid ColumnDefinitions="*,35" Padding="0,0,0,15" x:Name="Item"> <!-- Here -->
然后我們將Commandand更改CommandParameter為TapGestureRecognizer以下內容:
<TapGestureRecognizer
Command="{Binding Path=SelectNewAddress, Source={RelativeSource AncestorType={x:Type pages:AddressSelectionViewModel}}}"
CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}" />
<TapGestureRecognizer
Command="{Binding Path=DeleteAddress, Source={RelativeSource AncestorType={x:Type pages:AddressSelectionViewModel}}}"
CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}" />
在Command我們將函式指定為Path,然后我們通過 闡明這個函式的來源是在視圖模型內部AncestoryType。當在串列視圖中時,我們不能參考被迭代物件之外的屬性。因此,我們需要指定所需的來源。
所以現在我們正在參考實際的函式。但是我們還沒有發送object obj作為引數。
在 中,我們必須用和CommandParameter傳遞當前系結的物件。請注意,在 Source 中,我們參考的名稱是我們之前定義的。PathSourceItemx:NameGrid
uj5u.com熱心網友回復:
確保頁面將視圖模型作為其 BindingContext。(如果你在做 mvvm,你已經做過了。)
給
<flv:FlowListView一個名字:
<flv:FlowListView x:Name="theListView" ... >
- 該專案需要參考頁面視圖模型中的命令。BindingContext 通過層次結構向下傳播,因此相對于串列視圖名稱可以輕松完成:
<flv:FlowListView.FlowColumnTemplate>
<DataTemplate>
...
<TapGestureRecognizer
Command="{Binding BindingContext.SelectNewAddress, Source={x:Reference theListView}}" ...
- 專案的 BindingContext 是專案模型,因此很容易作為引數傳遞:
<TapGestureRecognizer
Command=... CommandParameter="{Binding .}" />
注意:這與向導的答案之間的差異:
{Binding .}僅需要參考專案本身即可。- 而不是使用
RelativeSource需要指定 a 的Type,我個人的偏好是命名一個視圖,然后參考該名稱。我發現這更容易閱讀并記住如何做。 - 我省略了與問題無關的所有細節。以上步驟就足夠了。(
x:DataType命令對性能有好處,所以我絕不建議不要這樣做。但這是一個單獨的主題,恕我直言。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/481180.html
標籤:xml xamarin xamarin.forms 虚拟机
上一篇:XamariniOSBuildSystem.IO.FileNotFoundException:找不到檔案“../obj/iPhone/Dev/actool/bundle/my20x20.png”
