使用unity添加依赖注入后为什么按钮被禁用?

时间:2017-06-29 20:17:49

标签: c# .net wpf xaml

我不知道为什么会发生这种奇怪的事情,下面是Xaml。对不起,我真的是WPF的新手。

<Window x:Class="ATMMachine.MainWindow"
    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:ATMMachine"
    mc:Ignorable="d"
    Title="ATM" Height="350.491" Width="500.451">
<Grid Margin="5,5,-379,5" Background="#11263B">
    <Label x:Name="label" Content="Please enter your account." HorizontalAlignment="Left" Margin="54,98,0,0" VerticalAlignment="Top" FontFamily="Courier New" FontSize="13.333" Foreground="#FFEFF8FD" Height="23" Width="244"/>
    <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="298,98,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="101"/>
    <Grid Margin="-1,2,1,-2" Background="#11263B" Height="300">
        <Label x:Name="label1" Content="Please enter your Pin." HorizontalAlignment="Left" Margin="60,98,0,0" VerticalAlignment="Top" FontFamily="Courier New" FontSize="13.333" Foreground="#FFEFF8FD" Height="23" Width="239"/>
        <TextBox x:Name="txtBoxAccount" Height="23" Margin="304,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150" HorizontalAlignment="Left" FontSize="18.667" 
                 FontWeight="Bold" FontFamily="Segoe UI Light" 
                 Text="{Binding Path=Pin}"/>

        <TextBox x:Name="txtBoxName"
                 HorizontalAlignment="Left" 
                 Height="23" 
                 Margin="304,53,0,0" 
                 TextWrapping="Wrap" 
                 VerticalAlignment="Top" 
                 Width="150" 
                 Text="{Binding Path=Number}"/>

        <Label x:Name="label1_Copy" 
               Content="Please enter your Account." 
               HorizontalAlignment="Left" 
               Margin="60,56,0,0" 
               VerticalAlignment="Top" 
               FontFamily="Courier New" 
               FontSize="13.333" 
               Foreground="#FFEFF8FD" 
               Height="23" 
               Width="239"
         />
        <Button x:Name="btnSignIn" 
                Content="Sign In" 
                HorizontalAlignment="Left" 
                Margin="304,188,0,0" 
                VerticalAlignment="Top" 
                Width="150" 
                Height="44" 
                Command="{Binding SignInCommand}"
         />
    </Grid>
</Grid>

App.xaml中。

<Application x:Class="ATMMachine.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:ATMMachine"
        >
<Application.Resources>

</Application.Resources>

以下是App.xaml.cs`中的OnStartUp覆盖

public partial class MainWindow : Window
{
    public MainWindow(IViewMainWindowViewModel viewModel)
    {
        InitializeComponent();
        DataContext = viewModel;
    }

以下是名为BankViewModel的viewmodel类

 public class BankViewModel : INotifyPropertyChanged, IViewMainWindowViewModel
{
    public ICustomerRepository customerRepository { get; private set; }

    public BankViewModel(ICustomerRepository customerRepo)
    {
        customerRepository = customerRepo;
    }

    public BankViewModel()
    {
        _canExecute = true;
    }

private ICommand _signInCommand;
    public ICommand SignInCommand
    {
        get
        {
            return _signInCommand ?? (_signInCommand = new ATMCommandHandler(() => AuthenticateUser(), _canExecute));
        }
    }

    private bool _canExecute;

  }

我正在附加禁用“签名”按钮的图片。我不知道通过统一注册类型与按钮禁用或启用有什么关系。 enter image description here

1 个答案:

答案 0 :(得分:2)

BankViewModel有两个ctors,确保实际执行_canExecute = true行。如果需要,您可以考虑使用ctors。

public BankViewModel(ICustomerRepository customerRepo)
   :this()
{
    customerRepository = customerRepo;
}

public BankViewModel()
{
    _canExecute = true;
}