UWP TextBlock 在绑定到函数时不会更新

UWP TextBlock does not update when bound to function

我们正在使用带有 Template10 的 UWP。 Template10.ViewModelBase 管理更改通知。我们有一个 CostTextBlock 绑定到 ViewModel.Cost。使用转换器 CostTextBlockViewModel.Cost 更新时更新。当我们绑定到一个函数时,Cost 以正确的格式呈现,但不会更新。 在 ViewModel 中我们有:

public class ViewModel : ViewModelBase
{

    decimal? _Cost = default(decimal?);
    public decimal? Cost
    {
        get
        {
            return _Cost;
        }
        set
        {
            if (value == 0) value = null;
            Set(ref _Cost, value);
        }
    }

我们在 ViewModel 的其他地方更新成本:

this.Cost = null;

在App.xaml中我们定义转换器:

<T10Converters:StringFormatConverter x:Key="PriceConverter" Format="{}{0:N2}"/>

在视图中:

Text="{x:Bind ViewModel.Cost,Mode=OneWay, Converter={StaticResource PriceConverter}}"/>

我们可以加载带有订单的视图,并且 Cost 可以正确呈现。使用转换器,当 Cost 设置为 null 时,更改会反映在视图中。

我们还有一个方法和转换器一样:

public static string FormatPrice(decimal? price)
{
    if (price == null)
        return null;
    return ((decimal)price).ToString("N2");
}

在这种情况下,视图中的 xaml 是

Text="{x:Bind Helpers:Globalisation.FormatPrice(ViewModel.Cost),Mode=OneWay}"

这在视图中正确地设置了 Cost 的格式,但是与转换器一起使用的相同代码 this.Cost = null; 不会更新视图,即使 Cost 已更新。

为什么 CostTextBlock 在绑定到 FormatPrice 时不反映对 ViewModel.Cost 的更新?

这个问题发生在一些从视图模型更新的属性中。我设法使用 RaisePropertyChanged(nameof(<property>));

解决了这个问题

ViewModelBase 旨在处理更改通知,它似乎在控件绑定到属性的所有情况下都这样做。如果有人能想出更好的解决方案或解释为什么在控件绑定到函数的这少数情况下更改通知没有触发,我会很乐意将它们标记为答案。