引用prism的MVVM示例

VS2013测试通过by一剑

MainModel.cs

using System.ComponentModel;

namespace referencePrismMVVM.Model
{
    public class MainModel
    {
        public int Number1 { get; set; }

        public int Number2 { get; set; }

        public int Result { get; set; }
    }
}

 MainViewModel.cs

using referencePrismMVVM.Model ;
using Microsoft.Practices.Prism.Commands;
using System.ComponentModel;

namespace referencePrismMVVM.VideModel
{
    public class MainViewModel:INotifyPropertyChanged 
    {
        public ICommand AddCommand { get; private set; }
        public MainViewModel()
        {
            this.AddCommand = new DelegateCommand<object>(this.OnSubmit);
        }

        private void OnSubmit(object obj)
        {
            Result = Number1 + Number2;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public int Number1 { get; set; }

        public int Number2 { get; set; }

        private int result;

        public int Result
        {
            get
            {
                return this.result;
            }
            set
            {
                if (value != this.result)
                {
                    this.result = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("Result"));
                    }
                }
            }
        }
    }
}

 

 MainPage.xaml

<UserControl
    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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="referencePrismMVVM.MainPage"
    mc:Ignorable="d" 
    xmlns:ds="clr-namespace:referencePrismMVVM.VideModel"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.DataContext>
        <ds:MainViewModel/>
    </UserControl.DataContext>

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox Text="{Binding Number1, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="38,142,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="72"/>
        <TextBox Text="{Binding Number2, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="154,142,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="72"/>
        <Button x:Name="AddButton" Command="{Binding AddCommand}" Content="=" HorizontalAlignment="Left" Margin="243,142,0,0" VerticalAlignment="Top" Width="37"/>
        <sdk:Label HorizontalAlignment="Left" Height="19" Margin="128,145,0,0" VerticalAlignment="Top" Width="15" Content="+"/>
        <sdk:Label Content="{Binding Result,Mode=TwoWay}" HorizontalAlignment="Left" Height="16" Margin="295,145,0,0" VerticalAlignment="Top" Width="71"/>
    </Grid>
</UserControl>

 

转载于:https://www.cnblogs.com/aswordok/p/3641133.html

WPF Prism MVVM 中 ViewModel 的完整代码会依赖于具体的应用程序需求和数据模型设计。以下是一个简单的示例,用于展示如何实现 ViewModel。 ```csharp using Prism.Commands; using Prism.Mvvm; using System.Collections.ObjectModel; namespace MyApp.ViewModels { public class MainViewModel : BindableBase { private ObservableCollection<string> _items = new ObservableCollection<string>(); public ObservableCollection<string> Items { get { return _items; } set { SetProperty(ref _items, value); } } private string _selectedItem; public string SelectedItem { get { return _selectedItem; } set { SetProperty(ref _selectedItem, value); } } public DelegateCommand AddItemCommand { get; private set; } public DelegateCommand RemoveItemCommand { get; private set; } public MainViewModel() { AddItemCommand = new DelegateCommand(AddItem); RemoveItemCommand = new DelegateCommand(RemoveItem); } private void AddItem() { Items.Add("New Item"); } private void RemoveItem() { Items.Remove(SelectedItem); } } } ``` 在这个示例中,我们创建了一个名为 MainViewModel 的 ViewModel,其中包含一个 ObservableCollection 对象和两个 DelegateCommand 对象。Items 属性表示了一个字符串的集合,SelectedItem 属性表示选择的字符串。AddItemCommand 是一个添加字符串到集合的命令,RemoveItemCommand 是一个从集合中删除字符串的命令。这些命令都与 AddItem 和 RemoveItem 方法相关联。 此外,我们还继承了 Prism 的 BindableBase 类,这个类实现了 INotifyPropertyChanged 接口,用于通知 View 层数据发生了变化。这个基类提供了 SetProperty 方法,用于设置属性值并触发 PropertyChanged 事件。 在实际的应用中,ViewModels 的代码会更加复杂,可能需要包含多个数据模型和逻辑处理。但是,基本的原则是 ViewModel 应该与具体的 View 实现无关,View 可以随意更改,而 ViewModel 应该尽可能地保持不变。这样可以使得代码更加可维护和可扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值