2013-08-20 69 views
0

我已經爲wpf中的一個簡單計算器編寫了代碼。WPF中計算器的MVVM代碼

我MainWindow.Xaml代碼..

<Window x:Class="CalculatorNew.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid ContextMenuClosing="Multi"> 
    <TextBox x:Name="TextBox1" HorizontalAlignment="Left" Height="23" Margin="216,23,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.679,0.552"/> 
    <TextBox x:Name="TextBox2" HorizontalAlignment="Left" Height="23" Margin="216,76,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.679,0.552"/> 
    <TextBox x:Name="TextBox3" HorizontalAlignment="Left" Height="23" Margin="216,121,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.679,0.552"/> 
    <TextBlock HorizontalAlignment="Left" Margin="68,30,0,0" TextWrapping="Wrap" Text="First Number" VerticalAlignment="Top" RenderTransformOrigin="1.137,1.259" Height="16" Width="80"/> 
    <TextBlock HorizontalAlignment="Left" Margin="68,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="1.137,1.259" Height="16" Width="80" Text="Result"/> 
    <TextBlock HorizontalAlignment="Left" Margin="68,83,0,0" TextWrapping="Wrap" VerticalAlignment="Top" RenderTransformOrigin="1.137,1.259" Height="16" Width="99" Text="Second Number"/> 
    <Button Content="+" HorizontalAlignment="Left" Margin="73,172,0,0" VerticalAlignment="Top" Width="75" Click="Add"/> 
    <Button Content="-" HorizontalAlignment="Left" Margin="179,172,0,0" VerticalAlignment="Top" Width="75" Click="Sub"/> 
    <Button Content="*" HorizontalAlignment="Left" Margin="286,172,0,0" VerticalAlignment="Top" Width="75" Click="Multi"/> 
    <Button Content="/" HorizontalAlignment="Left" Margin="392,172,0,0" VerticalAlignment="Top" Width="75" Click="Div"/> 

    </Grid> 

我MainWindowdow.Xaml.cs代碼..

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace CalculatorNew 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 





    private void Add(object sender, RoutedEventArgs e) 
    { 
     TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) + System.Convert.ToDecimal(TextBox2.Text)).ToString(); 
    } 

    private void Sub(object sender, RoutedEventArgs e) 
    { 
     TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) - System.Convert.ToDecimal(TextBox2.Text)).ToString(); 
    } 



    private void Div(object sender, RoutedEventArgs e) 
    { 
     TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text)/System.Convert.ToDecimal(TextBox2.Text)).ToString(); 
    } 

    private void Multi(object sender, RoutedEventArgs e) 
    { 
     TextBox3.Text = (System.Convert.ToDecimal(TextBox1.Text) * System.Convert.ToDecimal(TextBox2.Text)).ToString(); 
    }  

} 

}

我是新來WPF和MVVM。所以我發現用MVVM格式編寫它有點困難。

有人能告訴我如何以MVVM格式重寫它,以便我可以用它作爲未來的參考嗎?

+0

如果您有Visual Studio 2010,請給[MVVM In The Box](http://karlshifflett.wordpress.com/2010/11/07/in-the-box -ndash-mvvm-training /)一看,這是學習MVVM的好方法。 – JMK

回答

5

我認爲你的問題很有可能被關閉,因爲它沒有表現出最低限度的願意爲你做出努力。請閱讀教程,網上有很多內容,並嘗試首先了解這些概念。您至少需要了解ViewModel是什麼,以及View如何與它關聯(綁定)。

你很可能不會得到正確的工作代碼,它完全符合你在這個網站上想要的,我對於一個更有興趣解釋事情的工作方式。

無論如何,爲了得到你想要的東西,你需要一個充當ViewModel的類,它的屬性代表你使用的概念。你可以有表示操作數的小數屬性和表示操作符的東西,以及表示UI中每個按鈕的命令。然後,您需要將UI元素(文本框,按鈕)鏈接到ViewModel中的屬性的綁定,並且需要將ViewModel的實例設置爲您感興趣的UI元素的容器的DataContext(它是一個Window ,網格或其他)。我認爲這幾乎總結了它...