I have recently been worried about such issues. In the end, I used App.xaml.csto create the viewmodel.
The answers
Yes, this is the right way to create a static viewmodel in App.xaml.cs, because the class Appis accessible from any page of the application, it is also correct to declare them static, because you want to access it without creating an instance of the application, and, as Tarik said in your answer:
ViewModel and Model are static fields, so values are not destroyed if they go beyond. It also makes it easy to refresh multiple pages.
: , , , .
App.xaml.cs RootFrame:
private static MainViewModel viewModel;
public static MainViewModel ViewModel
{
get
{
if(viewModel == null)
viewModel = new MainViewModel();
return viewModel;
}
}
- , jst ( InitializeComponents();):
DataContext = App.ViewModel;
a >
, OnNavigatedTo() ( , ), , ViewModel, ).
:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
DataContext = null;
DataContext = App.ViewModel;
}
, :
- , , backbrowsing - , , . , , "".
, .
, , , :
(, , get:))
xaml :
<TextBlock text="{Binding SomePropertyNameFromViewModel}" />
<TextBlock text="{Binding SomeModelInViewModel.ItsProperty}" />
, :
<ListBox IemSource="{Binding SomeCollectionInViewModel}">
...rest omitted for brevity...
et cetera...
? , . ViewModel 6 7 , , , , .
P.S.: , , , WP pivot .
, App.xaml.cs, , . .