主页面
--------------------------
<Page
x:Class="CheckMemoryLeak.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CheckMemoryLeak"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding PersonList,Mode=OneTime}" SelectedItem="{Binding SelectedPerson,Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name,Mode=OneTime}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Page>
C# code
--------------------------------------------------
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var viewModel = new MainViewModel();
this.DataContext = viewModel;
viewModel.NavigateEvent += ViewModel_NavigateEvent;
}
private void ViewModel_NavigateEvent(object sender, NavigationEventArgs e)
{
this.Frame.Navigate(Type.GetType(e.PageName), e.Parameter);
}
//protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
//{
// base.OnNavigatedFrom(e);
// var viewModel = this.DataContext as INavigable;
// viewModel.OnNavigatedFrom(e.Parameter);
//}
}
view mode
------------------------------------------
public class MainViewModel : INotifyPropertyChanged, INavigable
{
private PersonViewModel _selectedPerson;
private List<PersonViewModel> _personList;//{ get; set; }
public List<PersonViewModel> PersonList
{
get
{
return _personList;
}
set
{
_personList = value;
OnPropertyChanged();
}
}
public PersonViewModel SelectedPerson
{
get
{
return _selectedPerson;
}
set
{
_selectedPerson = value;
OnPropertyChanged();
NavigateEvent(this, new NavigationEventArgs
{
PageName = "CheckMemoryLeak.SecondPage",
Parameter = new PersonViewModel { Name = _selectedPerson.Name, Age = _selectedPerson.Age }
});
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler<NavigationEventArgs> NavigateEvent;
public event EventHandler GoBackEvent;
public MainViewModel()
{
PersonList = new List<PersonViewModel>
{
new PersonViewModel { Name = "张三" , Age = 20 },
new PersonViewModel { Name = "李四" , Age = 30 },
new PersonViewModel { Name = "王五" , Age = 40 },
new PersonViewModel { Name = "赵六" , Age = 50 }
};
}
private void OnPropertyChanged([CallerMemberName]string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public void OnNavigatedTo(object obj)
{
}
public void OnNavigatedFrom(object obj)
{
}
}
second page
------------------------------------------------------
<Page
x:Class="CheckMemoryLeak.SecondPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CheckMemoryLeak"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="{Binding SelectedPerson.Name}"></TextBlock>
<TextBlock Text="{Binding SelectedPerson.Age}"></TextBlock>
<Button x:Name="ButtonGoBack" Click="Button_Click">GoBack</Button>
</StackPanel>
</Page>
C# code
--------------------------------
public sealed partial class SecondPage : Page
{
public SecondPage()
{
this.InitializeComponent();
this.DataContext = new SecondViewModel();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Frame.GoBack();
}
protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var viewModel = this.DataContext as INavigable;
viewModel.OnNavigatedTo(e.Parameter);
}
protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
this.ButtonGoBack.Click -= Button_Click;
}
}
mode
second view model
----------
public class SecondViewModel : INotifyPropertyChanged, INavigable
{
private PersonViewModel _selectedPerson;
public PersonViewModel SelectedPerson
{
get
{
return _selectedPerson;
}
set
{
_selectedPerson = value;
OnPropertyChanged();
}
}
public event EventHandler GoBackEvent;
public event EventHandler<NavigationEventArgs> NavigateEvent;
public event PropertyChangedEventHandler PropertyChanged;
public void OnNavigatedFrom(object obj)
{
}
public void OnNavigatedTo(object obj)
{
SelectedPerson = obj as PersonViewModel;
}
private void OnPropertyChanged([CallerMemberName]string name = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
<Page x:Class="CheckMemoryLeak.SecondPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:CheckMemoryLeak" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <TextBlock Text="{Binding SelectedPerson.Name}"></TextBlock> <TextBlock Text="{Binding SelectedPerson.Age}"></TextBlock> <Button x:Name="ButtonGoBack" Click="Button_Click">GoBack</Button> </StackPanel></Page>