Я изучаю UWP с помощью призмы, но не могу заставить его работать. Я создал проект с шаблоном Prism Mvvm и добавил только модель и несколько элементов управления.
при запуске приложения в верхнем текстовом поле, привязанном к MyPerson, ничего не отображается. Имя второго текстового поля, привязанного к Myperson.Age, показывает 25. Когда я нажимаю на кнопку, в верхнем текстовом поле отображается «Блоги Джо», а во втором текстовом поле появляется то же самое. Ничего не обновляется?
Вот код XAML:
<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>
Моя модель:
namespace App5.Models
{
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
}
Моя 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;
}
}
}