如何正确地将此代码从代码移植到MVVM? [英] How correctly move this code from code behind to MVVM?

查看:83
本文介绍了如何正确地将此代码从代码移植到MVVM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确地将代码从代码移到mvvm?

how correctly move this code from code behind to mvvm?

namespace DemoApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ChartSurface.ChartSurface cs;
        double[] data;
        public MainWindow()
        {

            InitializeComponent();

            this.FontSizeCB.SelectionChanged += new SelectionChangedEventHandler(OnFontSizeCBChanged);
            this.FontFamilyCB.SelectionChanged += new SelectionChangedEventHandler(OnFontFamilyCBChanged);
            this.LineBrashCB.SelectionChanged += new SelectionChangedEventHandler(OnLineBrashCBChanged);
            this.LineThicknessCB.SelectionChanged += new SelectionChangedEventHandler(OnLineThicknessCBChanged);
            this.XAxisTitleTB.TextChanged += new TextChangedEventHandler(OnXAxisTitleTBChanged);
            this.YAxisTitleTB.TextChanged += new TextChangedEventHandler(OnYAxisTitleTBChanged);
            this.SinewaveRB.Checked += new RoutedEventHandler(OnTWaveRBChecked);
            this.SquarewaveRB.Checked += new RoutedEventHandler(OnTWaveRBChecked);

            cs = new ChartSurface.ChartSurface
            {
                LineData = new Double[] { 0, 6, 1, 2, 2, 4.6, 3, 3.5, 4, 1.9, 5, 3, 6, 5, 7, 9, 8, 4, 9, 2, 10, 3.5 },
                LineThickness = 2,
                LineBrush = Brushes.DarkRed,
                FontFamily = "Comic Sans MS",
                FontSize = 15,
                XAxisTitle = this.XAxisTitleTB.Text,
                YAxisTitle = this.YAxisTitleTB.Text
            };
            this.DataContext = cs;
            data = cs.LineData.ToArray();
        }

        private void OnTWaveRBChecked(object sender, RoutedEventArgs e)
        {
            if (this.SinewaveRB.IsChecked == true)
                cs.LineData = data;
            else
                cs.LineData = CreateStepLineSeries(data);
        }

        private void OnYAxisTitleTBChanged(object sender, TextChangedEventArgs e)
        {
            cs.YAxisTitle = (sender as TextBox).Text;
        }

        private void OnXAxisTitleTBChanged(object sender, TextChangedEventArgs e)
        {
            cs.XAxisTitle = (sender as TextBox).Text;
        }

        private void OnLineThicknessCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.LineThickness = Convert.ToDouble(((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string);
        }

        private void OnLineBrashCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.LineBrush = ((sender as ComboBox).SelectedItem as TextBlock).Background as Brush;
        }

        private void OnFontFamilyCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.FontFamily = ((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string;
        }

        private void OnFontSizeCBChanged(object sender, SelectionChangedEventArgs e)
        {
            cs.FontSize = Convert.ToDouble(((sender as ComboBox).SelectedItem as ComboBoxItem).Content as string);
        }

        double[] CreateStepLineSeries(double[] source)
        {
            List<double> returnValue = new List<double>();
            for (int i = 1; i < source.Length; i += 2)
            {
                double currentValueX = source[i - 1];
                double currentValueY = source[i];
                returnValue.Add(currentValueX);
                returnValue.Add(currentValueY);
                if (i < source.Length - 1)
                {
                    double nextValueX = source[i + 1];
                    returnValue.Add(nextValueX);
                    returnValue.Add(currentValueY);
                }
            }
            return returnValue.ToArray();
        }
    }
}





我尝试了什么:



我试图把这个部分移到viewmodel类中



What I have tried:

I tried to move this part into viewmodel class

cs = new ChartSurface.ChartSurface
            {
                LineData = new Double[] { 0, 6, 1, 2, 2, 4.6, 3, 3.5, 4, 1.9, 5, 3, 6, 5, 7, 9, 8, 4, 9, 2, 10, 3.5 },
                LineThickness = 2,
                LineBrush = Brushes.DarkRed,
                FontFamily = "Comic Sans MS",
                FontSize = 15,
                XAxisTitle = this.XAxisTitleTB.Text,
                YAxisTitle = this.YAxisTitleTB.Text
            };







class ViewModel
   {
       ChartSurface.ChartSurface cs;

       public ViewModel()
       {
           cs = new ChartSurface.ChartSurface
           {
               LineData = new Double[] { 0, 6, 1, 2, 2, 4.6, 3, 3.5, 4, 1.9, 5, 3, 6, 5, 7, 9, 8, 4, 9, 2, 10, 3.5 },
               LineThickness = 2,
               LineBrush = Brushes.DarkRed,
               FontFamily = "Comic Sans MS",
               FontSize = 15,
               XAxisTitle = "xx",
               YAxisTitle = "yy"
           };
       }
   }





并在代码隐藏中更改此行



and change this line in codebehind

this.DataContext = cs;





进入此



into this

this.DataContext = new ViewModel();



但它不起作用。


but it`s doesn't work.

推荐答案

引用:

this.DataContext = cs;

进入此

this.DataContext = new ViewModel();

但它不起作用。

this.DataContext = cs;
into this
this.DataContext = new ViewModel();
but it`s doesn't work.





您发布的声明不是等效。



假设第一个声明甚至工作,你的ChartSurface现在是ViewModel(类)的成员;与之前的在顶部相比。



由于ChartSurface是第三方图表,它是(视图)的一部分,而不是视图模型;理想情况下应该包含在用户控件中。



The statements you posted are not "equivalent".

Assuming the first statement even worked, your "ChartSurface" is now a "member" of the ViewModel (class); versus before when it was "at the top".

And since ChartSurface is a 3rd party chart, it's (part of) a "view" and not a "view model"; and ideally should have been wrapped in a user control.


这篇关于如何正确地将此代码从代码移植到MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