【发布时间】:2012-05-09 19:50:18
【问题描述】:
我有一个 WPF 项目,它只是几十个小工具的集合。我将每个工具放在主 tabcontrol 的一个 tabitem 上,并为每个 tabitem 编写了一个部分 MainWinow 类。但是,由于工具之间的关系很小,我更愿意密封每个工具,以免它们相互干扰。此外,我听说偏班是邪恶的。这里的问题是 MainWindow 以外的一个类很难与 UI 项进行通信(据我所知)。关于我应该去哪里的任何建议?
非常感谢。
应 Blam 的要求,这是我当前代码的简化版本。原代码太大,这里贴不出来。
xaml:
<Window x:Class="WpfApplication7.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>
<TabControl Margin="0,0,0,0" Name="tabControl1">
<TabItem Header="Tool1" Name="Tool1">
<Grid>
<Label Name="lblTool1"/>
</Grid>
</TabItem>
<TabItem Header="Tool2" Name="Tool2">
<Grid>
<Label Name="lblTool2"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
部分类 1 (MainWindow.xaml.cs):
namespace WpfApplication7
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string caption = "Tool Collection";
public MainWindow()
{
InitializeComponent();
this.Title = caption;
InitialTool1();
InitialTool2();
}
/*
Some other methods for main window, including those for menu bar, tool bar, ...
*/
}
}
部分类 2 (Tool1.cs):
namespace WpfApplication7
{
string tool1Details = "This is tool 1";
/*
Other parameters related to tool1
*/
public partial class MainWindow : Window
{
public void InitialTool1()
{
lblTool1.Content = tool1Details;
}
/*
Some other methods that communicate with tabitem1
*/
}
}
部分类 3 (Tool2.cs):
namespace WpfApplication7
{
public partial class MainWindow : Window
{
string tool2Detail = "This is tool 2";
/*
Other parameters related to tool2
*/
public void InitialTool2()
{
lblTool2.Content = tool2Detail;
}
/*
Some other methods that communicate with tabitem2
*/
}
}
将它们拆分为部分类的目的是我们可以将它们放在不同的文件中。
【问题讨论】:
-
我肯定不关注你能贴一些代码吗?
-
有没有办法让一个班级相信一个单独的tabitem就是世界,并让它的行为像与那个单独的tabitem对应的MainWindow?
-
我会考虑让每个工具都成为自己的用户控件。
-
你能提供更多细节吗?你的意思是我应该为每个工具创建一个控件,然后将它们添加到大 tabcontrol 上?
-
谢谢,mdm20。我会采用你的方式。但是,由于您使用的是 cmets,因此我无法将您的设置为答案。我很抱歉。
标签: wpf oop partial-classes