当您说绑定 Run 控件的 Text 属性“不起作用”时,您确定绑定到正确的对象吗?(我还包括一个明显的问题,即您的属性是否在视图模型中正确声明,但我假设您已经检查并确保了足够范围的 getter/setter)。我在几个应用程序中广泛使用这种方法,并且没有遇到任何问题。您是否在输出窗口中遇到任何可能有助于缩小原因的绑定错误?
我建议在您完全确定您不能对视图执行您想要的操作之前不要向您的视图模型添加属性 - 如果稍后需求发生变化并且您想要(例如)应用前景色变换器怎么办价值观之一?
编辑
我刚刚整理了一个简单的演示项目,展示了它的工作原理。在 VS -> 文件 -> 新建 -> 项目 -> 空白应用程序 (XAML) 中编辑 MainPage.xaml 和 MainPage.Xaml.cs。文件的全文如下...
主页.Xaml
<Page
x:Class="RunDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:RunDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<!-- added from here... -->
<TextBlock FontSize="36" HorizontalAlignment="Center" VerticalAlignment="Center">
<Run Text="{Binding Usage}"/>
<Run Text=" / "/>
<Run Text="{Binding Total}"/>
</TextBlock>
<!-- ...to here. -->
</Grid>
</Page>
主页,xaml.cs
using System.ComponentModel;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace RunDemo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
DataContext = new DummyViewModel();
}
}
public class DummyViewModel : INotifyPropertyChanged
{
private int total = 15;
private string usage = "ten";
public int Total
{
get
{
return total;
}
set
{
total = value;
OnPropertyChanged("Total");
}
}
public string Usage
{
get
{
return usage;
}
set
{
usage = value;
OnPropertyChanged("Usage");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if(null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}