因此,当用户单击+和-图像按钮时,我得到了此页面,TotalPrice标签绑定得到更新,没问题。这在项目模板绑定中。但是,无论何时增加或减少产品,我都需要TotalOrderValue也需要更新。但是当列表视图中的某些内容被更新时,我无法触发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是列表视图中所有产品的总数。
答案 0 :(得分:1)
我的建议是从PageViewModel中的ProductModel中向PropertyChanged事件添加一个侦听器:
powers_to_remove = [2**i for i in range(int(math.ceil(math.log(len(arr), 2))))]
result = [arr[i] for i in range(len(arr)) if i+1 not in powers_to_remove]
您应该将侦听器附加到页面的OnAppearing()中,然后将其删除到OnDisappearing()
答案 1 :(得分:1)
您可以像这样创建新的类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,并且可以控制所有需要“通信”的内容