avr

29

Mvvm Prism C#

Posted by : admin | On : 29 avril 2015

lesson dev universal app with C# and xaml

Source code Prism Part 1

<controls:PageBase x:Class="App35.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:controls="using:App35.Controls"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App35"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.Mvvm"
    prism:ViewModelLocator.AutoWireViewModel="True"
    xmlns:designtime="using:App35.DesignTime"
    mc:Ignorable="d">
    <d:PageDataContext>
        <designtime:MainPageViewModel/>
    </d:PageDataContext>
    <Hub Header="{Binding }" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    </Hub>
</controls:PageBase>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App35
{

    namespace Controls
    {
        public abstract partial class PageBase : Page, Microsoft.Practices.Prism.Mvvm.IView { }

    }

    namespace Views {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Controls.PageBase
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
        }
    }

    namespace Interfaces {
        public interface IMainPageViewwModel
        {
            public string Title { get; set; }
        }
    }

    namespace ViewModels
    {
        public class MainPageViewModel : Microsoft.Practices.Prism.Mvvm.ViewModel, Interfaces.IMainPageViewwModel
        {
            public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
            {
                this.Title = "Hello Runtime !";
            } 

            string _Title = default(string);
            public string Title { get { return _Title; } set { SetProperty(ref _Title, value); } }
        }
    }

    namespace DesignTime
    {
        public class MainPageViewModel : Interfaces.IMainPageViewwModel
        {
            public MainPageViewModel()
            {
               this.Title = "Hello this is the design time ! ";
            }
            public string Title { get; set; }
        }
    }

}

 

using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227

namespace App35
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Microsoft.Practices.Prism.Mvvm.MvvmAppBase
    {
        public App()
        {
            this.InitializeComponent();
        }

        public enum Experiences { Main }

        protected override System.Threading.Tasks.Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
        {
            this.NavigationService.Navigate(Experiences.Main.ToString(), null);
            return System.Threading.Tasks.Task.FromResult<object>(null);
        }
    }
}
<prism:MvvmAppBase
    x:Class="App35.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:App35">

</prism:MvvmAppBase>