2012-12-04 46 views
0

我想從C#中的代碼隱藏中以編程方式添加BottomAppBar。我做到了這樣做:以編程方式在C#中用按鈕添加BottomAppBar

1:增加了一個資源文件,其中包含一個網格,一個StackPanel和兩個按鈕的DataTemplate。

2:在我的BasePage.cs(派生自Page類)中,我定義了一個新的AppBar並將它的ContentTemplate設置爲在step1中創建的資源。

3:我從step2設置this.BottomAppBar = AppBar。

現在,將AppBar添加到從BasePage派生的所有頁面中。這工作正常。

問題:

我不能得到PointerPressed或從AppBar我的兩個元素觸發的任何其他事件。

我相信這是我很想念的東西。任何想法,任何人?

更新:下面添加的示例下載鏈接和我想要的是當單擊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; 

回答

2

在數據模板化中,總是最好實現命令而不是事件處理程序。 您要做的是確保創建一個DelegateCommand,該方法與您試圖觸發的按鈕單擊事件的方法相同。 DelegateCommand的代碼應放置在視圖模型中,該視圖模型充當此特定控件(應用欄)的父級的DataContext,或者如果您使用的是具有命令聚合器的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()); 
    } 
    } 
+0

謝謝扎克。不,我現在沒有使用MVVM,但想要如果它適合我​​的應用程序。我會嘗試一下你的建議,並更多地關注MVVM。 – TrekStir

+0

我知道你的想法和代碼文章:http://www.codeproject.com/Articles/391783/An-Address-Book-Application-Made-in-MVVM-For-Metro。我會上傳我的代碼。雖然我在按鈕上工作,但我也希望能在Image PointerPressed上工作。圖像不支持Command。 – TrekStir

+0

你可以使用一個按鈕並創建一個模板,並且我確定可能有一個名爲「ImageButton」的在線...這是我在Google上找到的第一個結果。 http://www.michielpost.nl/PostDetail_72.aspx –

0

你真的加入Click事件處理程序對AppBar的按鈕?

+0

是我做到了。但那不行。如果我手動添加appbar到xaml並將click處理程序添加到頁面的代碼隱藏中,它就可以工作。但在這種情況下,我需要爲我的所有頁面做到這一點。 – TrekStir

+0

@TrekStir你可以發佈一些代碼爲應用程序吧 – Mayank

+0

當然......我將不得不創建一個示例頁來演示它......今天晚些時候。 – TrekStir

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> 
相關問題