【问题标题】:Binding when navigating with caliburn AppBarButton not working使用 caliburn AppBarButton 导航时绑定不起作用
【发布时间】:2013-01-14 15:17:49
【问题描述】:

AppBarButton 有这个奇怪的问题。我已将按钮绑定到我的视图模型中的方法,如下所示:

主视图的 XAML

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource HsAppBarColor}" Opacity="1">
        <shell:ApplicationBar.Buttons>
            <cal:AppBarButton IconUri="/icons/appbar.feature.settings.rest.png" Text="ajustes" Message="GoToSettings" />
            <cal:AppBarButton IconUri="/icons/appbar.favs.rest.png" Text="favoritos" Message="GoToFavs" />
        </shell:ApplicationBar.Buttons>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

用于 MainViewModel 的 C#

    public void GoToSettings()   {
        navigation.UriFor<SettingsViewModel>().Navigate();    
    }

它会触发 GoToSettings 代码并导航到 SettingsView 页面,但是,SettingsViewModel 从未初始化,也不会调用构造函数。用于将视图与视图模型绑定的 Caliburn 代码不起作用。

我想知道在 MainPage 中有一个为其示例数据设置 DataContext 的 Pivot 是否会产生影响。我尝试评论数据上下文,但它仍然没有调用 SettingsViewModel 构造函数。

这是 Pivot XAML 代码

<controls:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0" Margin="0" Title="" DataContext="{Binding Source={StaticResource Stores}}" TitleTemplate="{StaticResource StoresPivotTitleTemplate}" HeaderTemplate="{StaticResource StoresPivotHeaderTemplate}"  Style="{StaticResource StoresPivotStyle}" FontFamily="Arial Narrow" Background="{x:Null}">
    <controls:PivotItem Header="Restorantes" BorderThickness="0,-20,0,0" Margin="12,0" Background="{x:Null}">
        <Grid>
            <ListBox  toolkit:TurnstileFeatherEffect.FeatheringIndex="2"  cal:Message.Attach="[Event SelectionChanged] = [Action Restaurants_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" Background="{x:Null}"/>
        </Grid>
    </controls:PivotItem>
    <controls:PivotItem Header="Tiendas" Margin="12,0">
        <Grid>
            <ListBox  toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Stores_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" FontFamily="Segoe WP"/>
        </Grid>
    </controls:PivotItem>
</controls:Pivot>

Stores_SelectionChanged 方法也导航到一个页面,但这次绑定有效并且触发了 viewmodel 构造函数。

知道为什么它在 AppBarButton 中不起作用,但是当它们使用相同的视图模型时,它在 Pivot 附加操作中起作用?

编辑:为视图添加了完整的 xaml

   <phone:PhoneApplicationPage
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls" 
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:controlsPrimitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
   x:Class="Happyshop.Views.StoresView"
  SupportedOrientations="Portrait" Orientation="Portrait"
  mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
  shell:SystemTray.IsVisible="True" Foreground="{x:Null}">

<toolkit:TransitionService.NavigationInTransition>
    <toolkit:NavigationInTransition>
        <toolkit:NavigationInTransition.Backward>
            <toolkit:TurnstileFeatherTransition Mode="BackwardIn"/>
        </toolkit:NavigationInTransition.Backward>
        <toolkit:NavigationInTransition.Forward>
            <toolkit:TurnstileFeatherTransition Mode="ForwardIn" BeginTime="0:0:0.7"/>
        </toolkit:NavigationInTransition.Forward>
    </toolkit:NavigationInTransition>
</toolkit:TransitionService.NavigationInTransition>
<toolkit:TransitionService.NavigationOutTransition>
    <toolkit:NavigationOutTransition>
        <toolkit:NavigationOutTransition.Backward>
            <toolkit:TurnstileFeatherTransition Mode="BackwardOut"/>
        </toolkit:NavigationOutTransition.Backward>
        <toolkit:NavigationOutTransition.Forward>
            <toolkit:TurnstileFeatherTransition Mode="ForwardOut" BeginTime="0:0:0.2"/>
        </toolkit:NavigationOutTransition.Forward>
    </toolkit:NavigationOutTransition>
