[Some information relates to pre-released product which may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.]
[涉及某信息預(yù)發(fā)布的版本可能在它的商業(yè)版本大幅修改。對(duì)于這里提供的信息,微軟不作任何擔(dān)保。]
在MSDN中,Windows 10 SDK 的東東上,都會(huì)聲明這一句話,我也引過來吧啦,他不擔(dān)保,我也保不了。
正文
在Win10 UWP開發(fā)中,新加入了一個(gè)關(guān)鍵字 x:Bind. 它好在哪?為什么要用他。
build大會(huì)視頻資源:http://www.microsoftvirtualacademy.com/training-courses/a-developers-guide-to-windows-10-preview?prid=ch9courselink
一、x:Bind 好在哪?


從這兩張畫可以看出來,x:Bind的性能要優(yōu)于Binding。為什么?
這個(gè)綁定被稱為 "compiled data bindings", 從字面上看 編譯的數(shù)據(jù)綁定。
我們以前常用的Binding,是運(yùn)行時(shí)(Run Time)的綁定,而這個(gè) 在編譯時(shí)(Build Time)就已經(jīng)決定。
好處就是效率高,速度快,綁定的錯(cuò)誤在編譯時(shí)就會(huì)提示出來,方便調(diào)試。
二、x:Bind
x:Bind 主要的幾點(diǎn):
1. 強(qiáng)類型
2.上下文為page 或 UserControl
3.綁定的默認(rèn)Mode為OneTime
我們用Binding的時(shí)候,有的時(shí)候可以不用去考慮類型,因?yàn)橛泻枚嗄J(rèn)的轉(zhuǎn)換(如,TypeConverter),但是使用x:Bind時(shí),如果類型不匹配編譯時(shí),就會(huì)出錯(cuò)。
例如:
<CheckBox IsChecked="{x:Bind}" />
我們把當(dāng)前的上下文綁定到 IsChecked(bool?) 上
Invalid binding path '' : Cannot bind type 'BindDemo.MainPage' to 'System.Nullable(System.Boolean)' without a converter

結(jié)果就會(huì)這樣子。當(dāng)然,如果你綁到String類型的屬性上是不會(huì)錯(cuò)的,他會(huì)幫你ToString()地。
而綁定的上下文,現(xiàn)在看來就是當(dāng)前的類的本身了,就是繼承自Page和UserControl的本身。
對(duì)于在初學(xué)的同學(xué),再也不怕找不到對(duì)象了,也不用設(shè)置DataContext。
對(duì)于熟悉MVVM的同學(xué),要用x:Bind 就要把ViewModel寫到CodeBehand中一個(gè),真心怪怪的說,好多框架可能也得調(diào)整啦。
三、小試牛刀
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="{x:Bind}" VerticalAlignment="Center" HorizontalAlignment="Center" /> </Grid>
CodeBehand不做任何代碼啦.
結(jié)果:
從這結(jié)果也可以看出來,他的上下文,是當(dāng)前的這個(gè)頁。
咱們?cè)贑odeBehand里加一Title的屬性。
public sealed partial class MainPage : Page { public string Title { get; set; } = "Bind Demo"; public MainPage() { this.InitializeComponent(); } }
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="{x:Bind Title}" VerticalAlignment="Center" HorizontalAlignment="Center" /> </Grid>

這里,熟悉MVVM的人就要想了,我們?cè)趺唇壎╒iewModel呢。其實(shí)就是把ViewModel的對(duì)象,寫在CodeBehand里一個(gè)就好啦。
public class MainViewModel { public string Name { get; set; } = "Bind"; public string Content { get; set; } = "Demo"; }
public sealed partial class MainPage : Page { public string Title { get; set; } = "Bind Demo"; public MainViewModel ViewModel { get; set; } = new MainViewModel(); public MainPage() { this.InitializeComponent(); } }
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel HorizontalAlignment="Center"> <TextBlock Text="{x:Bind Title}" /> <TextBlock Text="{x:Bind ViewModel.Name}" /> <TextBlock Text="{x:Bind ViewModel.Content}" /> </StackPanel> </Grid>
結(jié)果

四、數(shù)據(jù)模板中使用
因?yàn)閤:Bind 是強(qiáng)類型,所以你在使用DataTemplate的時(shí)候,要指定x:DataType,我們改寫一下上面的例子。
CodeBehand的代碼不用變,我們直接變Xaml中的。加一個(gè)Key為TestDataTmpleate的數(shù)據(jù)模板,為了區(qū)別,我們給一個(gè)背景色,把順序也換一下.
<Page x:Class="BindDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:BindDemo" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.Resources> <DataTemplate x:Key="TestDataTemplate" x:DataType="local:MainViewModel"> <StackPanel Background="Orange"> <TextBlock Text="{x:Bind Content}" /> <TextBlock Text="{x:Bind Name}" /> </StackPanel> </DataTemplate> </Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel HorizontalAlignment="Center"> <TextBlock Text="{x:Bind Title}" /> <ContentControl ContentTemplate="{StaticResource TestDataTemplate}" Content="{x:Bind ViewModel}" /> </StackPanel> </Grid> </Page>
結(jié)果:
如果,我們不設(shè)置x:DataType呢。他就會(huì)告訴我們這個(gè)Error。

五、綁定事件到方法
x:Bind 是支持事件綁定到方法的,例如我給MainViewModel 加一個(gè)Test方法。然后在數(shù)據(jù)模板中加一個(gè)Button。
Click這個(gè)不用想都支持,我就隨便試了一個(gè)PointerEntered事件啦。
綁定的這個(gè)方法,可以不寫參數(shù),也可以把事件的參數(shù)寫全。
<DataTemplate x:Key="TestDataTemplate" x:DataType="local:MainViewModel"> <StackPanel Background="Orange"> <TextBlock Text="{x:Bind Content}" /> <TextBlock Text="{x:Bind Name}" /> <Button PointerEntered="{x:Bind Test}" /> </StackPanel> </DataTemplate>

完美進(jìn)入斷點(diǎn)。
六、總結(jié)
現(xiàn)在Binding也是可以一同使用的,至少不帶表x:Bind可以全完替代Binding,至少在動(dòng)態(tài)類型的綁定上,x:Bind是玩不轉(zhuǎn)的。而且使用x:Bind 多少打亂一些我們已習(xí)慣的MVVM的結(jié)構(gòu)。
但是x:Bind所帶來的性能的提升,真心讓人心動(dòng),可以的話,可以盡量的使用這個(gè)。
本文只是我的x:Bind的初步理解,也沒找到什么文檔,如果有不對(duì)的地方,請(qǐng)大家指出來。謝謝。
本文地址:http://www.rzrgm.cn/gaoshang212/p/4534138.html
浙公網(wǎng)安備 33010602011771號(hào)