我正在.NET MAUI 中開發一個應用程式,我有一個帶有集合視圖的頁面,該頁面加載了資料。我將資料顯示到集合視圖子項上。但是后來我遇到了一個問題,我有一個步進器,我希望當我更改步進器值時,我命名為 priceLabel 的標簽也以這種格式更改(priceLabelValue = 新步進器值 * 當前顯示的集合視圖專案的價格)。我在集合視圖之外還有另一個標簽,我想在其上顯示集合視圖中所有專案的總價格。
我嘗試了所有我認為的方法,但都是徒勞的,我遇到的一個問題是我無法從后面的代碼訪問我在集合視圖中創建的視圖。
當我嘗試使用視圖模型更新這些子視圖時,我還發現我無法從視圖模型訪問當前集合視圖項。
提示:我正在為產品開發一個購物車頁面......希望這也會有所幫助
這是我的代碼。
// xml代碼
<CollectionView x:Name="collcn"
ItemsSource="{Binding ShoppingBagCollection}"
Grid.Row="1"
Grid.ColumnSpan="2"
EmptyView="NO ITEMS ADDED">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="model:Cloth">
<Grid Padding="0"
Grid.ColumnDefinitions="*,*"
Grid.RowDefinitions="*,*,*"
ColumnSpacing="10"
HeightRequest="160"
HorizontalOptions="FillAndExpand"
Margin="20,20,0,20">
<Frame Grid.Column="0"
Grid.RowSpan="3"
Style="{StaticResource CardView}"
Margin="30"
HorizontalOptions="Start"
IsClippedToBounds="True">
<Image Source="{Binding Image}"
Aspect="Fill"
HeightRequest="100"
WidthRequest="100"/>
</Frame>
<VerticalStackLayout Grid.Column="1"
Grid.RowSpan="3"
Spacing="5"
HorizontalOptions="Start"
Margin="0,20,20,0">
<Label Text="{Binding Name}"
Style="{StaticResource MediumLabel}"
Grid.Row="0"/>
<!-- This label displays current value of stepper-->
<Label x:Name="stepperValue"
Style="{StaticResource MediumLabel}"
Text="{Binding Source={x:Reference stepper}, Path=Value}"/>
<Stepper x:Name="stepper"
Grid.Row="1"
Minimum="1"
Maximum="{Binding Quantity}"
Increment="1"/>
<!-- This label should display current value of stepper * Price of current Item-->
<Label x:Name="price"
Style="{StaticResource MediumLabel}"
Grid.Row="2"
Text="{Binding Price}"/>
</VerticalStackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Grid
Grid.ColumnSpan="2"
Grid.Row="2"
Padding="0"
Grid.ColumnDefinitions="*,*"
Grid.RowDefinitions="*,*,*,*"
ColumnSpacing="0"
HorizontalOptions="FillAndExpand"
Margin="20">
<VerticalStackLayout
Grid.RowSpan="4"
Grid.Column="0">
<!-- This entry should display total price of all items in collection view but i have failed to get a walk around-->
<Entry x:Name="totalcostLabel"
Grid.Row="2"
Placeholder="$0.00"
Margin="0,30,0,0"
Text="{Binding Source={x:Reference collcn}}"
WidthRequest="120"
HorizontalOptions="Start"
IsReadOnly="True"
Style="{StaticResource MediumLabel}"/>
<Label Grid.Row="2"
Text="TotalCost"
Margin="0"/>
</VerticalStackLayout>
// 視圖模型我認為沒有必要在后面發布視圖模型和代碼,因為在嘗試了這么多不同的方式之后我沒有添加任何特殊的邏輯。
任何人都可以幫助我,并告訴我如何才能做到這一點,非常感謝。
uj5u.com熱心網友回復:
我已嘗試根據您的代碼重現您的問題,并找到了解決方案,盡管可能還有其他解決方案。試試下面的代碼:
首先,在您的 .xaml 檔案中,為您的步進器添加一個 ValueChanged 事件,即stepper_ValueChanged,該事件將在步進器的值更改時觸發。
<Stepper x:Name="stepper"
Grid.Row="1"
Minimum="1"
Maximum="{Binding Quantity}"
Increment="1"
ValueChanged="stepper_ValueChanged"/>
然后,在 .cs 檔案中,我們生成一個stepper_ValueChanged(或由 vs 自動生成)。
MainPageViewModel vm;
public MainPage()
{
InitializeComponent();
vm = new MainPageViewModel();
this.BindingContext = vm;
}
private void stepper_ValueChanged(object sender, ValueChangedEventArgs e)
{
double pricetotal = 0;
// The BindingContext of each stepper is Cloth instance in ObservableCollection.
//So here we go through every instance to find which one this stepper is related to.
var stepper = (Stepper)sender;
foreach (Cloth cloth in vm.ShoppingBagCollection)
{
if(cloth == stepper.BindingContext)
{
cloth.Price = cloth.Price / e.OldValue * e.NewValue;
}
pricetotal = cloth.Price;
}
vm.PriceTotal = pricetotal; // // the total price shown in the entry, we will define it later in ViewModel. It also changes while stepper changes.
}
在您的模型布中,也進行一些更改。您應該實作 INotifyPropertyChanged 和 PropertyChanged 事件。有關 INotifyPropertyChanged 的??更多資訊,您可以參考ViewModels 和 Property-Change Notifications
public class Cloth : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public double _price;
public double Price
{
get
{
return _price;
}
set
{
_price = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Price)));
}
}
......
}
最后,在 ViewModel 中,定義條目系結的新屬性PriceTotal并實作 INotifyPropertyChanged。
public class MainPageViewModel :INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private double _priceTotal;
public double PriceTotal
{
get
{
return _priceTotal;
}
set
{
_priceTotal = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PriceTotal)));
}
}
.......
}
這是我的解決方案的主要結構,盡管我知道應該有更多細節。
希望我的回答能幫到你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533325.html
標籤:C#。网xml毛伊岛
