使用caliburn导航时绑定AppBarButton不起作用

时间:2013-01-14 15:17:49

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

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

MainView的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中是否有一个Pivot为其样本数据设置DataContext可能会影响。我尝试评论数据上下文,但它仍然没有调用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>

UPDATE 这是一个愚蠢的错误。当您使用具有intellisense的generate类时,创建的类不会标记为public。默认情况下,类标记为内部,因此Caliburn项目无法访问ViewModel,因此无法绑定它。

1 个答案:

答案 0 :(得分:1)

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

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

相关问题