MVVMまではいかないですが、簡単にデータバインディングを試してみたいと思います。
データモデル
public class SampleDat { public string stringValue { get; set; } = string.Empty; public int intValue { get; set; } = 0; public DateTime dateValue { get; set; } = DateTime.Now; public decimal decValue { get; set; } = 0; }
こんな感じプロパティを持ったクラスを作ります。
XAML
<Grid Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Style="{StaticResource baseGrid}"> <Label Text="{Binding stringValue}" FontSize="Large"> </Label> </Grid> <Grid Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="6" Style="{StaticResource baseGrid}"> <Label Text="{Binding intValue}" FontSize="Large"> </Label> </Grid> <Grid Grid.Column="5" Grid.ColumnSpan="2" Grid.Row="1" Style="{StaticResource baseGrid}"> <Label Text="{Binding dateValue}" FontSize="Large"> </Label> </Grid> <Grid Grid.Column="5" Grid.ColumnSpan="2" Grid.Row="6" Style="{StaticResource baseGrid}"> <Label Text="{Binding decValue}" FontSize="Large"> </Label> </Grid>
今回はLabelのTextにバインディングします。
{Binding プロパティ名}
な感じで設定しておきます。
ソースコード
dat.stringValue = "hoge"; dat.intValue = 1234; dat.dateValue = DateTime.Now; dat.decValue = 456.789M; this.BindingContext = dat;
どこでも良いのですが、お試しでコンストラクタに書いちゃいました。
UWPなXAMLだと、
this.DataContext = dat
となるので、このあたりが違いますね。
で、結果はこう。(UWPの場合)
なお、Xamarin.Forms Previewerでは値が出てきませんでした。