如何解决视图未在.Close()上关闭的问题

本文关键字:问题 Close 何解决 解决 视图 | 更新日期: 2023-09-27 18:27:22

为了打开ProductView,我添加了一个DialogService,到目前为止,ShowDetailDialog()正在按预期工作。

问题:

我在ProductView上调用Close(),视图未关闭。我通过在对对话框服务关闭方法的调用上设置一个断点来调试这个问题。

当我遍历代码时,null检查显示productView为null,这阻止了Close()的调用。

有人知道为什么productView为null吗?(尽管它在视图上显示数据)

对话框服务:(主持显示和关闭方法)

namespace MongoDBApp.Services
{
    class DialogService : IDialogService
    {
        Window productView = null;
        ProductView _productView;
        public DialogService()
        {
            _productView = new ProductView();
        }
        public void CloseDetailDialog()
        {
            if (productView != null)
                productView.Close(); 
        }
        public void ShowDetailDialog()
        {
            _productView.ShowDialog();
        }
    }
}

ProductViewModel:(ProductVM的摘要,调用SaveCommand上的close方法)

        private void SaveProduct(object product)
        {
            _dialogService.CloseDetailDialog();
            Messenger.Default.Send<ProductModel>(SelectedProduct);
        }

CustomerOrdersViewmodel:(最初调用ShowDetailDialog()的位置)

        private void EditOrder(object obj)
        {
            Messenger.Default.Send<ProductModel>(SelectedProduct);
            _dialogService.ShowDetailDialog();                         
        }

如何解决视图未在.Close()上关闭的问题

这就是我一直关闭窗口的方式。

这是我的命令:

class CancelCommand : ICommand
    {
        private NewTruckViewModel newTruck;
        public CancelCommand(NewTruckViewModel vm)
        {
            newTruck = vm;
        }
        public event EventHandler CanExecuteChanged;
        public bool CanExecute(object parameter)
        {
            return true;
        }
        public void Execute(object parameter)
        {
            newTruck.Cancel();
        }
    }

这是我的视图模型和从我的命令调用的方法:

 private NewTruck myWnd; //View Declaration
//Ctor where I set myView (myWnd) equal to a view that is passed in.
public NewTruckViewModel(ObservableCollection<Truck> Trucks, NewTruck wnd, bool inEditTruck)
        {
            myEngine.stopHeartBeatTimer();
            editTruck = inEditTruck;
            myWnd = wnd;
            SaveTruckCommand = new SaveTruckCommand(this);
            CancelCommand = new CancelCommand(this);
            ClearCommand = new ClearCommand(this);
            SetLevel1MTCommand = new SetLevel1MTCommand(this);
            SetLevel2MTCommand = new SetLevel2MTCommand(this);
            SetLevel3MTCommand = new SetLevel3MTCommand(this);
            SetLevel1FLCommand = new SetLevel1FLCommand(this);
            SetLevel2FLCommand = new SetLevel2FLCommand(this);
            SetLevel3FLCommand = new SetLevel3FLCommand(this);
            myTrucks = Trucks;
        }
     public void Cancel()
            {
                myWnd.Close();
            }

这对我有用。

我通过在视图上实现IDialogService解决了这个问题。然后从ViewModel中调用Show()和Close()方法。

解决方案:

接口:

public interface IDialogService
{
    void CloseDialog();
    void ShowDialog(EditProductViewModel prodVM);
}

视图:

public partial class ProductView : Window, IDialogService
{
    public ProductView()
    {
        InitializeComponent();
        this.DataContext = new EditProductViewModel(this);
    }
    public void CloseDialog()
    {
        if (this != null)
            this.Visibility = Visibility.Collapsed;
    }
    public void ShowDialog(EditProductViewModel prodVM)
    {
        this.DataContext = prodVM;
        this.Show();
    }
    private void Window_Closed(object sender, EventArgs e)
    {
        this.Visibility = Visibility.Collapsed;
    }

}

ViewModel#1:

    private IDialogService _dialogService;
    public CustomerOrdersViewModel(IDialogService dialogservice)
    {
        this._dialogService = dialogservice;                
    }

    private void EditOrder(object obj)
    {
        EditProductViewModel pvm = new EditProductViewModel(_dialogService);
        pvm.Present(pvm);
        Messenger.Default.Send<ProductModel>(SelectedProduct);                              
    }

ViewModel#2:

    private IDialogService _dialogService;

    public EditProductViewModel(IDialogService dialogService)
    {
       this._dialogService = dialogService;  
    }

    private void SaveProduct(object product)
    {
        SelectedProduct = SelectedProductTemp;
        _dialogService.CloseDialog();
    }
    public void Present(EditProductViewModel prodVM) 
    {
        _dialogService.ShowDialog(prodVM);
    }