저는 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