Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Como usar Cimbalino Windows Phone Toolkit - MessageBoxService

From Wiki
Jump to: navigation, search

Este artigo tem como objetivo mostrar como usar Cimbalino Windows Phone Toolkit – MessageBoxService.

SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata

Exemplo de código
Código fonte: MessageBoxService Example (github)

Testado com
SDK: Windows Phone 8.0 SDK, Windows Phone 7.1.1 SDK
Aparelho(s): Nokia Lumia 800, Nokia Lumia 920

Compatibilidade
Artigo
Tradução:
Por saramgsilva
Última alteração feita por saramgsilva em 23 Nov 2013

Contents

Introdução

Cimbalino Windows Phone Toolkit é um conjunto de itens uteis e poderosos para ajudar na implementação de aplicações Windows Phone. Projeto base do toolkit contendo serviços para implementação do padrão de MVVM, conversores, classes auxiliares, métodos de extensões;

Ecrã do exemplo criado

MessageBox2.png

Contruindo o exemplo

O código fonte pode ser obtido em Exemplo MessageBoxService (github).

Os pacotes estão disponíveis em Nuget Package Manager (para ambos os "targets") e podem ser instalados, para mais detalhes consultar: Como instalar os pacotes Cimbalino Windows Phone Toolkit. O exemplo deste artigo usa MVVM Light para ajudar na implementação do padrão MVVM, para mais detalhes consultar:

Registando os serviços

Devemos começar por registar cada serviço no ViewModelLocator, como podemos ver de seguida:

 using Cimbalino.Phone.Toolkit.Services;
 
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// </summary>
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
 
if (!SimpleIoc.Default.IsRegistered<IMessageBoxService>())
{
SimpleIoc.Default.Register<IMessageBoxService, MessageBoxService>();
}
 
SimpleIoc.Default.Register<MainViewModel>();
}
 
public MainViewModel MainViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
 
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}

Implementando a ViewModel

Em seguinda devemos implementar o MainViewModel, como podemos ver de seguida:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
 
using Cimbalino.Phone.Toolkit.Services;
 
using GalaSoft.MvvmLight.Command;
 
/// <summary>
/// This class contains properties that the main View can data bind to.
/// </summary>
public class MainViewModel : ViewModelBase
{
/// <summary>
/// The message box service.
/// </summary>
private readonly IMessageBoxService _messageBoxService;
 
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel(IMessageBoxService messageBoxService)
{
_messageBoxService = messageBoxService;
ShowMessageBoxCommand = new RelayCommand(ShowMessageBox);
}
 
/// <summary>
/// Shows the message box.
/// </summary>
private void ShowMessageBox()
{
_messageBoxService.Show("I am a message box", "Cimbalino sample", MessageBoxButton.OK);
}
 
/// <summary>
/// Gets the show message box command.
/// </summary>
/// <value>
/// The show message box command.
/// </value>
public ICommand ShowMessageBoxCommand { get; private set; }
}

Implementando a View

Para conetar a view model com a página devemos adicionar a ViewModelLocator no App.xaml:

 <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"  />

e adicionar o binding na página principal:


DataContext="{Binding MainViewModel, 
Source={StaticResource Locator}}"

A MainPage.xaml poderá ser algo do género:

<phone:PhoneApplicationPage x:Class="CimbalinoSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
Orientation="Portrait"
DataContext="{Binding MainViewModel,
Source={StaticResource Locator}}"

SupportedOrientations="Portrait"
shell:SystemTray.IsVisible="True"
mc:Ignorable="d">
 
<!-- LayoutRoot is the root grid where all page content is placed -->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
 
<!-- TitlePanel contains the name of the application and page title -->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Margin="12,0"
Style="{StaticResource PhoneTextTitle2Style}"
Text="Cimbalino Sample" />
<TextBlock Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle2Style}"
Text="MessageBoxService" />
</StackPanel>
 
<!-- ContentPanel - place additional content here -->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<Button Command="{Binding ShowMessageBoxCommand}" Content="Show message box" />
</Grid>
 
</Grid>
 
</phone:PhoneApplicationPage>



Código Fonte

O código fonte pode ser obtido aqui.

This page was last modified on 23 November 2013, at 19:24.
198 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×