えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

MVVM Light の DialogMessage 用の ビヘイビア

MVVM Light の DialogMessage 用の ビヘイビア (Behavior) を作ってみた。
これで UnitTest も問題ない。

DialogMessageAction.cs

using
System.Windows;
using System.Windows.Interactivity;
using GalaSoft.MvvmLight.Messaging;

namespace
BehaviorsSample
{
    public class DialogMessageAction : TriggerAction<DependencyObject>
    {
        public DialogMessageAction()
        {
        }
        protected override void Invoke(object parameter)
        {
            DialogMessage message = parameter as DialogMessage;
            if (message != null)
            {
                MessageBoxResult result = MessageBox.Show(message.Content, message.Caption, message.Button);
                message.Callback(result);
            }
        }
    }
}

DialogMessageTrigger.cs

using
System.Windows;
using System.Windows.Interactivity;
using GalaSoft.MvvmLight.Messaging;

namespace
BehaviorsSample
{
    public class DialogMessageTrigger : TriggerBase<DependencyObject>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            Messenger.Default.Register<DialogMessage>(base.AssociatedObject,
                p => this.InvokeActions(p));
        }
        protected override void OnDetaching()
        {
            base.OnDetaching();
            Messenger.Default.Unregister<DialogMessage>(base.AssociatedObject);
        }
    }
}

MainWindow.xaml

<
Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
        xmlns:local="clr-namespace:BehaviorsSample"
        x:Name="window" x:Class="Window"
        Title="MainWindow"  Height="300" Width="300"
        >
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

       
<i:Interaction.Triggers>
                <local:DialogMessageTrigger>
                        <local:DialogMessageAction/>
                </local:DialogMessageTrigger>
        </i:Interaction.Triggers>

        <Window.DataContext>
                <Binding Path="Main" Source="{StaticResource Locator}"/>
        </Window.DataContext>

   
<Grid x:Name="LayoutRoot">
        <Button x:Name="ExecuteButton" Width="100" Height="100" Content="
">
                <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{Binding ShutdownCommand, Mode=OneWay}"/>
                        </i:EventTrigger>
                </i:Interaction.Triggers>
        </Button>
    </Grid>
</
Window>

MainWindow.cs は消してます。
http://blogs.wankuma.com/mnow/archive/2010/11/10/194751.aspx


MainViewModel.cs

using
System.ComponentModel;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace
BehaviorsSample.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            if (IsInDesignMode)
            {
            }
            else
            {
            }
            ShutdownCommand = new RelayCommand(() => Shutdown());
        }

        private RelayCommand _shutdownCommand;
        public RelayCommand ShutdownCommand
        {
            get
            {
                return _shutdownCommand;
            }
            private set
            {
                _shutdownCommand = value;
                RaisePropertyChanged("ShutdownCommand");
            }
        }

        private void Shutdown()
        {
            if (ConfirmShutdown())
            {
                App.Current.Shutdown();
            }
        }

        private MessageBoxResult _messageBoxResult;
        private bool ConfirmShutdown()
        {
            DialogMessage message = new DialogMessage("
終了してよろしいですか", p => ShutdownCallback(p));
            message.Caption = "
終了確認";
            message.Button = MessageBoxButton.OKCancel;
            message.Icon = MessageBoxImage.Question;
            _messageBoxResult = MessageBoxResult.Cancel;
            Messenger.Default.Send<DialogMessage>(message);
            if (_messageBoxResult == MessageBoxResult.OK)
            {
                return true;
            }
            return false;
        }

        private void ShutdownCallback(MessageBoxResult result)
        {
            _messageBoxResult = result;
            if (result == MessageBoxResult.OK)
            {
                ViewModelLocator.Cleanup();
            }
        }

        public override void Cleanup()
        {
            _shutdownCommand = null;
            base.Cleanup();
        }
    }
}


投稿日時 : 2010年12月14日 17:45

コメントを追加

# 
Twitter Trackbacks for

MVVM Light ??? DialogMessage ?????? ???????????????
[wankuma.com]
on Topsy.com
2010/12/14 19:16 Pingback/TrackBack


Twitter Trackbacks for

MVVM Light ??? DialogMessage ?????? ???????????????
[wankuma.com]
on Topsy.com

