2016-05-20 10 views
2

私は現在、作業環境としてXamarin Formsを使用している私のプロジェクトにContentPage.csを持っています。コードの最後にあるボタンにOnClickを追加できるかどうかは疑問でした。どんな助けもありがとうございます。ありがとうございました。XamarinフォームボタンOnClick

using System;

Xamarin.Formsを使用。

名前空間DebuggerTestAndroidIOS { パブリッククラスVSParameters:ContentPage { 公共VSParameters() { コンテンツは=新しいStackLayout { 子供= { 新しいStackLayout {BackgroundColorを= Color.FromHex( "0ADF80")、 子供= {

     new Label { Text = PatientInformation.PatientName, 
          TextColor= Color.White, 
          FontSize = 25 
         } 
        } 
       }, 

       //Patient Information Stack Layout 
       new StackLayout{ Orientation = StackOrientation.Horizontal, 
        Children = { 

         //Patient Image, sex and date of birth 
         new StackLayout{Orientation = StackOrientation.Vertical, Padding = new Thickness (30, 0, 0, 0), 
          Children = { 

           new Image {Source = "UserMale.png"}, 
           new Label {Text = "Sex: " + PatientInformation.Sex , FontSize = 15, TextColor= Color.Black}, 
           new Label{Text = "Date of Birth: " + (PatientInformation.DateOfBirth).ToString(), FontSize = 15, TextColor= Color.Black} 
          } 

         }, 

         //other patient information 
         new StackLayout{Orientation = StackOrientation.Vertical, 
          Children = { 
           new Label {Text = "ID: " + PatientInformation.PatientID, FontSize = 15, TextColor= Color.Black}, 


           new Label{Text = "Room: " + PatientInformation.RoomNumber, FontSize = 15, TextColor= Color.Black}, 
           new Label {Text = "Bed: " + PatientInformation.BedID, FontSize = 15, TextColor= Color.Black}, 
           new Label{Text = "Primary Doctor: " + PatientInformation.PrimaryDoctor, FontSize = 15, TextColor= Color.Black} 

          } 

         } 


        } 
       }, 
       new StackLayout {Orientation = StackOrientation.Horizontal, Padding = new Thickness(30, 0, 0, 0), 
        Children = { 

         new StackLayout{Orientation = StackOrientation.Vertical, 
          Children = { 
          new Label {Text ="Heart Rate", FontSize= 20, TextColor = Color.FromHex("D95D65"), HorizontalOptions = LayoutOptions.StartAndExpand}, 
           new Label{Text=""}, 
           new Label {Text ="Temperature", FontSize= 20, TextColor = Color.FromHex("08CD78"), HorizontalOptions = LayoutOptions.StartAndExpand}, 
           new Label{Text=""}, 
          new Label {Text ="Respiration Rate", FontSize= 20, TextColor = Color.FromHex("08CD78"), HorizontalOptions = LayoutOptions.StartAndExpand}, 
          new Label {Text ="Blood Pressure: ", FontSize= 20, TextColor = Color.FromHex("D95D65"), HorizontalOptions = LayoutOptions.StartAndExpand}, 
           new Label{Text=""}, 
           new Label{Text=""}, 
           new Label {Text ="Systolic", FontSize= 18, TextColor = Color.FromHex("D95D65")}, 
          new Label {Text ="Diastolic", FontSize= 18, TextColor = Color.FromHex("D95D65")} 
          } 
         }, 

         new StackLayout{Orientation = StackOrientation.Vertical, 
          Children = { 
           new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100}, 
           new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100}, 
           new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100}, 
           new Label{Text=""}, 
           new Label{Text=""}, 
           new Label{Text=""}, 

           new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100}, 
           new Entry {TextColor = Color.Black, BackgroundColor = Color.FromRgb(231, 231, 231), WidthRequest= 100}, 


          } 
         }, 

        } 

       }, 

       new StackLayout{HorizontalOptions = LayoutOptions.Center, 
        Children = { 

         new Button{Text = "Add parameters"} 


        } 

       } 
      } 
     }; 

    } 
} 

}

+0

は見え –

+0

はい、私がしました。あなたのコメントをありがとう! – Lala

答えて

4

あなたは本当にあなたがこれを行うことができますインライン化したい場合は:あなたは、いくつかの選択肢を持っているよう

new Button { 
    Text = "Add parameters" 
    Command = new Command(() => { 
     //Do something 
    }) 
}; 
+0

ありがとう!短くて甘い! – Lala

0

Iを決してunderstooコントロールをインラインで追加する。おそらくあなたは後でそれらにアクセスすることができますが、名前がなければ、悪い考えのようなスタックのインデックスによってアクセスしなければならないように思えるかもしれません。だから私は正確に答えを持っていませんが、むしろコメントにうまく収まらない提案です。

public MyPage: ContentPage 
    { 
     public MyPage() 
     { 
      var slayout = new StackLayout(); 
      var button = new Button(); 
      button.click += (o,s) => {someevent}; 
      slayout.children.add(button); 
      this.Content= slayout; 
     } 
    } 

その後、コントロールを作成するコンテンツとしてレイアウトを追加し、その後、レイアウトに追加すると、コントロールにアクセスできるようになります。

0

Content = new StackLayout....の開始前に、あなたのボタンを定義:

var myButton = new Button 
{ 
    Text = "Add parameters" 
}; 
myButton.Clicked += (object sender, EventArgs e) => 
{ 
    System.Diagnostics.Debug.WriteLine("I've been clcked"); 
}; 

次にあなたがStackLayoutの子として追加します。

このコード:

new StackLayout{ 
    HorizontalOptions = LayoutOptions.Center, 
    Children = { 
    new Button{Text = "Add parameters"} 
    } 
} 

は次のようになります。

new StackLayout{ 
    HorizontalOptions = LayoutOptions.Center, 
    Children = { 
    myButton 
    } 
} 
+0

ありがとうございます。 – Lala

関連する問題