MVVM TextBlock 不会在运行时更新文本
MVVM TextBlock doesn't update text on runtime
我正在尝试了解 mvvm 模型。所以我跟着这个 youtube 教程:
YoutubeVideo
我有一个叫 Person 的 class。在视图的第一个文本框中,您可以插入您的姓名,在第二个文本框中插入姓氏。 Textblock 应该自动更新为全名 属性 但 这只发生在设计时 我不知道为什么。
这是主页的xaml:
<Page
x:Class="PropertyChanged.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PropertyChanged"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:PropertyChanged.Models"
mc:Ignorable="d">
<Page.Resources>
<m:Person x:Key="person"/>
</Page.Resources>
<Grid DataContext="{Binding Source={StaticResource person}}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto">
<TextBox Text="{Binding Path=Name, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding Path=Lastname, Mode=TwoWay}"></TextBox>
<TextBlock Text="{Binding Path=Fullname}" Width="Auto"></TextBlock>
</StackPanel>
</Grid>
还有我的人 class 在命名空间 PropertyChanged.Models:
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set { name = value;
OnPropertyChanged("Name");
OnPropertyChanged("FullName");
}
}
private string lastname;
public string Lastname
{
get { return lastname; }
set { lastname = value;
OnPropertyChanged("Lastname");
OnPropertyChanged("FullName");
}
}
private string fullname;
public string Fullname
{
get { return name +" " +lastname; }
set { fullname = value;
OnPropertyChanged("Fullname");
}
}
public Person()
{
if (DesignMode.DesignModeEnabled)
{
this.Name = "Matthias";
this.Lastname = "Herrmann";
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我不得不收回之前的回复。
我在我的系统上测试了代码并且它可以工作,它可以更改名称并且 TextBlock
在运行时更新。
唯一不同的是检测设计时间是否激活的代码:
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
this.Name = "Matthias";
this.Lastname = "Herrmann";
}
但是我仍然不建议对将在运行时编辑的对象使用 StaticResource
。
您可以在运行时更改对象的属性,并且由于属性中的更改通知,您将能够在 UI 中看到更改,但您无法重新设置 Person
对象本身,因为 StaticResource
的性质,这可能会造成混淆。
我有一个叫 Person 的 class。在视图的第一个文本框中,您可以插入您的姓名,在第二个文本框中插入姓氏。 Textblock 应该自动更新为全名 属性 但 这只发生在设计时 我不知道为什么。
这是主页的xaml:
<Page
x:Class="PropertyChanged.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PropertyChanged"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:PropertyChanged.Models"
mc:Ignorable="d">
<Page.Resources>
<m:Person x:Key="person"/>
</Page.Resources>
<Grid DataContext="{Binding Source={StaticResource person}}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto">
<TextBox Text="{Binding Path=Name, Mode=TwoWay}"></TextBox>
<TextBox Text="{Binding Path=Lastname, Mode=TwoWay}"></TextBox>
<TextBlock Text="{Binding Path=Fullname}" Width="Auto"></TextBlock>
</StackPanel>
</Grid>
还有我的人 class 在命名空间 PropertyChanged.Models:
public class Person : INotifyPropertyChanged
{
private string name;
public string Name
{
get { return name; }
set { name = value;
OnPropertyChanged("Name");
OnPropertyChanged("FullName");
}
}
private string lastname;
public string Lastname
{
get { return lastname; }
set { lastname = value;
OnPropertyChanged("Lastname");
OnPropertyChanged("FullName");
}
}
private string fullname;
public string Fullname
{
get { return name +" " +lastname; }
set { fullname = value;
OnPropertyChanged("Fullname");
}
}
public Person()
{
if (DesignMode.DesignModeEnabled)
{
this.Name = "Matthias";
this.Lastname = "Herrmann";
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我不得不收回之前的回复。
我在我的系统上测试了代码并且它可以工作,它可以更改名称并且 TextBlock
在运行时更新。
唯一不同的是检测设计时间是否激活的代码:
if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
this.Name = "Matthias";
this.Lastname = "Herrmann";
}
但是我仍然不建议对将在运行时编辑的对象使用 StaticResource
。
您可以在运行时更改对象的属性,并且由于属性中的更改通知,您将能够在 UI 中看到更改,但您无法重新设置 Person
对象本身,因为 StaticResource
的性质,这可能会造成混淆。