sách gpt4 ai đã đi

wpf - 如何在 Window.Resources 标签中使用带有数据绑定(bind)的 View 模型、模型、命令类?

In lại 作者:行者123 更新时间:2023-12-03 10:14:52 32 4
mua khóa gpt4 Nike

如果 WPF MVVM 应该没有代码,为什么在使用 ICommand 时,是否需要在 Window.xaml.cs 代码中实例化 DataContext 属性?我已经并排观看并关注了 YouTube WPF MVVM、数据绑定(bind)、ICommand、INotifyPropertyChanged 视频,我很困惑。

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel.VM_CalcRblAmt();
}
}

如何将 View 模型、模型、命令类与数据绑定(bind)一起使用
在 Window.Resource 标记中?如果那是正确的 mvvm 模式? Window.Resource 标记中的以下内容(但不起作用)?
我问这个是因为在不同作者的 WPF MVVM 教程上并排看,我看不到如何将 View 模型、模型和命令类插入 UI 中的 xml 命名空间(在本例中为窗口)。例如下面:
<>
xmlns:nsviewmodel="clr-namespace:Wpf_MVVVM_Rbr.ViewModel" />



Command="{Binding CalcRbrAmtCommand}"
Content="Rebalance"/>



同样,上面的代码实际上不在 Window.Resources 之间
目前的标签。它位于 Window.Resources 标记之外的 UI XAML 代码中。目前确实有效。但是 MVVM 模式是否不需要在 Window.Resources 或 Page.Resources 标签中引用 xmlns 命名空间并为其赋予 x:Key="someKeyName"并在 Resources 标签内使用数据绑定(bind)?

因此,如果这是正确的 MVVM 实践,我如何使用数据绑定(bind)将 Window.Resources 数据绑定(bind)、 View 模型、命令和模型类放在一起?

Cảm ơn trước nhé!
真挚地,
TÔI

1 Câu trả lời

1.如果WPF MVVM应该没有代码,为什么在使用ICommand时,需要在Window.xaml.cs代码中实例化DataContext属性?
设置 DataContext 的方法有很多:

第一种方法。 鉴于:





第二种方法。 您应该覆盖 App.xaml.cs 的 OnStartUp() 方法
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow app = new MainWindow();
ProductViewModel context = new ProductViewModel();
app.DataContext = context;
app.Show();
}
}

第三种方法。 在 Windows 的构造函数中:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext=new MainWindowViewModel();
}
}

第四种方法。 您可以通过 UnityContainer 的 DependencyInjection 设置 DataContext。但是 DependencyInjection、Prism 和 UnityContainer 是其他问题,并且超出了这个问题的范围。举个例子:
protected override void RegisterTypes()
{
unityContainer.RegisterType("ModuleAUpper");
unityContainer.RegisterType();
unityContainer.RegisterTypeForNavigation();
}

2、如何使用Window.Resource标签中带有数据绑定(bind)的 View 模型、模型、命令类?如果那是正确的 mvvm 模式? Window.Resource 标记中的以下内容(但不起作用)?

Resources are just chance to store data.
要设置 viewModel 和 XAML 中的属性之间的绑定(bind),您应该:
private string message="helloWorld";    

public string Message
{
get { return message;}
set { message = value;}
}

和 XAML:

绑定(bind) Button在 MVVM 范围内:


和 View 模型:
private ICommand _clickCommand;
public ICommand ClickCommand
{
lấy
{
return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute));
}
}
private bool _canExecute;

public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
return _canExecute;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
_action();
}
}

làm mới:

我推荐你阅读最好的 MVVM tutorial by Rachel Lim我读过的。

关于wpf - 如何在 Window.Resources 标签中使用带有数据绑定(bind)的 View 模型、模型、命令类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36214234/

32 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com