月別アーカイブ: 2011年9月

【Silverlight】 カスタム検証属性を作成するには

◆検証属性(数値チェック)

using System;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace Validations.ValidationAttributes
{
  [AttributeUsage(AttributeTargets.Property
    , AllowMultiple = true, Inherited = true)]
  public class NumberStringAttribute : ValidationAttribute
  {
    public NumberStringAttribute()
    {
        // エラーメッセージ
      this.ErrorMessage = "{0}は数値文字を入力してください。";
    }

    protected override ValidationResult IsValid
      (object value, ValidationContext validationContext)
    {
      if (value.ToString().Length == 0)
        return ValidationResult.Success;

      // 数値チェック
      if (new Regex("^([0-9])*$").IsMatch(value.ToString() ))
        return ValidationResult.Success;
      else
        return new ValidationResult(string.Empty);
    }    
  }
}

◆使用例(プロパティに NumberStringAttribute を適用)

[NumberString()]
public string Age
{
  get { return Model.Age; }
  set
  {
    Model.Age = value;
    this.RaisePropertyChanged(() => Age);
  }
}

【Silverlight】 クエリ文字列を設定して、画面遷移を行うには

◆ メイン画面(XAML)

<UserControl
  x:Class="SilverlightApplication7.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:navigation="clr-namespace:System.Windows.Controls;
    assembly=System.Windows.Controls.Navigation" 
  xmlns:uriMapper="clr-namespace:System.Windows.Navigation;
    assembly=System.Windows.Controls.Navigation"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

  <Grid x:Name="LayoutRoot">
    <StackPanel>
      <Button Width="100" HorizontalAlignment="Left"
        Content="遷移" Name="btnNavigate"></Button>
      <navigation:Frame x:Name="ContentFrame" Source="/Home">
        <navigation:Frame.UriMapper>
          <uriMapper:UriMapper>
            <uriMapper:UriMapping Uri="/{pageName}"
              MappedUri="/Views/{pageName}.xaml"/>
          </uriMapper:UriMapper>
        </navigation:Frame.UriMapper>
      </navigation:Frame>
    </StackPanel>
  </Grid>

◆ メイン画面(C# Code)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace SilverlightApplication7
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            this.btnNavigate.Click += (s, e) =>
            {
                // About画面へ遷移(クエリ文字列「id」に "test" を設定)
                this.ContentFrame.Navigate(
                  new Uri("/About?id=test", UriKind.Relative));
            };

            this.ContentFrame.NavigationFailed += (s, e) =>
            {
                MessageBox.Show("画面遷移エラー");
            };            
        }        
    }
}

◆ About画面(C# Code)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace SilverlightApplication7
{
    public partial class About : Page
    {
        public About()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // クエリ文字列「id」の取得
            MessageBox.Show(this.NavigationContext.QueryString["id"]);
        }
    }
}