将github上C#编写的wpf简单的命令教程示例移植成VB.Net编写

本文分享了如何将C#编写的WPF教程中的MainWindow代码从C#移植到VB.NET,包括RoutedCommand的实现与事件处理。通过实例展示了如何在VB.NET环境下保持功能一致性,适用于VB.Net开发者的需求。

大家好,现在大部分的WPF教程文档几乎都是C#,我是喜欢用VB.Net,想找个教程文档实在是太难了,今天就是把github上的C#教程程序移植成VB.net,原教程链接
主要移植教程中MainWindow.cs文件,原文件程序如下:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace CustomRoutedCommand
{
	/// <summary>	
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public static RoutedCommand ColorCmd = new RoutedCommand();	
		public MainWindow()
		{
			InitializeComponent();
		}
	// ExecutedRoutedEventHandler for the custom color command.
		private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
		{
			var target = e.Source as Panel;		
			if (target != null)
			{
				target.Background = target.Background == Brushes.AliceBlue ? Brushes.LemonChiffon : Brushes.AliceBlue;
			}
	}
		// CanExecuteRoutedEventHandler for the custom color command.
		private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			if (e.Source is Panel)
			{
				e.CanExecute = true;
			}
			else
			{
				e.CanExecute = false;
			}
		}
	}
}

下面使用VB.Net进行移植,MainWindow.xaml.vb,程序如下:

Class MainWindow
	Public Shared ColorCmd As RoutedCommand = New RoutedCommand
	Public Sub New()
		InitializeComponent()
	End Sub
	Private Sub ColorCmdExecuted(sender As Object, e As ExecutedRoutedEventArgs)
		Dim target = TryCast(e.Source, Panel)
		If target IsNot Nothing Then
			target.Background = IIf(target.Background Is Brushes.AliceBlue, Brushes.LemonChiffon, Brushes.AliceBlue)
		End If
	End Sub
	Private Sub ColorCmdCanExecute(sender As Object, e As CanExecuteRoutedEventArgs)
		If TypeOf (e.Source) Is Panel Then
			e.CanExecute = True
		Else
			e.CanExecute = False
		End If
	End Sub
End Class

最终的程序效果截图,如下:
在这里插入图片描述
在这里插入图片描述
点击按钮时First StackPanel背景颜色会发生改变。这里就不贴出.xaml文件内容了,几乎不需要修改可直接使用原教程里的.xaml文件中程序,请自行查看原文。

如果喜欢我的文章请点赞、关注+收藏,谢谢大家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值