# Useful info. Lucky me I discovered your web site by accident, and I am stunned why this accident didn't came about earlier! I bookmarked it. 2019/05/03 14:18 Useful info. Lucky me I discovered your web site b

Useful info. Lucky me I discovered your web site by accident, and I am stunned why this accident didn't came about earlier!
I bookmarked it.

# Hey there, You have done an incredible job. I'll certainly digg it and personally suggest to my friends. I am sure they'll be benefited from this site. 2019/05/06 9:55 Hey there, You have done an incredible job. I'll

Hey there, You have done an incredible job. I'll certainly digg
it and personally suggest to my friends. I
am sure they'll be benefited from this site.

# What's up, just wanted to say, I liked this post. It was funny. Keep on posting! 2019/07/23 18:49 What's up, just wanted to say, I liked this post.

What's up, just wanted to say, I liked this post. It was funny.
Keep on posting!

# What's up, just wanted to say, I liked this post. It was funny. Keep on posting! 2019/07/23 18:50 What's up, just wanted to say, I liked this post.

What's up, just wanted to say, I liked this post. It was funny.
Keep on posting!

# What's up, just wanted to say, I liked this post. It was funny. Keep on posting! 2019/07/23 18:51 What's up, just wanted to say, I liked this post.

What's up, just wanted to say, I liked this post. It was funny.
Keep on posting!

# What's up, just wanted to say, I liked this post. It was funny. Keep on posting! 2019/07/23 18:52 What's up, just wanted to say, I liked this post.

What's up, just wanted to say, I liked this post. It was funny.
Keep on posting!

# I believe this is one of the such a lot vital information for me. And i am happy studying your article. But should remark on few normal things, The web site style is ideal, the articles is actually excellent : D. Just right activity, cheers 2019/08/15 6:27 I believe this is one of the such a lot vital info

I believe this is one of the such a lot vital information for me.
And i am happy studying your article. But should remark on few normal things,
The web site style is ideal, the articles is actually excellent : D.
Just right activity, cheers

# I believe this is one of the such a lot vital information for me. And i am happy studying your article. But should remark on few normal things, The web site style is ideal, the articles is actually excellent : D. Just right activity, cheers 2019/08/15 6:28 I believe this is one of the such a lot vital info

I believe this is one of the such a lot vital information for me.
And i am happy studying your article. But should remark on few normal things,
The web site style is ideal, the articles is actually excellent : D.
Just right activity, cheers

# I believe this is one of the such a lot vital information for me. And i am happy studying your article. But should remark on few normal things, The web site style is ideal, the articles is actually excellent : D. Just right activity, cheers 2019/08/15 6:29 I believe this is one of the such a lot vital info

I believe this is one of the such a lot vital information for me.
And i am happy studying your article. But should remark on few normal things,
The web site style is ideal, the articles is actually excellent : D.
Just right activity, cheers

# I believe this is one of the such a lot vital information for me. And i am happy studying your article. But should remark on few normal things, The web site style is ideal, the articles is actually excellent : D. Just right activity, cheers 2019/08/15 6:30 I believe this is one of the such a lot vital info

I believe this is one of the such a lot vital information for me.
And i am happy studying your article. But should remark on few normal things,
The web site style is ideal, the articles is actually excellent : D.
Just right activity, cheers

# Hello to every body, it's my first pay a visit of this web site; this web site consists of awesome and in fact fine information in favor of readers. 2019/09/03 5:53 Hello to every body, it's my first pay a visit of

Hello to every body, it's my first pay a visit of this web site; this
web site consists of awesome and in fact fine information in favor of readers.

# Hello to every body, it's my first pay a visit of this web site; this web site consists of awesome and in fact fine information in favor of readers. 2019/09/03 5:54 Hello to every body, it's my first pay a visit of

Hello to every body, it's my first pay a visit of this web site; this
web site consists of awesome and in fact fine information in favor of readers.

# Hello to every body, it's my first pay a visit of this web site; this web site consists of awesome and in fact fine information in favor of readers. 2019/09/03 5:55 Hello to every body, it's my first pay a visit of

Hello to every body, it's my first pay a visit of this web site; this
web site consists of awesome and in fact fine information in favor of readers.

# Hello to every body, it's my first pay a visit of this web site; this web site consists of awesome and in fact fine information in favor of readers. 2019/09/03 5:56 Hello to every body, it's my first pay a visit of

