Features & Benefits
This will enable you to create methods in C# easily with double tap. And also enabled to crate MVVM properties easily. to do thet you have to create PropertyChange event . as follows,
| |
| CodePlex Project and Download Link | |
Features & Benefits
This will enable you to create methods in C# easily with double tap. And also enabled to crate MVVM properties easily. to do thet you have to create PropertyChange event . as follows,
| |
| CodePlex Project and Download Link | |
<prism:MvvmAppBase x:Class="SamplePrism.App" xmlns:prism="using:Microsoft.Practices.Prism.Mvvm" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:SamplePrism"> </prism:MvvmAppBase>
sealed partial class App : MvvmAppBase { public App() { this.InitializeComponent(); } protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args) {
//Main is the name of view i need to navigate this.NavigationService.Navigate("Main", null); return Task.FromResult<object>(null); } }
public abstract partial class PageBase: Page,IView { }3. Then Use this page base in our Views, xaml
<controls:PageBase x:Class="SamplePrism.Views.MainPage" xmlns:prism="using:Microsoft.Practices.Prism.Mvvm" xmlns:controls="using:SamplePrism.Controls" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:SamplePrism.Views" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" prism:ViewModelLocator.AutoWireViewModel="True" xmlns:designtime="using:SamplePrism.DesignTimeViewModel" mc:Ignorable="d">
public sealed partial class MainPage : PageBase { public MainPage() { this.InitializeComponent(); } }
public interface IMainPageViewModel { string Title { get; set; } }
public class MainPageViewModel : ViewModel, IMainPageViewModel { string _Title = default(string); public string Title { get { return _Title; } set { SetProperty(ref _Title, value); } } public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState) { this.Title = "Hello prism"; } }

namespace DesignTimeData.Runtime { public class MainViewModel:INotifyPropertyChanged { private string _Title { get; set; } public string Title { get { return _Title; } set { _Title = value; OnPropertyChanged("Title"); } } private string _Description { get; set; } public string Description { get { return _Description; } set { _Description = value; OnPropertyChanged("Description"); } } public MainViewModel() {
// Hard coded runtime data
this._Title = "Title in Run time"; this._Description = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna."; } // Create the OnPropertyChanged method to raise the event // Use in MVVM public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } } }
<Page x:Class="DesignTimeData.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:DesignTimeData" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:runtime="using:DesignTimeData.Runtime" mc:Ignorable="d"> <Page.DataContext> <runtime:MainViewModel/> </Page.DataContext>
3 .Then Create New Class which have exact name of your view model. Better to use different namespace / folder . In this case It is MainViewModel namespace DesignTimeData.DesignTimedata { public class MainViewModel { public string Title { get { return "DesignTime Title"; } } public string Description { get { return "Design time description"; } } } }
mc:Ignorable="d"with every page. something defines under this tag will not be displayed in the runtime. then this is the one that we need to use.
<Page x:Class="DesignTimeData.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:DesignTimeData" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:desgn="using:DesignTimeData.DesignTimedata" xmlns:runtime="using:DesignTimeData.Runtime" mc:Ignorable="d"> <Page.DataContext> <runtime:MainViewModel/> </Page.DataContext> <d:Page.DataContext> <desgn:MainViewModel /> </d:Page.DataContext> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel Margin="100,150,0,0"> <TextBlock Style="{StaticResource HeaderTextBlockStyle}" Text="{Binding Title}"/> <TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="{Binding Description}"/> </StackPanel> </Grid> </Page>

public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btnNormal_Click(object sender, RoutedEventArgs e) { resultTxt.Text = getString("sample"); independentWork(); } private async void btnAsync_Click(object sender, RoutedEventArgs e) { resultTxt.Text = await getStringAsync(); } /// <summary> /// Method with waiting in synchronize way /// /// /// </summary> /// <returns></returns> private string getString(string name) { //Task.Delay(2000); Thread.Sleep(2000); return "My String"; } /// <summary> /// same method calling from async /// </summary> /// <returns></returns> private async Task<string> getStringAsync() { Task<string> asyncTask = Task.Factory.StartNew(() => getString("sample")); independentWork(); return await asyncTask; } /// <summary> /// this work is independent from others /// </summary> private void independentWork() { resultTxt.Text += "\n independent work is done "; } }
