0

我使用 Devexpress 和 Ado.net 连接模式与数据库制作了 WPF 应用程序。我使用其他语言工作,现在转向 C#,我是 WPF 新手。我编写了这段代码来编辑单个和分组的行:

void EditRow(int focRowHand, nrcsaEntities a)
        {

        Name nametext = grid.GetRow(focRowHand) as Name;      
        try
            {
            if (nametext.Name1 != string.Empty)
                {

                update_id = nametext.PK_ID;
                txtName2.Text = update_text = nametext.Name1;

                if (Panel3.Visibility == System.Windows.Visibility.Visible)
                    {
                    Panel1.Visibility = System.Windows.Visibility.Visible;
                    Panel3.Visibility = System.Windows.Visibility.Collapsed;
                    }

                else
                    {
                    Panel1.Visibility = System.Windows.Visibility.Collapsed;
                    Panel3.Visibility = System.Windows.Visibility.Visible;
                    }
                }
            }
        catch (Exception err)
            {
            DXMessageBox.Show(err.StackTrace);

            }

        }

    private void ToggleButton1_Copy_Click(object sender, RoutedEventArgs e)
        {

        if (view.FocusedRowHandle == -1)
            {
            DXMessageBox.Show("Please Select any Item From Grid List");
            }
        else
            {
            try
                {
                int FocRowHand = view.FocusedRowHandle;
                nrcsaEntities a = new nrcsaEntities();

                if (grid.IsGroupRowHandle(FocRowHand))
                    {
                    int childCount = grid.GetChildRowCount(FocRowHand);
                    for (int i = 0; i < childCount; i++)
                        {
                        int childHandle = grid.GetChildRowHandle(FocRowHand, i);
                        EditRow(childHandle, a);
                        }
                    }
                else
                    {
                    EditRow(FocRowHand, a);

                    }

                }
            catch (Exception ee)
                {
                DXMessageBox.Show(ee.StackTrace);
                }
            }

由于我的客户要求生成高质量的代码。可能会有超过 1000 名用户使用此应用程序,并且可以保存超过 5000 个用户数据,我的问题是:因为我有更少的时间将我的应用程序提交给我的客户。如果我想将此代码转换为 MVVM,如何执行此操作,因为转换对我来说有点复杂。其次,您如何看待这种代码质量。我对此很困惑。我期待着您的回复。

4

2 回答 2

4

All I can tell you is that if you convert this application to WPF and MVVM in particular, then you will need to re-write a LOT of code. One problem is that WPF is very different from other languages... most developers face an uphill struggle when first learning WPF because of these differences.

Then, if you want to use the MVVM design pattern, things become even more different... for example, it is generally frowned upon to write code in code behind files. Of course, it is possible to use the code behind files, but we tend to implement Attached Properties that 'wrap' the functionality of these UI control event handlers, such as SelectionChanged instead.

Typically, the code that you have shown us would have to be moved into a view model, but then you'd lose the ability to reference your controls and so you'd have to find other ways to re-implement the same behaviour, but in an MVVM way. You'd also need to implement a load of ICommand instances to replace a lot of your Click handlers, etc.

Now, I must admit that my comments so far may make you think that I am not recommending that you convert your project. However, I am not not recommending that you convert your project. There are great benefits to WPF and using the MVVM pattern... the graphics, animations, styles and the ability to make any control look like anything else among other things.

The last point that I'd like to make relates to your question about 'big data'. WPF is a processor hungry framework... there's no way around this. It can be slow when rendering tens of thousands of data items into pretty UI elements. However, after working on a large scale application for a couple of years now, I've found that there are ways of improving the situation.

We can make WPF use the power of installed graphics cards, or use virtualised panels that only load data for the visible items, among other things. You really need to hear this negative side of WPF before you start your conversion, because once it has been converted, it'll be too late. I would also recommend that the computers that will run the application are made powerful enough, one way or another.

I hope this 'summary' has not been too negative for you and has helped in some way. I would like to end by saying that I personally am a huge fan of both WPF and MVVM.

于 2013-08-30T15:52:07.193 回答
1

我建议你从你的客户那里多花几天时间,然后去Devexpress MVVM Scaffolding Wizard它可以使用了。您只需连接到数据库,只需更改应用程序的接口。

于 2013-08-30T16:04:26.573 回答