This project is read-only.

How to initialise a ViewModel using ConfigurationService

Jan 19, 2013 at 6:14 PM

Hi there,

I'm new to the MVVM _and_ async/await, so I think I'm tying myself up in knots here.

My goal is to build a simple login page that will record the user's previous login details, so I have a LoginPage ViewModel that uses ConfigurationService to load & save values.

It works on Windows 8, but not on Windows RT, where the Username, Password & Account values are either not saved or not loaded (I can't tell which, would appreciate any insight on how to debug that too!)

I am initialising the ViewModel using [ActivationComplete]:

[ActivationComplete]
public async Activated()
{
    Username = await ConfigurationService.GetValue("Username", String.Empty);
    Password = await ConfigurationService.GetValue("Password", String.Empty);
    Account = await ConfigurationService.GetValue("Account", String.Empty);
}

Then later on in the same ViewModel, I save the values:

public DelegateCommand LoginCommand
{
    get
    {
        if (loginCommand = null)
        {
             loginCommand = new DelegateCommand(
                 x => DoLogin()
             );
        }
    }
}

async void DoLogin()
{
    await ConfigurationService.SetValue("Username", Username);
    await ConfigurationService.SetValue("Password", Password);
    await ConfigurationService.SetValue("Account", Account);
    ... Do some login code ...
}

I am very unhappy about having DoLogin and Activate marked as async, but I've only done it because I'm not sure how else to avoid the compiler warnings :)

Any guidance would be very much appreciated!

Thanks!

Jan 19, 2013 at 6:47 PM

Hi stony,

I'm sorry you are running into issues. The new await/async keywords are a bit different and the reason the configuration service is fully task based is because all I/O on the WinRT platform is async. That being said I think we should be able to get you up and running.

 

By default the Configuration Service will save config values in an XML file located in the deployment directory (located in you application data folder). That's probably the best way to tell if the value's are actually being saved.

 

I'll try and put together a simple example to test your use case. Can you send me your LoginViewModel class and I'll take a look at that as well (you can contact me directly through codeplex with an attachment).

Thanks

-Ian

Jan 20, 2013 at 7:46 AM

Hi Ian,

Thanks for the quick reply. I can't see how to attach anything in Codeplex, any the view model is pretty basic at the moment anyway, so I've pasted it below. My real email address is richard at teamhaven dot com, so we can take this offline if that is easier for you :)

Cheers,

R.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

using StyleMVVM.Configuration;
using StyleMVVM.DependencyInjection;
using StyleMVVM.LanguageExtensions;
using StyleMVVM.ViewModel;

using App1.DataModel;

namespace App1.ViewModels
{
    [ViewModel]
    public class LoginPageViewModel : PageViewModel
    {
        [Import]
        public IConfigurationService ConfigurationService { get; set; }

        [ActivationComplete]
        public async void Activated()
        {
            Username = await ConfigurationService.GetValue("Username", String.Empty);
            Password = await ConfigurationService.GetValue("Password", String.Empty);
            Account = await ConfigurationService.GetValue("Account", String.Empty);
        }

        private string username;
        public string Username
        {
            get { return username; }
            set { SetProperty(ref username, value); LoginCommand.RaiseCanExecuteChanged(); }
        }

        private string password;
        public string Password
        {
            get { return password; }
            set { SetProperty(ref password, value); LoginCommand.RaiseCanExecuteChanged(); }
        }

        private string account;
        public string Account
        {
            get { return account; }
            set { SetProperty(ref account, value); LoginCommand.RaiseCanExecuteChanged(); }
        }

        private DelegateCommand loginCommand;
        public DelegateCommand LoginCommand
        {
            get
            {
                if (loginCommand == null)
                {
                    loginCommand = new DelegateCommand(
                        x => DoLogin(),
                        x => !(String.IsNullOrWhiteSpace(username) || String.IsNullOrWhiteSpace(password) || String.IsNullOrWhiteSpace(account))
                        );
                }
                return loginCommand;
            }
        }

        async void DoLogin()
        {
            await ConfigurationService.SetValue("Username", Username);
            await ConfigurationService.SetValue("Password", Password);
            await ConfigurationService.SetValue("Account", Account);
        }
    }
}