UWP XAML棱镜未正确进行数据绑定
我正在用prism学习UWP,但我不能让它工作。我用Prism Mvvm模板创建了一个项目,只添加了一个模型和几个控件。 运行应用程序时,顶部绑定到MyPerson的文本框中没有任何内容。请命名绑定到MyPerson的第二个文本框。年龄显示25岁。当我点击按钮时,顶部的文本框显示“Joe Blogs”,第二个文本框显示相同的内容。没有更新任何内容? 以下是XAML代码:UWP XAML棱镜未正确进行数据绑定,uwp,prism,Uwp,Prism,我正在用prism学习UWP,但我不能让它工作。我用Prism Mvvm模板创建了一个项目,只添加了一个模型和几个控件。 运行应用程序时,顶部绑定到MyPerson的文本框中没有任何内容。请命名绑定到MyPerson的第二个文本框。年龄显示25岁。当我点击按钮时,顶部的文本框显示“Joe Blogs”,第二个文本框显示相同的内容。没有更新任何内容? 以下是XAML代码: <Page x:Class="App5.Views.MainPage" xmlns="http://schemas.mi
<Page
x:Class="App5.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Style="{StaticResource PageStyle}"
mc:Ignorable="d">
<Grid x:Name="ContentArea" Margin="{StaticResource MediumLeftRightMargin}">
<Grid.RowDefinitions>
<RowDefinition Height="48" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock
x:Uid="Main_Title"
Grid.Row="0"
Style="{StaticResource PageTitleStyle}" />
<Grid Grid.Row="1" Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
<StackPanel>
<TextBox
Width="100"
Height="100"
Text="{x:Bind Path=ViewModel.MyPerson.Name, Mode=TwoWay}" />
<TextBox
Width="100"
Height="100"
Text="{x:Bind Path=ViewModel.MyPerson.Age, Mode=TwoWay}" />
<Button
Width="100"
Height="100"
HorizontalAlignment="Center"
Click="Button_Click"
Content="test" />
</StackPanel>
</Grid>
</Grid>
我的ViewModel:
using App5.Models;
using Prism.Windows.Mvvm;
namespace App5.ViewModels
{
public class MainViewModel : ViewModelBase
{
private Person _myPerson;
public Person MyPerson
{
get => _myPerson;
set => SetProperty(ref _myPerson, value);
}
public MainViewModel()
{
MyPerson = new Person() { Age = 25, Name = "Bill Blogs" };
}
}
}
我的代码隐藏:
using System;
using App5.ViewModels;
using Windows.UI.Xaml.Controls;
namespace App5.Views
{
public sealed partial class MainPage : Page
{
private MainViewModel ViewModel => DataContext as MainViewModel;
public MainPage()
{
InitializeComponent();
}
private void MainPage_Loaded(object sender,
Windows.UI.Xaml.RoutedEventArgs e)
{
}
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.MyPerson.Name = "Viv Blogs";
ViewModel.MyPerson.Age = 50;
ViewModel.MyPerson = ViewModel.MyPerson;
}
}
}Person类应实现
INotifyPropertyChanged
接口并发出更改通知:
public class Person : ViewModelBase
{
private int _age;
public int Age
{
get => _age;
set => SetProperty(ref _age, value);
}
private int _name;
public int Name
{
get => _name;
set => SetProperty(ref _name, value);
}
}
然后,您可以简单地设置它的Age
和Name
属性:
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.MyPerson.Name = "Viv Blogs";
ViewModel.MyPerson.Age = 50;
}
要使MainViewModel
在设置MyPerson
属性时发出更改通知,需要将该属性设置为新的Person
对象:
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.MyPerson = new MyPerson() { Name = "Viv Blogs", Age = 50 };
}
Person
类应实现INotifyPropertyChanged
接口并发出更改通知:
public class Person : ViewModelBase
{
private int _age;
public int Age
{
get => _age;
set => SetProperty(ref _age, value);
}
private int _name;
public int Name
{
get => _name;
set => SetProperty(ref _name, value);
}
}
然后,您可以简单地设置它的Age
和Name
属性:
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.MyPerson.Name = "Viv Blogs";
ViewModel.MyPerson.Age = 50;
}
要使MainViewModel
在设置MyPerson
属性时发出更改通知,需要将该属性设置为新的Person
对象:
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ViewModel.MyPerson = new MyPerson() { Name = "Viv Blogs", Age = 50 };
}
如果您直接绑定到数据模型对象(在您的案例中是Person),我建议您也向其添加INotifyPropertychanged接口。这将使数据更改自动传播到UI。好的,但我认为prism是为您做的。不,它不是,但您可以使用Fody自动注入它。。。如果您直接绑定到数据模型对象(在您的案例中是Person),我建议您也向其添加INotifyPropertychanged接口。这将使数据更改自动传播到UI。好的,但我认为prism是为您做的。不,它不是,但您可以使用Fody自动注入它。。。好的,我应该用我的基类做这件事还是从我的基类继承?你是什么意思
ViewModelBase
是实现INotifyPropertyChanged
接口的基类。我不想为所有模型从ViewModelBase继承。然后创建您自己的模型基类,或者直接在Person
类中实现INotifyPropertyChanged
接口。我应该用我的基类执行此操作吗类还是从我的基类继承?什么意思ViewModelBase
是实现INotifyPropertyChanged
接口的基类。我不想为所有模型从ViewModelBase继承。然后创建您自己的模型基类,或者直接在Person
类中实现INotifyPropertyChanged
接口。