/ / xamlのxaml.csファイル内の外部スタックレイアウトコンポーネントIDへの参照Xamarin - c#、xaml、xamarin

xamlのxaml.csファイル内の外部スタックレイアウトコンポーネントIDへの参照Xamarin - c#、xaml、xamarin

私はstackLayoutコンポーネントからxamlファイルを作り出しています。私はこのTimerButtonを呼び出しました。

私は2つのTimerButtonを持っており、それらを区別したい。

//In MainPage.xaml
<component:TimerButton x:Name="Smoke"></component:TimerButton>
<component:TimerButton x:Name="Snuff"></component:TimerButton>

TimerButtonコンポーネント内のC#コード(ViewModel.TobaccoType)の値を設定できるように、IDを送信する必要があります。私はx:arguments / name / typeを使ってみました。

//In TimerButton.xaml.cs
ViewModel = new TimerButtonViewModel();

if (this.FindByName<TimerButton>("Smoke") != null)
{
ViewModel.TobaccoType = "Smoke";
}

回答:

回答№1は1

あなたは以下を行うことができます。タイマーボタンをいわゆる DependencyProperty このような:

public static readonly DependencyProperty TobaccoTypeProperty =
DependencyProperty.Register(
"TobaccoType", typeof(String),
typeof(TimerButton), null);

public String TobaccoType
{
get { return (String)GetValue(TobaccoTypeProperty); }
set { SetValue(TobaccoTypeProperty, value); }
}

そして、あなたはこれをあなたのXAMLで以下のように参照します:

//In MainPage.xaml
<component:TimerButton x:Name="Smoke" TobaccoType="Smoke"></component:TimerButton>
<component:TimerButton x:Name="Snuff" TobaccoType="Snuff"></component:TimerButton>

このプロパティは、TimerButton.csで簡単に参照できます