【发布时间】:2020-02-29 03:23:22
【问题描述】:
我需要弹出一个需要时间的窗口。该按钮处于按下状态,直到打开新窗口。因此,我想在单击按钮之后和窗口打开之前在 UI 窗口上添加一个等待指示器。 ViewModel 的代码是正确的,因为我引用了一个示例代码。但是为什么我点击按钮后没有反应。
项目文件网址
https://supportcenter.devexpress.com/attachment/file/5268961b-ce35-4e40-b7c1-e33bffab902b
主窗口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
namespace WaitIndicatorDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : DXWindow
{
public MainWindow()
{
InitializeComponent();
vm = new MainVM();
DataContext = vm;
}
MainVM vm;
private void buttonShow_Click(object sender, RoutedEventArgs e)
{
vm.IsBusy = !vm.IsBusy;
}
}
}
查看模式:
using DevExpress.Mvvm;
namespace WaitIndicatorDemo
{
public class MainVM
{
private readonly ISplashScreenService _waitIndicatorService;
public virtual bool IsBusy { get; set; }
public MainVM()
{
_waitIndicatorService =
ServiceContainer.Default.GetService<ISplashScreenService>("WaitIndicatorService");
}
protected void OnIsBusyChanged()
{
if (IsBusy)
_waitIndicatorService.ShowSplashScreen();
else
_waitIndicatorService.HideSplashScreen();
}
}
}
以下是 XAML,注释的是原始示例代码。复选框绑定到 IsBusy。选中复选框时会弹出指示符。我现在想在按下按钮后弹出。
<dx:DXWindow x:Class="WaitIndicatorDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
xmlns:waitIndicatorDemo="clr-namespace:WaitIndicatorDemo"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
WindowStartupLocation="CenterScreen" SnapsToDevicePixels="True"
Title="MainWindow" Height="350" Width="525">
<!--DataContext="{dxmvvm:ViewModelSource Type=waitIndicatorDemo:MainVM}"-->
<!--Title="MainWindow" Height="350" Width="525">-->
<Grid Margin="10">
<!--<dxe:CheckEdit Content="Is Busy" IsChecked="{Binding IsBusy}"
VerticalAlignment="Top" HorizontalAlignment="Left" />
<Button Content="Button1" IsEnabled ="{Binding IsBusy, Converter={dxmvvm:BooleanNegationConverter}}"
VerticalAlignment="Top" HorizontalAlignment="Center" Click="Button_Click"/>
<Button Content="Button2" IsEnabled="{Binding IsBusy, Converter={dxmvvm:BooleanNegationConverter}}"
VerticalAlignment="Top" HorizontalAlignment="Right"/>-->
<Button x:Name="buttonShow" Content="Show" HorizontalAlignment="Left" Height="35" Margin="50,70,0,0" VerticalAlignment="Top" Width="75" Click="buttonShow_Click" />
</Grid>
</dx:DXWindow>
【问题讨论】:
-
OnIsBusyChanged被调用了吗?你在你的 xaml 中设置了吗? -
vm.IsBusy 的值变化不会自动调用 OnIsBusyChanged() 吗?
-
@selfay 不,
OnIsBusyChanged方法不会被神奇地调用,你必须在你的IsBusy设置器中自己调用它。请注意,有些工具会为您发挥作用,例如 Fody,但我建议您在尝试自动化之前先了解流程。 -
@Roger Leblanc @Chronicle 谢谢。你说的对。我将 protected void OnIsBusyChanged() 更改为 public 并调用它。有用。但奇怪的是为什么在示例代码中不调用它就可以工作。
-
@selfay DevExpress 示例代码可能使用POCOViewModel 属性来生成一些添加样板代码的神奇代码。如前所述,当您了解神奇代码为您做了什么时,它是很棒的,但您首先需要走很长的路来完全了解幕后发生的事情,然后再尝试使用像
POCOViewModel这样的属性。
标签: c# wpf devexpress viewmodel