【发布时间】:2015-03-04 09:13:27
【问题描述】:
我有一个加载一些数据的视图模型。加载 DependencyProperty 时设置为 true,稍后再次设置为 false。我经常在 WPF 中这样做,但是对于现代应用程序它不起作用。 :(
绑定到模型的视图应该显示模型是否仍在加载。它只是在页面加载时显示值,但在值更改后不会更新视图。
我写了一个小例子,它使用 SharedProjects for Phone、Modern App 和 WPF。 (BindingExample)
出于测试目的,它只是在真假之间切换。
XAML
<Page
x:Class="App2.BlankPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid >
<TextBlock Text="{Binding Loading, Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Width="100" Height="100"/>
</Grid>
</Page>
页面的构造函数
public BlankPage1()
{
var v = new Viewmodle();
this.InitializeComponent();
this.DataContext = v;
v.Test();
}
视图模型:
public class Viewmodle : DependencyObject
{
public bool Loading
{
get { return (bool)GetValue(LoadingProperty); }
set { SetValue(LoadingProperty, value); }
}
// Using a DependencyProperty as the backing store for Loading. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LoadingProperty =
DependencyProperty.Register("Loading", typeof(bool), typeof(Viewmodle), new PropertyMetadata(false));
public async void Test()
{
while (true)
{
await System.Threading.Tasks.Task.Delay(1000);
this.Loading = true;
await System.Threading.Tasks.Task.Delay(1000);
this.Loading = false;
}
}
}
【问题讨论】:
标签: c# xaml binding windows-runtime winrt-xaml