</toolkit:TransitionService.NavigationOutTransition>


<phone:PhoneApplicationPage.Resources>
    <Style x:Key="StoresPivotStyle" TargetType="controls:Pivot">
        <Setter Property="Margin" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <Grid/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:Pivot">
                    <Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <Grid.Background>
                            <SolidColorBrush Color="{StaticResource HsDarkBlue}"/>
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="65"/>
                            <RowDefinition Height="96"/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid CacheMode="BitmapCache" Grid.RowSpan="3"/>
                        <Border Margin="-12,-5,0,0" BorderThickness="0,0,0,0">
                            <ContentPresenter ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Margin="12,5,0,0"/>
                        </Border>
                        <Border Grid.Row="1" toolkit:TurnstileFeatherEffect.FeatheringIndex="1" >
                            <controlsPrimitives:PivotHeadersControl x:Name="HeadersListElement" Background="Black"/>
                        </Border>
                        <ItemsPresenter x:Name="PivotItemPresenter" Margin="0" Grid.Row="2"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>
<phone:PhoneApplicationPage.Background>
    <SolidColorBrush Color="{StaticResource HsDarkBlue}"/>
</phone:PhoneApplicationPage.Background>
<phone:PhoneApplicationPage.FontFamily>
    <StaticResource ResourceKey="PhoneFontFamilyNormal"/>
</phone:PhoneApplicationPage.FontFamily>
<phone:PhoneApplicationPage.FontSize>
    <StaticResource ResourceKey="PhoneFontSizeNormal"/>
</phone:PhoneApplicationPage.FontSize>

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource HsAppBarColor}" Opacity="1">
        <shell:ApplicationBar.Buttons>
            <cal:AppBarButton IconUri="/icons/appbar.feature.settings.rest.png" Text="ajustes" Message="GoToSettings" />
            <cal:AppBarButton IconUri="/icons/appbar.favs.rest.png" Text="favoritos" Message="GoToFavs" />
        </shell:ApplicationBar.Buttons>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>


<controls:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0" Margin="0" Title="" DataContext="{Binding Source={StaticResource Stores}}" TitleTemplate="{StaticResource StoresPivotTitleTemplate}" HeaderTemplate="{StaticResource StoresPivotHeaderTemplate}"  Style="{StaticResource StoresPivotStyle}" FontFamily="Arial Narrow" Background="{x:Null}">
    <controls:PivotItem Header="Restorantes" BorderThickness="0,-20,0,0" Margin="12,0" Background="{x:Null}">
        <Grid>
            <ListBox  toolkit:TurnstileFeatherEffect.FeatheringIndex="2"  cal:Message.Attach="[Event SelectionChanged] = [Action Restaurants_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" Background="{x:Null}"/>
        </Grid>
    </controls:PivotItem>
    <controls:PivotItem Header="Tiendas" Margin="12,0">
        <Grid>
            <ListBox  toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Stores_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" FontFamily="Segoe WP"/>
        </Grid>
    </controls:PivotItem>
</controls:Pivot>

更新 这是一个愚蠢的错误。当您使用智能感知生成类时,创建的类不会标记为公共。默认情况下,类被标记为内部,因此 Caliburn 项目无法访问 ViewModel,因此它不会绑定它。

【问题讨论】:

  • 您是否在视图中的其他任何位置设置视图的 DataContext?通常这个问题是因为 DataContext 被覆盖了。
  • 否,仅为 Pivot 设置 DataContext
  • 如果您对视图进行了任何编辑,您能否发布完整的 Xaml 视图以及代码隐藏?
  • 我发现了愚蠢的错误,无论如何,谢谢你的帮助
  • 很高兴看到它解决了。您应该回答自己的问题,以便其他犯同样错误的人找到它。

标签: c# windows-phone-7 xaml caliburn.micro


【解决方案1】:

已解决。问题是在 Visual Studio 中通过 Intellisense 代码生成创建的类未标记为公共。

类必须标记为 public 以便 Caliburn 能够访问和实例化一个。否则,他们会使用默认的内部可见性

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多