質問するログイン新規登録

回答編集履歴

1

コード追記

2017/06/10 02:03

投稿

yamamo
yamamo

スコア37

answer CHANGED
@@ -3,4 +3,70 @@
3
3
  提示されているコードを見る限りではLabelの表示は更新されるかと思います。
4
4
 
5
5
  原因は提示されているコード以外の部分にあるように思えるのですが、如何でしょうか。
6
- (例えば、1秒毎にインクリメントしているのがフィールド変数のcountだったりとか…)
6
+ (例えば、1秒毎にインクリメントしているのがフィールド変数のcountだったりとか…)
7
+ ```cs
8
+ public class CClass : INotifyPropertyChanged
9
+ {
10
+ public ICommand StartTimerCommand { get; }
11
+
12
+ public event PropertyChangedEventHandler PropertyChanged = delegate { };
13
+
14
+ int count;
15
+
16
+ public CClass()
17
+ {
18
+ StartTimerCommand = new Command(() =>
19
+ {
20
+ Device.StartTimer(
21
+ TimeSpan.FromSeconds(1),
22
+ () => { Counter++; return true; });
23
+ });
24
+ }
25
+
26
+ public int Counter
27
+ {
28
+ get
29
+ {
30
+ return this.count;
31
+ }
32
+ set
33
+ {
34
+ if (this.count != value)
35
+ {
36
+ this.count = value;
37
+ OnPropertyChanged("Counter");
38
+ }
39
+ }
40
+ }
41
+
42
+ protected virtual void OnPropertyChanged(string propertyName)
43
+ {
44
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
45
+ }
46
+ }
47
+ ```
48
+ ```xaml
49
+ <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
50
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
51
+ xmlns:local="clr-namespace:App1"
52
+ x:Class="App1.MainPage">
53
+ <ContentPage.BindingContext>
54
+ <local:CClass />
55
+ </ContentPage.BindingContext>
56
+
57
+ <StackLayout VerticalOptions="Center">
58
+ <Label Text="{Binding Counter, Mode = TwoWay, StringFormat='{0}'}"
59
+ x:name="CountLabel"
60
+ BackgroundColor="Aqua"
61
+ HorizontalTextAlignment="Center"
62
+ VerticalTextAlignment="Center"/>
63
+
64
+ <Button Text="Start"
65
+ Command="{Binding StartTimerCommand}"
66
+ VerticalOptions="Center"
67
+ HorizontalOptions="Center" />
68
+ </StackLayout>
69
+
70
+ </ContentPage>
71
+ ```
72
+