在MvvmLight下使用{x:Bind}

One of the XAML-related announcements at Build this year was the availability of compiled bindings. These new bindings can be used instead of the “classic” {Binding} syntax in XAML. Note that this syntax is only available in Windows 10 universal applications, not in WPF yet.

The advantage of compiled bindings over normal bindings is that (wait for it) they are compiled. So there is syntax check by the compiler when you build your project. Also, the bindings will be resolved much faster because they do not rely on reflection during runtime.

For example, if you have the following property in a Windows 10 Universal application page:

public string TimeWhenLoadingPage
{
    get
    {
        return DateTime.Now.ToString("HH:mm:ss");
    }
}

Then you can consume this property in the XAML with:

<TextBlock HorizontalAlignment="Center"
           VerticalAlignment="Center"
           FontSize="18"
           Text="{x:Bind TimeWhenLoadingPage}" />

Using x:Bind with MVVM

Of course these bindings will react to the PropertyChanged event, just like normal bindings too. So you can use these in an MVVM app without issues. There are also the usual parameters you can use, such as Converter, ConverterParameter, Mode, etc.

The only parameter missing, however, is the Source one. ElementName is also missing from there, but for an MVVM app, Source is more annoying. So how can you do, for instance in an MVVM Light application?

In fact it’s not that hard: Since you can bind to a property of the Page, and since the x:Bind syntax supports access to nested properties, you just have to expose the ViewModel as a property of the Page. In general, we set the ViewModel as the DataContext of the Page, for example with:

<Page x:Class="App8.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    </Grid>
</Page>

Then we can expose the DataContext in the Page with:

public sealed partial class MainPage : Page
{
    public MainViewModel Vm
    {
        get
        {
            return (MainViewModel)DataContext;
        }
    }

    public MainPage()
    {
        InitializeComponent();
    }
}

(by the way, if you install the MVVM Light Visual Studio extension (VSIX) available on Codeplex, you get code snippets in Visual Studio. One of these has the shortcut mvvmvm and create the code show above, which is a convenient shortcut).

Once this property is exposed, we can use the properties within the ViewModel, for example with:

<TextBlock HorizontalAlignment="Center"
            VerticalAlignment="Center"
            FontSize="18"
            Text="{x:Bind Vm.Hello}" />

Hopefully this will help MVVM Light users to take advantage of these new bindings!

转自:这里

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值