使用按钮以编程方式在C#中添加Bottom App Bar

时间:2012-12-04 18:40:24

标签: silverlight xaml windows-8 winrt-xaml

我想在C#中以代码隐藏的方式以编程方式添加BottomAppBar。我这样做了:

1:添加了一个包含GridTemplate的资源文件,其中包含Grid,StackPanel和两个按钮。

2:在我的BasePage.cs(派生自Page类)中,我定义了一个新的AppBar,并将它的ContentTemplate设置为在step1中创建的Resource。

3:我在第2步设置了此.BottomAppBar = AppBar。

现在,这将AppBar添加到从BasePage派生的所有页面中。这很好。

问题:

我无法从AppBar中的两个元素中获取PointerPressed或任何其他事件。

我确信这是我遗失的非常基本的东西。任何想法,任何人?

更新:下面添加的示例下载链接以及我想要的是当点击BottomAppBar(Page1和2)中的图像时,它应该带我到MainPage。

Download Sample

AppBar代码

AppBar appbar = new AppBar();
appbar.Name = "BottomBar";
DataTemplate dt = Application.Current.Resources["BottomAppBarDT"] as DataTemplate;
appbar.ContentTemplate = dt;
this.BottomAppBar = appbar;

3 个答案:

答案 0 :(得分:2)

在数据模板中,它总是最好实现命令而不是事件处理程序。 您要做的是确保创建一个DelegateCommand,其方法与您尝试触发的Button Click Event相同。 DelegateCommand的代码应放在View Model中,该View Model充当此特定控件(App Bar)的父级的DataContext,或者如果您正在使用具有Command Aggregator的MVVM结构,那么它应该放在那里。

public class DelegateCommand : ICommand
  {
    private Action _action;

    public DelegateCommand(Action action)
    {
      _action = action;
    }

    public bool CanExecute(object parameter)
    {
      return true;
    }

    public event Windows.UI.Xaml.EventHandler CanExecuteChanged;


    public void Execute(object parameter)
    {
      _action();
    }
  }

  /// <summary>
  /// Gets a command that executes a search
  /// </summary>
  public ICommand ExecuteSearchCommand
  {
    get
    {
      return new DelegateCommand(() => ExecuteSearch());
    }
  }

答案 1 :(得分:0)

您是否真的将Click事件处理程序添加到AppBar上的按钮?

答案 2 :(得分:0)

这是最终的代码。

<强> BaseView.cs

public partial class BaseView: Page
    {
        public BaseView()
        {
            GoHomeCommand = new MyCommand<object>(OnGoHome);
            this.DataContext = this;
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            AddAppBar();
        }
        private void AddAppBar()
        {
            AppBar appbar = new AppBar();
            appbar.Name = "BottomBar";
            DataTemplate dt = Application.Current.Resources["BottomAppBarDT"] as DataTemplate;
            appbar.ContentTemplate = dt;
            this.BottomAppBar = appbar;
        }

        public MyCommand<object> GoHomeCommand { get; set; }
        void OnGoHome(object obj)
        {
            Debug.WriteLine("Go Home2");
            Frame.Navigate(typeof(MainPage));
        }
    }

    public class DelegateCommand : ICommand
    {
        private Action _action;

        public DelegateCommand(Action action)
        {
            _action = action;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            _action();
        }
    }

<强>资源

<DataTemplate x:Key="BottomAppBarDT" >
        <Grid x:Name="AppBarGrid" Background="SlateGray">
            <StackPanel x:Name="AppBarRightStack" Orientation="Horizontal" HorizontalAlignment="Left">
                <Button Command="{Binding GoHomeCommand}" FontSize="24">
                    <Image Source="Assets/Logo.png" Height="100" Width="100"/>
                </Button>
            </StackPanel>
        </Grid>
    </DataTemplate>