代码之家  ›  专栏  ›  技术社区  ›  Kazuhiko Nakayama

卡利布伦。微样本/特征/UWP:“cm:Bind.Model={Binding}”是什么

  •  0
  • Kazuhiko Nakayama  · 技术社区  · 8 年前

    Here is Calibrun.Micro example with UWP.

    enter image description here

    在起泡样品中, in this file , 有一条线

    <Grid cm:Bind.Model="{Binding}">
    

    什么意思?为什么有必要?

    <Button x:Name="Message" cm:Message.Attach="SelectPhrase($dataContext)" Margin="0,12" />
    

    请建议我。。。。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Community Mohan Dere    6 年前

    让我们分析一下代码,因为在Caliburn中有些东西有点模糊。Micro通过约定工作:

    <Page
        x:Class="Features.CrossPlatform.Views.BubblingView"
        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"
        xmlns:cm="using:Caliburn.Micro"
        mc:Ignorable="d">
    
        <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBlock Text="actions" Style="{StaticResource SubheaderTextBlockStyle}" Margin="40,10,40,0"/>
            <StackPanel Margin="40,20">
                <ItemsControl x:Name="Phrases">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid cm:Bind.Model="{Binding}">
                                <Button x:Name="Message" cm:Message.Attach="SelectPhrase($dataContext)" Margin="0,12" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </StackPanel>
    </Page>
    

    首先,通过命名转换设置页面的datacontext(viewmodel)。可以找到此viewmodel here 。如您所见,此viewmodel具有 Phrases 属性和a SelectPhrase(MessageActivityViewModel phrase) 方法

    如果我们从树上下来,我们有一个 <ItemsControl x:Name="Phrases"> 短语 属性,它是 MessageActivityViewModel .

    ItemsControl Grid {Binding} ,它将从模板继承datacontext,模板是正在呈现的当前元素(单个 MessageActivityViewModel ). 然而,我们想把 SelectPhrase 上的方法 BubblingViewModel 而不是在 为了做到这一点,我们使用 {Binding} MessageActivityViewModel .

    该按钮的作用是发送datacontext对象(作为渲染的 MessageActivityViewModel

    $dataContext:

    传递ActionMessage附加到的元素的DataContext。这在主/细节场景中非常有用,在主/细节场景中,ActionMessage可能会冒泡到父VM,但需要携带要处理的子实例。

    资料来源: http://caliburnmicro.com/documentation/cheat-sheet