modds 拖放编程 C# 创建类库 (DLL) - 第 5 部分





5.00/5 (4投票s)
可视化编程语言
引言
如果您想将 modds 可视化程序的某些功能用于您现有的大型项目中,那么 modds 类库模板可以帮助创建 .NET DLL,该 DLL 可用于现有的 Visual Studio 项目。本文将创建两个独立的 DLL,并将它们组合在一个 Visual Studio 项目中,以开发一个股票图表程序。
项目需求
- Modds C# 设计器 (来自 www.modds.org)
- Microsoft Visual Studio
- Windows 7 或更高版本
- .NET Framework 4.5.2 或更高版本
DLL 函数控件
DLL 函数控件是用于创建类库模板 DLL 功能界面的控件。该控件可在 modds IDE 控件工具箱 -> modds 通用控件 -> 模块 -> DLL 函数中找到。此控件只能用于类库模板。它可以创建带有任意数量参数且带或不带返回值的接口函数。
股票图表程序示例
在本例中,我们将创建两个独立的 DLL(MarketDataYahoo.dll 和 MarketDataView.dll)。MarketDataYahoo.dll 将包含一个名为“GetStockMarketData
”的接口函数。此函数将接受三个参数(股票代码、开始日期和结束日期),并且不返回任何值,而是将市场数据值广播到“StockMarketData
”消息通道。任何侦听该消息通道的其他对象都可以处理它。MarketDataView.dll 将包含一个名为“CreateStockChartView
”的接口函数。此函数不接受任何参数,并将返回一个股票图表视图对象。该对象将侦听“StockMarketData
”消息通道。当数据更新时,该对象将在其视图中显示。该对象视图可以与 WPF 的 ContentControl
进行数据绑定以进行 UI 显示。
MarketDataYahoo.dll
- 创建一个类库项目,并命名为“
MarketDataYahoo
” - 将下载源 (LIB) 中的“MarketData.dll”添加到References/ProgramDLL。MarketData.dll 是上一示例中从雅虎获取市场数据的代码。
- 创建一个消息通道,并命名为“
StockMarketData
”- 右键单击 -> Solution -> MarketDataYahoo -> Channels,选择 Add namespace 并命名为“
StockMarketData
” - 右键单击“
StockMarketData
”,选择 Add Broadcast
- 右键单击 -> Solution -> MarketDataYahoo -> Channels,选择 Add namespace 并命名为“
- 删除“DLLClass.xsml”,创建一个新的 Schema 并命名为“DataServer.xsml”。双击打开。
- 双击 Solution -> MarketDataYahoo -> References -> ProgramDLL -> MarketData.dll 在 .NET DLL 工具箱中打开它。
- 将以下控件拖到设计面板,并按以下图像连接它们
- Control Toolbox -> modds Common Controls -> Module -> DLL Function
- .NET DLL Toolbox -> MarketData.dll -> Class -> YahooMarketData -> YahooDailyMarketData
- Control Toolbox -> modds Common Controls -> Toolbox -> Group Data
- Solution -> MarketDataYahoo -> Channels -> StockMarketData -> Broadcast
StockChartView.dll
- 创建一个类库项目,并命名为“
StockChartView
” - 将下载源 (LIB) 中的“ChartConrol.dll”添加到References/ProgramDLL。ChartControl.dll 是上一示例中的 WPF 股票图表控件。
- 将下载源 (LIB) 中的“MarketDataView.xaml”添加到
SchemaView
。MarketDataView.xaml 是股票图表视图设计。 - 创建一个消息通道“
StockMarketData
”。- 右键单击 -> Solution -> StockChartView -> Channels,选择 Add namespace 并命名为“
StockMarketData
” - 右键单击“
StockMarketData
”,选择 Add Listener
- 右键单击 -> Solution -> StockChartView -> Channels,选择 Add namespace 并命名为“
- 删除“DLLClass.xsml”,创建一个新的 Schema 并命名为“StockChartView.xsml”。双击打开。
- 将以下控件拖到设计面板,并按以下图像连接它们
- Solution -> StockChartView -> SchemaView -> MarketDataView.xaml
- Solution -> Channels -> StockMarketData -> Listener
- Control Toolbox -> modds Common Controls -> Toolbox -> Group Data
- Control Toolbox -> modds Common Controls -> Toolbox -> Ungroup Data
- .NET DLL Toolbox -> CSharpCommonLibrary -> Primitive Type -> Double
- .NET DLL Toolbox -> CSharpCommonLibrary -> Class -> DateTime
- .NET DLL Toolbox -> CSharpCommonLibrary -> Class -> Object
- Properties -> DataObjects (Data, Open, High, Low, and Last)
- 双击 Solution -> StockChartView -> References -> ProgramDLL -> modds.LIB.dll 在 .NET DLL 工具箱中打开它。
- 创建一个新的 Schema 并命名为“ObjectBuilder.xsml”。双击打开。
- 将以下控件拖到设计面板,并按以下图像连接它们。
- Control Toolbox -> modds Common Controls -> Module -> DLL Function
- Solution -> StockChartView -> Schema -> StockChartView.xsml 并选择 Module Id
- .NET DLL Toolbox -> modds.LIB.dll -> Class -> GeneralObject -> CreateGeneralObject(Guid)
- 从 DLL Function 控件返回
Visual Studio 项目
创建新的 WPF 应用程序项目,并将 (MarketDataYahoo.dll 和 StockChartView
) 添加到 References。添加以下代码以完成程序。
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); chartContent.DataContext = StockChartView.ObjectBuilder.CreateStockChartView(); } private void Button_Click(object sender, RoutedEventArgs e) { string symbol = symbolBox.Text; DateTime startDate = (DateTime)startDatePicker.SelectedDate; DateTime endDate = (DateTime) endDatePicker.SelectedDate; MarketDataYahoo.DataServer.GetMarketData(symbol, startDate, endDate); } }