Hello to every body, it's my first pay a visit of this web site; this
web site consists of awesome and in fact fine information in favor of readers.

# I enjoy, result in I found just what I used to be taking a look for. You've ended my four day long hunt! God Bless you man. Have a great day. Bye 2021/08/24 18:17 I enjoy, result in I found just what I used to be

I enjoy, result in I found just what I used to be taking a look for.
You've ended my four day long hunt! God Bless you man.
Have a great day. Bye

# Great article. I will be dealing with some of these issues as well.. 2021/09/04 11:58 Great article. I will be dealing with some of thes

Great article. I will be dealing with some of
these issues as well..

# Great article. I will be dealing with some of these issues as well.. 2021/09/04 11:59 Great article. I will be dealing with some of thes

Great article. I will be dealing with some of
these issues as well..

# Great article. I will be dealing with some of these issues as well.. 2021/09/04 12:01 Great article. I will be dealing with some of thes

Great article. I will be dealing with some of
these issues as well..

# Great article. I will be dealing with some of these issues as well.. 2021/09/04 12:02 Great article. I will be dealing with some of thes

Great article. I will be dealing with some of
these issues as well..

# Magnificent site. A lot of useful info here. I'm sending it to some pals ans additionally sharing in delicious. And naturally, thanks on your sweat! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery 2021/09/13 4:53 Magnificent site. A lot of useful info here. I'm s

Magnificent site. A lot of useful info here. I'm sending it to
some pals ans additionally sharing in delicious.
And naturally, thanks on your sweat! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Incredible points. Solid arguments. Keep up the amazing work. 2021/10/26 23:11 Incredible points. Solid arguments. Keep up the am

Incredible points. Solid arguments. Keep up the amazing
work.

# Incredible points. Solid arguments. Keep up the amazing work. 2021/10/26 23:12 Incredible points. Solid arguments. Keep up the am

Incredible points. Solid arguments. Keep up the amazing
work.

# Incredible points. Solid arguments. Keep up the amazing work. 2021/10/26 23:13 Incredible points. Solid arguments. Keep up the am

Incredible points. Solid arguments. Keep up the amazing
work.

# Incredible points. Solid arguments. Keep up the amazing work. 2021/10/26 23:14 Incredible points. Solid arguments. Keep up the am

Incredible points. Solid arguments. Keep up the amazing
work.

# Heya i am for the primary time here. I found this board and I find It truly helpful & it helped me out much. I am hoping to present one thing back and help others such as you helped me. 2021/11/12 11:38 Heya i am for the primary time here. I found this

Heya i am for the primary time here. I found this board and I find It
truly helpful & it helped me out much. I am hoping to present one thing back and help others
such as you helped me.

# buy clomid online without prescription http://clomidfast.site/ 2022/04/12 13:14 Clomids

buy clomid online without prescription http://clomidfast.site/

# online order prednisone 10mg http://prednisoneen.store/ 2022/04/16 23:08 Prednisone

online order prednisone 10mg http://prednisoneen.store/

# generic lasix https://buylasix.icu/
lasix 2022/06/24 17:25 LasixRx

generic lasix https://buylasix.icu/
lasix

# overseas pharmacies https://allpharm.store/ 2022/07/21 22:22 AllPharm

overseas pharmacies https://allpharm.store/

# ivermectin approved by fda https://stromectolbestprice.com/ 2022/07/30 0:48 BestPrice

ivermectin approved by fda https://stromectolbestprice.com/

# order prednisone from canada https://deltasone.icu/
1250 mg prednisone 2022/08/22 9:59 Prednisone

order prednisone from canada https://deltasone.icu/
1250 mg prednisone

# male erection pills https://ed-pills.xyz/
impotence pills 2022/09/15 19:27 EdPills

male erection pills https://ed-pills.xyz/
impotence pills

# medications for ed https://ed-pills.xyz/
cures for ed 2022/09/16 19:54 EdPills

medications for ed https://ed-pills.xyz/
cures for ed

# pills for erection https://ed-pills.xyz/
how to cure ed 2022/09/17 8:05 EdPills

pills for erection https://ed-pills.xyz/
how to cure ed

タイトル
名前
URL
コメント