帮助重构XAML和C# [英] Help refactoring XAML and C#

查看:96
本文介绍了帮助重构XAML和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想重构我的代码以使所有内容变得更整洁,但我遇到了一些麻烦!

我有一个包含许多不同选项卡的主窗口,我想将这些选项卡功能放入单独的类中,但是这样做很麻烦!

这是MainWindow.xaml.cs中的服务器选项卡函数:

Hey all,

I want to refactor my code to make everything a little neater, but I am having some trouble!

I have a main window with many differnt tabs, and I would like to put these tab functions into seperate classes, but I am having trouble doing so!

Here is my server tab functions in my MainWindow.xaml.cs:

private void StartServer_Click(object sender, RoutedEventArgs e)
       {
           lblServer.Content = "Running";
           btnStartServer.IsEnabled = false;
           btnStopServer.IsEnabled = true;
           serverAPI.start();
           //            DisplayMessage("START SERVER");
       }

       private void StopServer_Click(object sender, RoutedEventArgs e)
       {
           lblServer.Content = "Stopped";
           btnStartServer.IsEnabled = true;
           btnStopServer.IsEnabled = false;
           serverAPI.stop();
       }





我希望它看起来像这样整洁一些:





I would like it set out a little neater like so:

private void StartServer_Click(object sender, RoutedEventArgs e)
       {
           ServerTab.StartServer();
       }

       private void StopServer_Click(object sender, RoutedEventArgs e)
       {
           ServerTab.StopServer();
       }




我无法从MainWindow.xaml.cs中看到这些功能,并且无法从ServerTab类更改MainWindow中的标签.

我尝试用公共局部类ServerTab:MainWindow"编写一个类,但这会带来很多问题.

对如何调用这些函数的任何帮助都将非常有用,谢谢!!




I am having trouble making these functions visible from MainWindow.xaml.cs, and I cannot change the labels in MainWindow from the ServerTab class.

I have tried writing a class with "public partial class ServerTab : MainWindow" but this comes up with a whole lot of problems.

Any help with how I can call these functions would be great, thank you so much!!!

推荐答案

将此作为一些初步注意事项:

您将功能(服务器")与UI(标签")混合在一起.

同样,硬编码的字符串"Running","Stopped"确实很糟糕,特别是对于指示状态的东西.有人告诉我您将需要在其他地方使用它们,在这种情况下,这简直是不可接受的.例如,使用枚举类型.同样,字符串仅用于表示,不用于任何功能.

还有什么?您是否知道像StartServer_Click这样的名称违反了(好的)Microsoft命名约定?该方法永远都不能那样调用.是的,它们是由Microsoft自动生成的,但是谁告诉您可以保留它们.重命名它们.而且,这是将处理程序添加到事件调用列表的过时方法.我建议不要使用Designer处理事件.使用匿名方法甚至可以使用lambda形式提供更多可支持的代码:

Consider this as some preliminary notes:

You mix functionality ("server") with UI ("tab").

Also, hard-coded strings "Running", "Stopped" are really bad, especially for something indicating status. Something tells me you will need to use them in some other place, in this case, this is simply unacceptable. Use enumeration type, for example. Again, strings are only for presentation, not for any functionality.

What else? Do you know that names like StartServer_Click violate (good) Microsoft naming conventions? The methods should never be called like that. Yes, they are auto-generated by Microsoft, but who told you that you can leave them as is. Rename them. Moreover, this is obsolete way of adding handlers to event invocation list. I recommend never using Designer for handling events. Using anonymous methods gives way more supportable code, and even in lambda form:

startServer.Click += (sender, eventArgs) => {
   // you code here:
   CallSomeMethod(); // the fact you not always use "sender" or "eventArgs" makes is more flexible;
                     // you can use signature you really need semantically.
   //use eventArgs or not...
   //use sender or not...
}



在这里,lambda形式和类型推断允许您跳过参数类型-它们是从事件的类型推断出来的.

—SA



Here, lambda form and type inference allow you to skip parameters types — they are inferred from the type of the event.

—SA


这篇关于帮助重构XAML和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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