2017-04-20 285 views
0

所以,我是新來Xamarin形式我發現添加按鈕的方法有兩種:在Xamarin Forms中添加按鈕的最佳方式是什麼?

中的.xaml文件

<!-- <Button Text="Click Me!" 
     Clicked="OnButtonClicked" VerticalOptions="CenterAndExpand" />--> 

1.Declaring按鈕和.xaml.cs文件

public void OnButtonClicked(object sender, EventArgs args) 
    { 
     count++; 

     label.Text = 
      String.Format("{0} click{1}!", count, count == 1 ? "" : "s"); 
  • 聲明僅在.xaml.cs按鈕文件

    using System; 
    using Xamarin.Forms; 
    
    namespace FormsGallery 
    { 
        class ButtonDemoPage : ContentPage 
        { 
         Label label; 
         int clickTotal = 0; 
    
         public ButtonDemoPage() 
         { 
          Label header = new Label 
          { 
           Text = "Button", 
           Font = Font.BoldSystemFontOfSize(50), 
           HorizontalOptions = LayoutOptions.Center 
          }; 
    
          Button button = new Button 
          { 
           Text = "Click Me!", 
           Font = Font.SystemFontOfSize(NamedSize.Large), 
           BorderWidth = 1, 
           HorizontalOptions = LayoutOptions.Center, 
           VerticalOptions = LayoutOptions.CenterAndExpand 
          }; 
          button.Clicked += OnButtonClicked; 
    
          label = new Label 
          { 
           Text = "0 button clicks", 
           Font = Font.SystemFontOfSize(NamedSize.Large), 
           HorizontalOptions = LayoutOptions.Center, 
           VerticalOptions = LayoutOptions.CenterAndExpand 
          }; 
    
          // Accomodate iPhone status bar. 
          this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5); 
    
          // Build the page. 
          this.Content = new StackLayout 
          { 
           Children = 
           { 
            header, 
            button, 
            label 
           } 
          }; 
         } 
    
         void OnButtonClicked(object sender, EventArgs e) 
         { 
          clickTotal += 1; 
          label.Text = String.Format("{0} button click{1}", 
                clickTotal, clickTotal == 1 ? "" : "s"); 
         } 
        } 
    } 
    
  • 但事情是:我想知道哪種方式更適合添加按鈕並且沒有任何未來的代碼問題。

    謝謝!

    +0

    有談論代碼VS XAML這只是事情的偏好,有的喜歡XAML則喜歡代碼的時候沒有這樣的東西更好。 – apineda

    +0

    有人發佈了相同的問題xamarin論壇你有沒有看過..https://forums.xamarin.com/discussion/33175/which-is-the-best-way-to-design-the-ui-in-xamarin-形式 – GvSharma

    回答

    1

    它們在功能上等同。在XAML中構建用戶界面通常可以讓設計中的問題更清晰地分離,但是一種方法並不比其他方法「更好」。

    0

    它們是相同的。在用XAML構建用戶界面後,它將轉換爲與C#等效的內容,與使用C#編寫視圖一樣。

    只要你喜歡就編寫你的UI。對我而言,更好的方法是將XAML更簡潔易懂。

    相關問題