私はC#UWP開発で新しく、実行時にTextBlockの値を変更しようとしていますが、バインディングが正しく機能しません。テキストブロックバインディングが実行時に更新されない
XAMLのTextBlockのtextプロパティをINotifyPropertyChangedを持つViewModelのプロパティにバインドしています。値は10秒ごとに変更されます。
私はそれが正しい方法であるかどうかわかりませんが、誰かが私を助けることができますか?
ありがとうございます!
class MainPaigeViewModel : INotifyPropertyChanged
{
public MainPaigeViewModel()
{
Task.Run(async() =>
{
Random random = new Random();
while (true)
{
await Task.Delay(10000);
int newValue = random.Next(-40, 40);
_MyValue = newValue.ToString();
Debug.WriteLine(MyValue);
}
});
}
//Properties
private string _MyValue;
public string MyValue
{
get { return _MyValue; }
set
{
_MyValue = value;
RaisePropertyChanged("MyValue");
}
}
//INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CountDown2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModels="using:CountDown2.ViewModels"
x:Class="CountDown2.MainPage"
mc:Ignorable="d">
<Page.DataContext>
<ViewModels:MainPaigeViewModel/>
</Page.DataContext>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<RelativePanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="{Binding MyValue, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Width="100"
Height="40"
TextAlignment="Center"
FontSize="20"
/>
</RelativePanel>
</Grid>
</Page>
あなたはRaisepropertychangeメソッドにブレークポイントを入れて、それが発射されたかどうかを確認することができますか? – riteshmeher
外部リンクではなく、ご質問のインプレースコードを入力してください。ここで、[mcve]を与えることが重要である理由を見てください。 – dymanoid
@riteshmeher RaisePropertyChangedは起動していませんが、何が問題なのでしょうか? – CGuiVi