c# - 如何实时更新文本块?
问题描述
我正在使用 MVVM Light 框架开发项目。
我有 MainViewModel 可以帮助我在视图模型之间导航。我有 GoBack 和 GoTo 方法。他们正在改变 CurrentViewModel。
private RelayCommand<string> _goTo;
public RelayCommand<string> GoTo
{
get
{
return _goTo
?? (_goTo = new RelayCommand<string>(view
=>
{
SwitchView(view);
}));
}
}
private void SwitchView(string name)
{
switch (name)
{
case "login":
User = null;
CurrentViewModel = new LoginViewModel();
break;
case "menu":
CurrentViewModel = new MenuViewModel();
break;
case "order":
CurrentViewModel = new OrderViewModel();
break;
}
在 MainWindow 中有内容控件和数据模板。
[...]
<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<view:Login/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MenuViewModel}">
<view:Menu/>
</DataTemplate>
[...]
<ContentControl VerticalAlignment="Top" HorizontalAlignment="Stretch"
Content="{Binding CurrentViewModel}" IsTabStop="false"/>
在我的 OrderView(它是 UserControl)中,我有文本块,它应该显示订单的 TotalPrice。
<TextBlock Text="{Binding AddOrderView.TotalPrice}" Padding="0 2 0 0" FontSize="20" FontWeight="Bold" HorizontalAlignment="Right"/>
OrderViewModel 有一个属性 TotalPrice 并且效果很好。当我调试时,我看到它已被更改,但在我的视图中没有发生任何事情。
private decimal _totalPrice;
public decimal TotalPrice
{
get
{
_totalPrice = 0;
foreach (var item in Products)
{
item.total_price = item.amount * item.price;
_totalPrice += item.price * item.amount;
}
return _totalPrice;
}
set
{
if (_totalPrice == value)
return;
_totalPrice = value;
RaisePropertyChanged("TotalPrice");
}
}
OrderViewModel 继承自 BaseViewModel 并实现 INotifyPropertyChanged。
为什么我的文本块没有更新/刷新?这个怎么做?
当我使用后退按钮更改视图并再次转到 OrderView 时,我看到了更改!
我花了几天时间寻找解决方案,但没有任何帮助。
https://i.stack.imgur.com/K8lip.gif
所以看起来当 View 正在设置时,如果不重新加载它就无法更改它。我不知道它是如何工作的。
解决方案
您不应该在属性的 getter 或 setter 中进行计算或任何耗时的操作。这会大大降低性能。如果计算或操作很耗时,那么您应该在后台线程中执行它并在完成后引发PropertyChanged
事件Task
。这样调用属性的 getter 或 setter 就不会冻结 UI。
您观察到的行为的解释:在自己的getter
而不是 setter
中更改属性值的副作用是新值不会传播到绑定目标。getter 仅在事件发生时由绑定调用。因此,在 getter 中进行计算不会触发绑定刷新。现在在重新加载页面时,所有绑定都会初始化绑定目标,因此会调用属性 getter。PropertyChanged
您必须设置TotalPrice
属性(而不是支持字段)才能触发绑定目标的刷新。但是正如您已经亲身经历的那样,PropertyChanged
在同一个 getter 中引发属性事件将导致无限循环,因此会导致StackOverflowException
.
此外,每当访问属性的 getter 时,总是会执行计算——即使TotalPrice
没有改变。
的价值TotalPrice
取决于Products
财产。为了最小化 的计算的发生TotalPrice
,只计算何时Products
发生了变化:
OrderViewModel.cs
public class OrderViewModel : ViewModelBase
{
private decimal _totalPrice;
public decimal TotalPrice
{
get => this._totalPrice;
set
{
if (this._totalPrice == value)
return;
this._totalPrice = value;
RaisePropertyChanged();
}
}
private ObservableCollection<Product> _products;
public ObservableCollection<Product> Products
{
get => this._products;
set
{
if (this.Products == value)
return;
if (this.Products != null)
{
this.Products.CollectionChanged -= OnCollectionChanged;
UnsubscribeFromItemsPropertyChanged(this.Products);
}
this._products = value;
this.Products.CollectionChanged += OnCollectionChanged;
if (this.Products.Any())
{
SubscribeToItemsPropertyChanged(this.Products);
}
RaisePropertyChanged();
}
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (!e.Action.Equals(NotifyCollectionChangedAction.Move))
{
UnsubscribeFromItemsPropertyChanged(e.OldItems);
SubscribeToItemsPropertyChanged(e.NewItems);
}
CalculateTotalPrice();
}
private void ProductChanged(object sender, PropertyChangedEventArgs e) => CalculateTotalPrice();
private void SubscribeToItemsPropertyChanged(IList newItems) => newItems?.OfType<INotifyPropertyChanged>().ToList().ForEach((item => item.PropertyChanged += ProductChanged));
private void UnsubscribeFromItemsPropertyChanged(IEnumerable oldItems) => oldItems?.OfType<INotifyPropertyChanged>().ToList().ForEach((item => item.PropertyChanged -= ProductChanged));
private void CalculateTotalPrice() => this.TotalPrice = this.Products.Sum(item => item.total_price);
private void GetProducts()
{
using (var context = new mainEntities())
{
var result = context.product.Include(c => c.brand);
this.Products = new ObservableCollection<Product>(
result.Select(item => new Product(item.name, item.mass, item.ean, item.brand.name, item.price)));
}
}
public void ResetOrder()
{
this.Products
.ToList()
.ForEach(product => product.Reset());
this.TotalPrice = 0;
}
public OrderViewModel()
{
SetView("Dodaj zamówienie");
GetProducts();
}
}
还要确保Product
(Products
集合中的项目)INotifyPropertyChanged
也实现。这将确保在属性更改Products.CollectionChanged
时引发事件。Product
要修复页面切换行为,您必须修改MainViewModel
类:
主视图模型.cs
public class MainViewModel : ViewModelBase
{
// The page viewmodels
private Dictionary<string, ViewModelBase> PageViewModels { get; set; }
public Stack<string> ViewsQueue;
public MainViewModel()
{
User = new User(1, "login", "name", "surname", 1, 1, 1);
this.PageViewModels = new Dictionary<string, ViewModelBase>()
{
{"login", new LoginViewModel()},
{"menu", new MenuViewModel()},
{"order", new OrderViewModel()},
{"clients", new ClientsViewModel(User)}
};
this.CurrentViewModel = this.PageViewModels["login"];
this.ViewsQueue = new Stack<string>();
this.ViewsQueue.Push("login");
Messenger.Default.Register<NavigateTo>(
this,
(message) =>
{
try
{
ViewsQueue.Push(message.Name);
if (message.user != null) User = message.user;
SwitchView(message.Name);
}
catch (System.InvalidOperationException e)
{
}
});
Messenger.Default.Register<GoBack>(
this,
(message) =>
{
try
{
ViewsQueue.Pop();
SwitchView(ViewsQueue.Peek());
}
catch (System.InvalidOperationException e)
{
}
});
}
public RelayCommand<string> GoTo => new RelayCommand<string>(
viewName =>
{
ViewsQueue.Push(viewName);
SwitchView(viewName);
});
protected void SwitchView(string name)
{
if (this.PageViewModels.TryGetValue(name, out ViewModelBase nextPageViewModel))
{
if (nextPageViewModel is OrderViewModel orderViewModel)
orderViewModel.ResetOrder();
this.CurrentViewModel = nextPageViewModel;
}
}
}
您修改后的 Product.cs
public class Product : ViewModelBase
{
public long id { get; set; }
public string name { get; set; }
public decimal mass { get; set; }
public long ean { get; set; }
public long brand_id { get; set; }
public string img_source { get; set; }
public string brand_name { get; set; }
private decimal _price;
public decimal price
{
get => this._price;
set
{
if (this._price == value)
return;
this._price = value;
OnPriceChanged();
RaisePropertyChanged();
}
}
private long _amount;
public long amount
{
get => this._amount;
set
{
if (this._amount == value)
return;
this._amount = value;
OnAmountChanged();
RaisePropertyChanged();
}
}
private decimal _total_price;
public decimal total_price
{
get => this._total_price;
set
{
if (this._total_price == value)
return;
this._total_price = value;
RaisePropertyChanged();
}
}
public Product(long id, string name, decimal mass, long ean, long brandId, decimal price, string imgSource)
{
this.id = id;
this.name = name;
this.mass = mass;
this.ean = ean;
this.brand_id = brandId;
this.price = price;
this.img_source = imgSource;
}
public Product(string name, decimal mass, long ean, string brandName, decimal price)
{
this.id = this.id;
this.name = name;
this.mass = mass;
this.ean = ean;
this.brand_name = brandName;
this.price = price;
}
public void Reset()
{
// Resetting the `amount` will trigger recalculation of `total_price`
this.amount = 0;
}
protected virtual void OnAmountChanged()
{
CalculateTotalPrice();
}
private void OnPriceChanged()
{
CalculateTotalPrice();
}
private void CalculateTotalPrice()
{
this.total_price = this.price * this.amount;
}
}
问题是您在切换到页面时总是创建一个新的视图模型。当然,所有以前的页面信息都会丢失。您必须重用相同的视图模型实例。为此,只需将它们存储在您在构造函数中初始化一次的专用私有属性中。
推荐阅读
- c++ - C ++在同一行中的两个用户输入关键字加上单独的检查
- c# - 如何修复 ArgumentException:JSON 必须表示对象类型
- mysql - 如何为此要求编写 mysql 查询?
- go - 错误的类型接口实现
- android - 如何以编程方式控制 React Native 中的 Youtube 嵌入视频质量
- php - 只有三星手机没有收到来自 fcm 的通知
- javascript - 如何使 Bootstrap 日期和时间选择器在顶部打开而不是打开
- ionic4 - 我们可以在移动设备和台式机上使用signle ionic-4 应用吗
- github - 如何安装宝石?
- spring - spring boot restTempalte call (get Data) with security handel