所以当用户点击+和 - 图像按钮时,我得到了这个页面,TotalPrice标签绑定得到更新,没问题。这是项目模板绑定。但是,我需要每当产品增加或减少时,我需要更新TotalOrderValue。但是当listview中的某些东西得到更新时,我无法触发UpdateTotalOrderValue。我该怎么做呢?
我尝试了一些事件,但无法让它们工作,因为它无法向上传达。
这是来自PAGE视图模型的
public decimal? TotalOrderValue
{
get { return totalOrderValue; }
set
{
totalOrderValue = value;
OnPropertyChanged();
}
}
private void UpdateTotalOrderValue()
{
foreach (var product in Items)
{
TotalOrderValue += product.Price;
}
}
这是listview绑定的ProductModel / DataTemplate你可以在这里查看我想要做的评论。
public int ModelUpdateCount
{
get { return modelUpdateCount; }
set
{
if (value >= 0)
{
modelUpdateCount = value;
TotalPrice = modelUpdateCount * Price;
//ProductsPageViewModel.UpdateTotalOrderValu();
NotifyPropertyChanged();
}
}
}
public decimal? TotalPrice
{
get { return totalPrice; }
set
{
totalPrice = value;
NotifyPropertyChanged();
}
}
这是页面本身
<Grid HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="0,0,0,0" RowSpacing="5" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition Height="60" />
<RowDefinition Height="*" />
<RowDefinition Height="120" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.1*" />
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.3*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="0.1*"/>
</Grid.ColumnDefinitions>
<!--TOP LABELS-->
<!--LIST OF PRODUCTS IN THE SCROLLVIEW-->
<Label Margin="5" Grid.Row="1" Grid.Column="0" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="IMAGE" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="1" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="ARTICLE" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="2" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="EAN NUMBER" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="3" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="DESCRIPTION" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="4" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="SIZE" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="5" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="SRP" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="6" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="PRIZE" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="7" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="QUANTITY" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<Label Margin="5" Grid.Row="1" Grid.Column="8" BackgroundColor="LightGray" HorizontalOptions="Fill" Text="TOTAL" Style="{StaticResource PanelText}" TextColor="Black" FontSize="20" VerticalOptions="Fill" />
<ListView x:Name="lvItemsList"
ItemsSource="{Binding GroupedItems}"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}"
HasUnevenRows="True"
Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="9" HorizontalOptions="FillAndExpand" Margin="5"
>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell Height="30">
<Frame BorderColor="Gray" Padding="5">
<Label Text="{Binding Key}" TextColor="Black" Font="Large" VerticalTextAlignment="Center" VerticalOptions="Center"/>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="60">
<Grid FlexLayout.Basis="100%" HeightRequest="60" Margin="5" RowSpacing="5" ColumnSpacing="10" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.1*" />
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.15*"/>
<ColumnDefinition Width="0.3*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="0.1*"/>
</Grid.ColumnDefinitions>
<ffimageloading:CachedImage
Source="{Binding LocalImagePath}" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" Aspect="AspectFill"
DownsampleToViewSize="true"
DownsampleHeight="180"
DownsampleWidth="120"
>
<ffimageloading:CachedImage.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=ViewModel.CmdImageTapped, Source={x:Reference Products}}"
CommandParameter="{Binding .}"
NumberOfTapsRequired="1"/>
</ffimageloading:CachedImage.GestureRecognizers>
</ffimageloading:CachedImage>
<Label VerticalOptions="CenterAndExpand" Grid.Column="1" Text="{Binding SkuNumber}" Style="{StaticResource PanelText}" HorizontalTextAlignment="Start" TextColor="Black"/>
<Label VerticalOptions="CenterAndExpand" Grid.Column="2" Text="{Binding Ean13}" Style="{StaticResource PanelText}" HorizontalTextAlignment="Center" TextColor="Black"/>
<Label VerticalOptions="CenterAndExpand" Grid.Column="3" Text="{Binding ProductName}" Style="{StaticResource PanelText}" HorizontalTextAlignment="Start" TextColor="Black" FontAttributes="Bold" />
<Label VerticalOptions="CenterAndExpand" Grid.Column="4" Text="{Binding Size}" Style="{StaticResource PanelText}" HorizontalTextAlignment="Center" TextColor="Black"/>
<Label HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="5" Text="{Binding SalesRecommendedPrice}" Style="{StaticResource PanelText}" HorizontalTextAlignment="Center" TextColor="Black"/>
<Label HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" Grid.Column="6" Text="{Binding PriceWithUnit}" Style="{StaticResource PanelText}" HorizontalTextAlignment="Center" TextColor="Black" FontAttributes="Bold"/>
<Grid VerticalOptions="CenterAndExpand" Grid.Column="7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Image="line_in_square.png" BackgroundColor="White" HorizontalOptions="Start" VerticalOptions="Start" Command="{Binding DeCrementCommand}" />
<Frame Grid.Column="1" BorderColor="Gray" HorizontalOptions="Fill" VerticalOptions="Center" Padding="8" >
<Label Text="{Binding ModelUpdateCount, StringFormat='{0}'}" FontSize="16" TextColor="Black" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"/>
</Frame>
<Button BackgroundColor="White" Grid.Column="2" Image="plus_sign.png" HorizontalOptions="End" VerticalOptions="Start" Command="{Binding IncrementCommand}" />
</Grid>
<Label VerticalOptions="CenterAndExpand" Grid.Column="8" Text="{Binding TotalPrice, StringFormat='{0:C2}'}" Style="{StaticResource PanelText}" FontAttributes="Bold" TextColor="Black"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- Bottom Command Bar -->
<Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="9" BackgroundColor="LightGray" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="5">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="360"/>
</Grid.ColumnDefinitions>
<Label Text="COMMENTS
/ PO NUMBER:" BackgroundColor="LightGray" VerticalOptions="Center" HorizontalOptions="Center" TextColor="Black" />
<Editor BackgroundColor="White" TextColor="Black" Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="10,10,10,10" />
<Label Grid.Row="0" Grid.Column="2" VerticalOptions="End" HorizontalOptions="End" Text="£ 221212" Margin="0,0,20,0" FontSize="16"/>
<Label Grid.Row="0" Grid.Column="2" VerticalOptions="Center" HorizontalOptions="Start" FontSize="40" Text="{Binding TotalOrderValue}" Margin="35,0,0,0"/>
<Button Text="SUBMIT PART ORDER" Command="{Binding SubmitOrderCommand}" BackgroundColor="#088da5" FontAttributes="Bold" TextColor="White" Grid.Row="1" Grid.Column="2" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="0,0,20,10"/>
如果需要,可视图像:
https://gyazo.com/80f64e4146b6a661f983938d1fb5d1bf
需要更新的0才是listview中所有产品的总和。
我的建议是在PageViewModel中从ProductModel添加一个PropertyChanged事件的监听器:
public void AttachListener()
{
foreach (var product in Items)
{
production.PropertyChanged += OnOrderCountUpdate;
}
}
private void OnOrderCountUpdate(object sender, PropertyChangedEventArgs e)
{
if(e.PropertyName == "ModelUpdateCount")
UpdateTotalOrderValue();
}
您应该在页面的OnAppearing()中附加侦听器,并在OnDisappearing()中将其删除
您可以像这样创建一个新类DataSource
:
public class PageVMDataSource
{
#region Attributes
private readonly PageVM pageVM;
private readonly ProductModel productModel;
#endregion
#region Public Methods
public PageVMDataSource(PageVM pageVM, ProductModel productModel)
{
this.pageVM = pageVM;
this.productModel= productModel;
}
public void Initialize()
{
this.productModel.PropertyChanged += this.OnProductModelPropertyChanged;
}
#endregion
#region Event Handlers
private void OnProductModelPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
switch (propertyChangedEventArgs.PropertyName)
{
case "ModelUpdateCount":
this.pageVM.UpdateTotalOrderValue;
break;
}
}
#endregion
}
创建VM和模型时,请创建此DataSource
的新实例,以便订阅模型更改并执行VM方法。
通过这样做,您正在应用SOLID,您可以控制所有需要“通信”的东西