我正在嘗試使用 Microsoft MVVM 社區工具包實作中繼命令。如果我將命令代碼放在 ViewModel 中,它就可以作業。如果我嘗試將命令代碼放在它自己的檔案夾中,則找不到命令代碼。我在下面創建了一個非常簡單的示例,將命令代碼放在 ViewModel 中(已注釋掉),并嘗試將命令 ccode 放在它自己的檔案夾中。我懷疑我在檔案夾中的代碼實作都是錯誤的。我沒有在它自己的檔案夾中找到使用中繼命令的命令代碼示例。我可以使用 ICommand 和 CommandBase 將命令代碼放在它自己的檔案夾中。
應用程式.xaml.cs
using RelayCommandLocation.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace RelayCommandLocation
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
RelayCommandView relayCommandView = new RelayCommandView();
relayCommandView.DataContext = new RelayCommandViewModel();
relayCommandView.Show();
}
}
}
檔案夾 ViewModels 中的 ViewModel
using Microsoft.Toolkit.Mvvm.Input;
using RelayCommandLocation.Commands;
using System.Windows.Input;
namespace RelayCommandLocation.ViewModels
{
public class RelayCommandViewModel : ObservableObject
{
public ICommand PushMeClick { get; set; }
// ################################################################################
// ############# This fails to compile = cannot find PushMeCommand code
public RelayCommandViewModel()
{
PushMeClick = new RelayCommand(PushMeCommandX.PushMeCommand);
}
// ********************************************************************************
// *********** This works. The RelayCommand code is in the ViewModel. *************
//public RelayCommandViewModel()
//{
// PushMeClick = new RelayCommand(PushMeCommand);
//}
//private void PushMeCommand()
//{
// MessageBox.Show($"Button pushed");
//}
}
}
檔案夾命令中的命令代碼
using System;
using System.Windows;
using System.Windows.Input;
namespace RelayCommandLocation.Commands
{
public class PushMeCommandX
{
public void PushMeCommand()
{
MessageBox.Show($"Button pushed");
}
}
}
檔案夾視圖中的 XAML
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RelayCommandLocation.Views"
mc:Ignorable="d"
Title="RelayCommandView" Height="450" Width="800">
<Grid>
<Button Content="Push Me" Command="{Binding PushMeClick}" Height="50" Width="75" />
</Grid>
</Window>
是否可以將中繼命令代碼放在自己的檔案夾中?
uj5u.com熱心網友回復:
從@Klaus Gutter 的評論中,您需要PushMeCommandX在您的RelayCommandViewModel
using Microsoft.Toolkit.Mvvm.Input;
using RelayCommandLocation.Commands;
using System.Windows.Input;
namespace RelayCommandLocation.ViewModels
{
public class RelayCommandViewModel : ObservableObject
{
public ICommand PushMeClick { get; set; }
// Create property or a field for your PushMeCommandX class.
public PushMeCommandX PushMe {get; set;}
public RelayCommandViewModel()
{
// Create an instance of the PushMeCommandX class to get access to it's methods.
PushMe = new PushMeCommandX();
PushMeClick = new RelayCommand(PushMe.PushMeCommand);
}
}
}
我將與@Sir Rufo 一起閱讀一些關于 C# 類的教程。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/448341.html
