C# 重写;按钮。单击+;=&引用;在WPF中使用ICommand
我正在使用一个外部SDKC# 重写;按钮。单击+;=&引用;在WPF中使用ICommand,c#,wpf,winforms,icommand,C#,Wpf,Winforms,Icommand,我正在使用一个外部SDK namespace ProSimSDK { public class ArmedFailure { ... public static event ArmedFailureEventDelegate onNew; public void Reset(); ... } } namespace ProSimSDK { public delegate void ArmedFailureEventDelega
namespace ProSimSDK
{
public class ArmedFailure
{
...
public static event ArmedFailureEventDelegate onNew;
public void Reset();
...
}
}
namespace ProSimSDK
{
public delegate void ArmedFailureEventDelegate(ArmedFailure armedFailure);
}
当我试图用WPF重写一些Winform代码时,我遇到了一些麻烦。在Winform中:
public Form1()
{
InitializeComponent();
ArmedFailure.onNew += new ArmedFailureEventDelegate(ArmedFailure_onNew);
}
// This function will be called when a new armedFailure is received
void ArmedFailure_onNew(ArmedFailure armedFailure)
{
//Here is the code I need to rewrite in WPF.
removeButton.Click += new EventHandler(delegate(object sender, EventArgs e)
{
failure.Reset();
});
}
在WPF中,我使用一个列表框。在一些指导下,我正在使用ListBox模板和命令。
在Window1.xaml中:
<DataTemplate x:Key="ListBoxItemTemplate">
<Grid>
<TextBlock x:Name="TB" Margin="5,10,5,5" Grid.Column="2" Height="23" Text="{Binding}" VerticalAlignment="Top" />
<Button HorizontalAlignment="Right" Grid.Column="3" Margin="500,10,5,0" CommandParameter="{Binding}" Command="{Binding ElementName=UC_Failures_Setting, Path=OnClickCommand}" Width="80" Click="Button_Click">remove</Button>
</Grid>
</DataTemplate>
<ListBox x:Name="listbox" ItemTemplate="{StaticResource ListBoxItemTemplate}" Margin="0,661,982,0" SelectionChanged="ListBox_SelectionChanged">
ActionCommand.cs:
public class ActionCommand: ICommand
{
private readonly Action<object> Action;
private readonly Predicate<object> Predicate;
public ActionCommand(Action<object> action) : this(action, x => true)
{
}
public ActionCommand(Action<object> action, Predicate<object> predicate)
{
Action = action;
Predicate = predicate;
}
public bool CanExecute(object parameter)
{
return Predicate(parameter);
}
public void Execute(object parameter)
{
Action(parameter);
}
public event EventHandler CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
}
remove
{
CommandManager.RequerySuggested -= value;
}
}
}
在Winform中显示?在WPF中,我不能这样写。谢谢。如果
ListBox
中填充了ArmedFailure
项,那么命令接收的参数应该是ArmedFailure
项
OnClickCommand = new ActionCommand
(
x =>
{
var failure = (ArmedFailure)x;
failure.Reset();
listbox.Items.Remove(x);
}
);
按钮中的所有内容。WinForms中的单击处理程序将成为ICommand的一部分。在wpf中执行我的工作中最简单的方法是准确地了解绑定的工作方式并遵循MVVM的工作方式,然后通过视图的XAML将按钮的单击事件绑定到ViewModel上的方法。当使用WPF时,MVVM非常接近标准。当开始在WPF中学习MVVM时,最好的两个选择是SimpleMVMToolkit(我的最爱)或MVVMLight……。如果你想在了解一切之前死去,use PRISMI发现,要真正了解WPF对绑定所做的工作,最好的方法是尝试在我自己的库中重新实现这些库的一部分,并猜测如何处理它。然后看看真实图书馆的资料,看看我有多接近。虽然速度不快,但信息量大。刚开始使用MVVM库时,我感觉自己在学习库,而不是学习WPF和MVVM。当然,不要试图用生产级代码来实现这一点。使用“玩具”应用程序进行实验。谢谢你的建议。我已经开始学习MVVM、绑定等,非常感谢!列表框应填充ArmedFailure项。@ASh
removeButton.Click += new EventHandler(delegate(object sender, EventArgs e)
{ failure.Reset(); });
OnClickCommand = new ActionCommand
(
x =>
{
var failure = (ArmedFailure)x;
failure.Reset();
listbox.Items.Remove(x);
}
);