我目前正在學習 MVVM,我遇到了這個問題。所以基本上我有一個主要的觀點,比如
<Window x:Class="ManufacturingToolV2.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:views="clr-namespace:ManufacturingToolV2.View"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid DataContext="{StaticResource viewModel}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<views:BurnInView />
</Grid>
“BurnInView”是一個單獨的視圖,有一個按鈕:
<UserControl x:Class="ManufacturingToolV2.View.BurnInView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodel="clr-namespace:ManufacturingToolV2.ViewModel"
xmlns:converter="clr-namespace:ManufacturingToolV2.Converters"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
>
<UserControl.Resources>
<viewmodel:BurnInTestViewModel x:Key="viewModel"/>
<converter:BoolToStringConverter x:Key="boolToStringConverter" />
</UserControl.Resources>
<Grid DataContext="{StaticResource viewModel}" Background="White">
<Button Content="Start Test" HorizontalAlignment="Stretch" Margin="10,86,10,0" VerticalAlignment="Top" Command="{Binding StartTestCommand, Mode=OneWay}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding TestRunning}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding TestRunning}" Value="False">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>>
</Grid>
我的“BurnInView”系結到一個視圖模型,這樣:
public class BurnInTestViewModel : ViewModelBase
{
public ICommand StartTestCommand;
public BurnInTestViewModel()
{
StartTestCommand = new DelegateCommand((time) => { StartBurnIn(3000); }, canExecute => { return !TestRunning; });
}
void StartBurnIn(int time)
{
cycles = 0;
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(time);
timer.Tick = (seder, e) => { cycles ; };
timer.Start();
}
}
但是現在當我單擊按鈕時,什么也沒有發生,它甚至沒有打到函式的開頭......
誰能告訴我出了什么問題?
uj5u.com熱心網友回復:
您只是忘記將您的 Command 宣告為屬性:
public ICommand StartTestCommand { get; set; }
要跟蹤此類錯誤,我建議您在除錯期間使用“XAML 系結失敗”選項卡。在您的示例中,它可以幫助您快速了解系結不起作用的原因:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420774.html
標籤:
